我希望在某些其他媒体播放器应用程序启动时停止媒体播放器。 我不知道该怎么做。
答案 0 :(得分:2)
您需要为AudioFocusChange添加侦听器。 请参阅:https://developer.android.com/reference/android/media/AudioManager.OnAudioFocusChangeListener.html
答案 1 :(得分:1)
我在一个项目中做了类似的事情,当我的应用程序开始播放时,Google Play音乐暂停,反之亦然。
AudioManager am = null;
// Request focus for music stream and pass AudioManager.OnAudioFocusChangeListener
// implementation reference
int result = am.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if(result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
{
// Play
}
// Implements AudioManager.OnAudioFocusChangeListener
@Override
public void onAudioFocusChange(int focusChange)
{
if(focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
{
// Pause
}
else if(focusChange == AudioManager.AUDIOFOCUS_GAIN)
{
// Resume
}
else if(focusChange == AudioManager.AUDIOFOCUS_LOSS)
{
// Stop or pause depending on your need
}
}