Android Media播放器不断崩溃

时间:2014-05-02 09:59:50

标签: android android-mediaplayer

在尝试修复我的媒体播放器代码问题两周后,继续让用户报告崩溃,我最终试图看到我做错了。

在主类中初始化的媒体播放器和背景声音对象:

public class GameOver extends Activity{

public AdView adView;
BackgroundSound mBackgroundSound;
MediaPlayer mMediaPlayer;

这是我的媒体播放器代码和与之相关的方法,我不会在后面按下任何东西:

    public class BackgroundSound extends AsyncTask<Void, Void, Void> {

    protected void onPreExecute() {
        mMediaPlayer = MediaPlayer.create(GameOver.this, R.raw.welldone);
    }

    protected Void doInBackground(Void... params) {
        mMediaPlayer.setVolume(100, 100);
        mMediaPlayer.start();
        mMediaPlayer.setLooping(true); // Set looping
        return null;
    }

    protected void onCancelled(Void v) {
        try {
            mMediaPlayer.stop();
            mMediaPlayer.reset();
            mMediaPlayer.release();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

public void onPause() {
    super.onPause();
    mBackgroundSound.onCancelled((Void) null);
}

public void onResume() {
    super.onResume();
    mBackgroundSound = new BackgroundSound();
    mBackgroundSound.execute((Void) null);

}

以下是一些示例崩溃:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.IllegalStateException
at android.media.MediaPlayer.setVolume(Native Method)
at com.deucalion0.findtheanimal.Menu$BackgroundSound.doInBackground(Menu.java:235)
at com.deucalion0.findtheanimal.Menu$BackgroundSound.doInBackground(Menu.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
... 4 more

另一个:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.IllegalStateException
at android.media.MediaPlayer.setLooping(Native Method)
at com.deucalion0.findtheanimal.Menu$BackgroundSound.doInBackground(Menu.java:234)
at com.deucalion0.findtheanimal.Menu$BackgroundSound.doInBackground(Menu.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more

我已经阅读了很多Stackoverflow问题,试图理解Google文档,我尝试的所有内容似乎都是一个糟糕的解决方案。我不知道如何处理,有人可以提出建议吗?

1 个答案:

答案 0 :(得分:4)

为什么以这种方式使用AsyncTask?

它现在做了什么:

  • 在主线程上创建MediaPlayer - 这是一个漫长的过程,因此它应该转到后台线程,它可能会导致ANR。
  • 从后台线程调用setVolume,start,setLooping - 这些是非常短的命令,因此无需在后台运行它们。
  • 从主线程调用停止,重置和释放。

什么是可行的解决方案:

  • 从后台主题创建MediaPlayer。
  • 创建后,设置循环并开始播放。 (这些是快速调用,因此如果您愿意,可以从主线程执行此操作,但只能在创建MediaPlayer之后执行。)
  • 当您想要停止播放时(在您的示例中,当调用onPause时),您可以调用stop()和release()。但是之前检查一下,如果它已经创建并启动(因为它是在后台线程中完成的,那么你可以在媒体播放器开始播放音频之前暂停你的活动)。

编辑:这是我测试的源代码,它适用于我(即使媒体播放器的准备时间非常慢,并且活动在加载之前暂停):

private MediaPlayer mMediaPlayer;
private volatile boolean mMediaPlayerStopped ;

@Override
protected void onPause() {
    Log.v("MusicTest", "Paused activity") ;
    stopMediaPlayer();
    super.onPause();
}

private void stopMediaPlayer() {
    Log.v("MusicTest", "Stopping MediaPlayer") ;
    mMediaPlayerStopped = true ;
    if (mMediaPlayer != null) {
        mMediaPlayer.stop() ;
        mMediaPlayer.reset() ;
        mMediaPlayer.release() ;
        mMediaPlayer = null ;
    }
}

private void startMediaPlayer() {
    mMediaPlayerStopped = false ;
    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            Log.v("MusicTest", "Creating MediaPlayer") ;
            MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.alarm10);
            mediaPlayer.setVolume(100, 100) ;
            mediaPlayer.setLooping(true) ;
            Log.v("MusicTest", "Starting MediaPlayer") ;
            mediaPlayer.start() ;
            mMediaPlayer = mediaPlayer ;
            if (mMediaPlayerStopped) {
                Log.v("MusicTest", "MediaPlayer was stopped while preparing") ;
                stopMediaPlayer();
            }
            return null;
        }
    }.execute();
}

@Override
protected void onResume() {
    Log.v("MusicTest", "Resumed activity") ;
    startMediaPlayer();
    super.onResume();
}