电话结束后,MediaPlayer显示错误

时间:2018-07-25 10:47:38

标签: android android-mediaplayer android-audiomanager telephonymanager

我正在开发一个用于播放视频的应用程序。我需要处理该应用失去音频焦点的事件。因此,我正在测试由打入MediaPlayer的视频播放电话引起的中断。

电话铃响并且用户拒绝通话时,电话中断消失,MediaPlayer恢复其先前的音量。很好

当电话响起并且如果用户接受来电时,我在onPause()回调中编写了MediaPlayer.pause()。它会触发确定,并且视频已正确暂停。

但是当用户终止电话呼叫时会出现问题:然后MediaPlayer显示错误100,并且无法从暂停中恢复播放。

我的日志可能显示某些内容:

D/AudioManager: AudioManager dispatching onAudioFocusChange(-2) for android.media.AudioManager@954b834com.languagematerial.lmmovies.LMMoviesMainActivity$1@56a765d
W/AudioSystem: AudioFlinger server died!
W/MediaMetadataRetriever: MediaMetadataRetriever server died!
W/IMediaDeathNotifier: media server died
E/MediaPlayer: error (100, 0)
E/MediaPlayer: Error (100,0)
D/EGL_emulation: eglMakeCurrent: 0xaf254480: ver 2 0 (tinfo 0xaf267b60)
E/MediaPlayer: stop called in state 0
           error (-38, 0)
D/EGL_emulation: eglMakeCurrent: 0xaf254480: ver 2 0 (tinfo 0xaf267b60)
I/MediaMetadataRetrieverJNI: native_setup
V/MediaMetadataRetrieverJNI: setDataSource
I/MediaMetadataRetrieverJNI: release
W/MediaPlayer: info/warning (3, 0)
E/MediaPlayer: internal/external state mismatch corrected
W/IMediaDeathNotifier: media server died
E/MediaPlayer: error (100, 0)
W/MediaMetadataRetriever: MediaMetadataRetriever server died!
E/MediaPlayer: Error (100,0)
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa1542dc0
D/EGL_emulation: eglMakeCurrent: 0xaf254480: ver 2 0 (tinfo 0xaf267b60)
D/AudioManager: AudioManager dispatching onAudioFocusChange(1) for android.media.AudioManager@954b834com.languagematerial.lmmovies.LMMoviesMainActivity$1@56a765d
D/EGL_emulation: eglMakeCurrent: 0xaf254480: ver 2 0 (tinfo 0xaf267b60)
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa1542be0

我已经实现了AudioFocusChange的侦听器,如下所示:

ojbGloOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int audiofocusChangeInt) {
            switch (audiofocusChangeInt) {
                case AudioManager.AUDIOFOCUS_GAIN:
                    play();
                    break;
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
                    break;
                case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
                    break;
                case AudioManager.AUDIOFOCUS_LOSS:
                    pause();
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: // Here when phone call is ringing, not yet answered. Ringing implies that the sound focus of the mediaplayer is already snatched
                    pause();
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    break;
                case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
                    break;
                default:
            }
        }
    };

电话结束后,它会使用以下值进入监听器: AUDIOFOCUS_GAIN 。因此,这意味着Android试图将AudioFocus返还给我的App,问题是当它进入侦听器时,MediaPlayer已经处于错误状态,而play()无法正常工作。

如果问题不是AudioFocus,那还能是什么? 顺便说一句,避免Error状态的唯一方法是,如果我完全停止MediaPlayer :(这很荒谬)

MediaPlayer.stop(); 
MediaPlayer.reset();
MediaPlayer.release();
MediaPlayer = null;

那么,有什么想法吗?谢谢

1 个答案:

答案 0 :(得分:0)

解决了!该页面为我提供了解决方案:Create a Music Player on Android: User Controls

您只需要将此代码添加到回调覆盖的函数中: onError

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
  mp.reset();
  return false;
}

现在,您只需在AudioFocus监听器中继续播放:

objGloOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
    @Override
    public void onAudioFocusChange(int audiofocusChangeInt) {
        switch (audiofocusChangeInt) {
            case AudioManager.AUDIOFOCUS_GAIN:
                yourMediaPlayer.play();
                break;   ...}}

礼炮!