在尝试修复我的媒体播放器代码问题两周后,继续让用户报告崩溃,我最终试图看到我做错了。
在主类中初始化的媒体播放器和背景声音对象:
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文档,我尝试的所有内容似乎都是一个糟糕的解决方案。我不知道如何处理,有人可以提出建议吗?
答案 0 :(得分:4)
为什么以这种方式使用AsyncTask?
它现在做了什么:
什么是可行的解决方案:
编辑:这是我测试的源代码,它适用于我(即使媒体播放器的准备时间非常慢,并且活动在加载之前暂停):
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();
}