Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中获取/设置媒体音量(spotify)和TTS?_Java_Android_Text To Speech_Volume_Audio - Fatal编程技术网

Java 如何在Android中获取/设置媒体音量(spotify)和TTS?

Java 如何在Android中获取/设置媒体音量(spotify)和TTS?,java,android,text-to-speech,volume,audio,Java,Android,Text To Speech,Volume,Audio,减少流体积,同时增加TTS体积 我有一个Android应用程序,每次都与TTS对话,并使用流媒体音乐(spotify)。所以,我需要减小音量,增加TTS 我使用此代码,但这也会降低tts AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,[int value],[if d

减少流体积,同时增加TTS体积

我有一个Android应用程序,每次都与TTS对话,并使用流媒体音乐(spotify)。所以,我需要减小音量,增加TTS

我使用此代码,但这也会降低tts

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,[int value],[if desired a flag]);
任何减少spotify的方法都像流音乐一样


在Java/android中。

您要做的是

下面是一些示例代码:

   /**
     * Our {@link AudioManager.OnAudioFocusChangeListener}
     */
    private static AudioManager.OnAudioFocusChangeListener audioFocus = new AudioManager.OnAudioFocusChangeListener() {

        @Override
        public void onAudioFocusChange(final int focusChange) {
            if (DEBUG) {
                MyLog.i(CLS_NAME, "AudioManager focusChange: " + focusChange);
            }

            switch (focusChange) {

                case AudioManager.AUDIOFOCUS_GAIN:
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS_GAIN");
                    }
                    break;
                case AudioManager.AUDIOFOCUS_LOSS:
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS_LOSS");
                    }
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS_LOSS_TRANSIENT");
                    }
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
                    }
                    break;
                default:
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS default");
                    }
                    break;
            }
        }
    };

    /**
     * Duck the audio
     *
     * @param ctx the application context
     */
    public static void duckAudioMedia(final Context ctx) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "duckAudioMedia");
        }

        try {

            final AudioManager audioManager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);

            switch (audioManager.requestAudioFocus(audioFocus, AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK)) {

                case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
                    if (DEBUG) {
                        MyLog.w(CLS_NAME, "AudioManager duckAudioMedia AUDIOFOCUS_REQUEST_FAILED");
                    }
                    break;
                case AudioManager.AUDIOFOCUS_REQUEST_GRANTED:
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "AudioManager duckAudioMedia AUDIOFOCUS_REQUEST_GRANTED");
                    }
                    break;
                default:
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "AudioManager duckAudioMedia AUDIOFOCUS default");
                    }
                    break;
            }
        } catch (final NullPointerException e) {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "duckAudioMedia: NullPointerException");
                e.printStackTrace();
            }
        } catch (final Exception e) {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "duckAudioMedia: Exception");
                e.printStackTrace();
            }
        }
    }

   /**
     * Notify the System that any previous condition requiring to duck or pause audio is now complete.
     *
     * @param ctx the application context
     */
    public static void abandonAudioMedia(final Context ctx) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "abandonAudioMedia");
        }

        try {

            final AudioManager audioManager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);

            switch (audioManager.abandonAudioFocus(audioFocus)) {

                case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
                    if (DEBUG) {
                        MyLog.w(CLS_NAME, "AudioManager abandonAudioMedia AUDIOFOCUS_REQUEST_FAILED");
                    }
                    break;
                case AudioManager.AUDIOFOCUS_REQUEST_GRANTED:
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "AudioManager abandonAudioMedia AUDIOFOCUS_REQUEST_GRANTED");
                    }
                    break;
                default:
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "AudioManager abandonAudioMedia AUDIOFOCUS default");
                    }
                    break;
            }
        } catch (final NullPointerException e) {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "abandonAudioMedia: NullPointerException");
                e.printStackTrace();
            }
        } catch (final Exception e) {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "abandonAudioMedia: Exception");
                e.printStackTrace();
            }
        }
    }
您必须删除我的自定义日志记录


在TTS启动时呼叫
duckAudioMedia
,在演讲结束时呼叫
放弃AudioMedia

你真的救了我一天