在文字转语音系统说出短语的同时,我试图让音频播放器静音。正如预期的那样,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");
}
});
}
});
有什么想法吗?提前谢谢。
答案 0 :(得分:2)
您需要将传递给AudioManager.OnAudioFocusChangeListener
的{{1}}传递给abandonAudioFocus
。这在the documentation for abandonAudioFocus
:
requestAudioFocus
此外,我认为您不应期望在放弃音频焦点时调用您的侦听器中的public int abandonAudioFocus (AudioManager.OnAudioFocusChangeListener l)
Parameters
l the listener with which focus was requested.
方法。当您放弃音频焦点时,已请求音频焦点的其他音频焦点客户端将被通知其已获得焦点。
如果您想了解如何在Android内部实施此功能,您可以查看MediaFocusControl
source code。