android释放焦点_在android中拨打电话后调用了AUDIOFOCUS_LOSS

I'm trying to pause media player when the phone rings. I use the sample code from android site. It's like this;

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;

}

}

When the phone rings AUDIOFOCUS_LOSS_TRANSIENT is sent; which is OK. And when the call ends the AUDIOFOCUS_GAIN is sent and the player continue to play; which is also OK. But right after sending AUDIOFOCUS_GAIN, AUDIOFOCUS_LOSS is sent. Have any ideas why it is losing audio focus? Thx in advance.

解决方案

Probably it will help somebody.

I had this issue when in project wasn't using directly MediaPlayer but through VideoView.

It has following code while opening the file

AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);

am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

So as you open the file it gains audio focus and listener that is set before gets fired with focusChange AudioManager.AUDIOFOCUS_LOSS.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值