当电话响起时,我正在尝试暂停媒体播放器。我使用来自 android 网站的示例代码。就像这样;
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
// resume playback
if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
mMediaPlayer.start();
mMediaPlayer.setVolume(1.0f, 1.0f);
}
break;
case AudioManager.AUDIOFOCUS_LOSS:
// Lost focus for an unbounded amount of time: stop playback and
// release media player
stopMediaPlayer();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
// Lost focus for a short time, but we have to stop
// playback. We don't release the media player because playback
// is likely to resume
if (mMediaPlayer.isPlaying())
mMediaPlayer.pause();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
// Lost focus for a short time, but it's ok to keep playing
// at an attenuated level
if (mMediaPlayer.isPlaying())
mMediaPlayer.setVolume(0.1f, 0.1f);
break;
}
}
手机响铃时发送 AUDIOFOCUS_LOSS_TRANSIENT;没关系。当通话结束时,发送 AUDIOFOCUS_GAIN 并且播放器继续播放;这也可以。但是在发送 AUDIOFOCUS_GAIN 之后,立即发送 AUDIOFOCUS_LOSS。有什么想法为什么它会失去音频焦点?提前谢谢。