当文本到语音系统说出短语时,我正在尝试使音频播放器静音。如预期的那样,当请求音频焦点时,audiomanager会减小音量,但是当调用abandonAudioFocus时,该音量不会恢复,也不会调用abandonAudioFocus的回调。
这是我的代码:

private class SpeakRunnable implements Runnable {
    String mText;

    public SpeakRunnable(String text) {
        mText = text;
    }

    @Override
    public void run() {
        stream_volume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
        am.requestAudioFocus(new OnAudioFocusChangeListener() {
            @Override
            public void onAudioFocusChange(int focusChange) {
                  //Check if focus granted
                  .....
                  HashMap<String, String> params = new HashMap<String, String>();
                  params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "none");
                  talker.speak(mText, TextToSpeech.QUEUE_ADD, params);
            }
        },
        AudioManager.STREAM_MUSIC,
        AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

    }
}


和:

talker.setOnUtteranceProgressListener(new UtteranceProgressListener() {

            @Override
            public void onStart(String utteranceId) {

            }

            @Override
            public void onError(String utteranceId) {

            }

            @Override
            public void onDone(String utteranceId) {
                am.abandonAudioFocus(new OnAudioFocusChangeListener() {
                    @Override
                    public void onAudioFocusChange(int focusChange) {
                        Log.v(app_id, "CALLBACK");
                    }
                });
            }
        });


任何想法?提前致谢。

最佳答案

您需要将传递给AudioManager.OnAudioFocusChangeListener的相同abandonAudioFocus传递给requestAudioFocus。在the documentation for abandonAudioFocus中对此进行了描述:

public int abandonAudioFocus (AudioManager.OnAudioFocusChangeListener l)

Parameters
l   the listener with which focus was requested.


而且,我不认为您会因为放弃音频焦点而在监听器中调用onAudioFocusChange方法。当您放弃音频焦点时,将向其他一些请求音频焦点的音频焦点客户端通知其已获得焦点。
如果您想了解如何在Android内部实现此功能,可以查看MediaFocusControl source code

10-08 10:58