如何解决MediaPlayer:错误(1,-19)

时间:2018-09-05 09:56:34

标签: android android-mediaplayer media-player

我正在播放非活动类的媒体播放器,经常播放音频剪辑时遇到问题,媒体播放器抛出MediaPlayer:错误(1,-19)。一旦发生此错误,媒体播放器将根本无法播放。我已经完成了一些links的学习,但没有任何解决方法。任何帮助将不胜感激。

 handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                delayInExecution = false;
                if (!mLeftOpen && !mRightOpen) {
                    System.out.println("Eyes are closed man....");
                    try {
                        if (!player.isPlaying()) {
                            player = MediaPlayer.create(EyeDetectionApplication.getInstance(), R.raw.beep);
                            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                            player.setLooping(false);
                            player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                                @Override
                                public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {

                                    System.out.println("setOnErrorListener ....");
                                    MediaPlayer mediaPlayer1 = MediaPlayer.create(EyeDetectionApplication.getInstance(), R.raw.beep);
                                    mediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);
                                    mediaPlayer1.setLooping(false);
                                    mediaPlayer1.start();
                                    player = mediaPlayer;
                                    return false;
                                }
                            });
                            player.start();
                        }
                    } catch (Exception e) {
                        System.out.println("Exception came....");
                        player = MediaPlayer.create(EyeDetectionApplication.getInstance(), R.raw.beep);
                        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        //   player.setOnPreparedListener(EyeDetectionApplication.getInstance());
                        player.setLooping(false);
                        player.start();
                    }
                } else {
                    if (mLeftOpen && mRightOpen && player.isPlaying()) {
                        player.release();
                    }
                }

            }
        }, 1500);

1 个答案:

答案 0 :(得分:1)

在媒体播放器中添加OnCompletionListener并释放它。

player.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
    mp.release();
};
});

从处理程序内部移至

 player = MediaPlayer.create(EyeDetectionApplication.getInstance(), R.raw.beep);
  handler.postDelayed(new Runnable() {
        @Override
        public void run() {//other code
        }
         })