当手机响起时,我试图暂停媒体播放器.我使用
android站点的示例代码.就这样
@H_403_2@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.有什么想法为什么失去音频焦点?提前Thx.