Android MediaPlayer在通话结束后显示错误

问题描述 投票:0回答:1

我正在用Android开发一个应用来播放视频。我需要处理该应用失去音频焦点的事件。因此,我正在测试由打入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;

所以,有什么想法吗?谢谢

android android-mediaplayer android-audiomanager telephonymanager
1个回答
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;   ...}}

Saludos!

© www.soinside.com 2019 - 2024. All rights reserved.