如果我的应用音频开始播放,我必须停止外部媒体播放器。对于youtube,它会自动停止,但谷歌的媒体播放器不会停止。
我使用了以下代码。
AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
AudioManager.OnAudioFocusChangeListener audioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS:
if (TextToSpeechController.getMediaPlayerInstance().isPlaying()) { TextToSpeechController.getMediaPlayerInstance().stop();}
break;
}
};
audioManager.requestAudioFocus(audioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
但它也阻止了我的其他音频。 任何帮助将不胜感激。
答案 0 :(得分:0)
试试这个
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
{
// do something - or don't
}