在android中拨打电话后调用了AUDIOFOCUS_LOSS [英] AUDIOFOCUS_LOSS called after a phone call in android

查看:845
本文介绍了在android中拨打电话后调用了AUDIOFOCUS_LOSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在手机响铃时暂停媒体播放器。我使用android站点的示例代码。就像这样;

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;
        }
    }

当手机响铃时发送AUDIOFOCUS_LOSS_TRANSIENT;这没关系。当通话结束时,发送AUDIOFOCUS_GAIN并且播放器继续播放;哪个也行。但是在发送AUDIOFOCUS_GAIN之后,发送了AUDIOFOCUS_LOSS。有什么想法它失去音频焦点? Thx提前。

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.

推荐答案

可能它会对某人有所帮助。
我在项目中没有直接使用MediaPlayer而是通过VideoView时出现此问题。
打开文件时有以下代码

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);

因此,当您打开文件时,它会获得音频焦点和听众,这是在使用focusChange触发之前设置的code> AudioManager.AUDIOFOCUS_LOSS 。

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

这篇关于在android中拨打电话后调用了AUDIOFOCUS_LOSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆