在我的应用程序中,我创建了一个处理播放声音的类,它被称为SoundPlayer,并扩展AsyncTask以从主线程单独加载声音文件。它看起来像这样:
public class SoundPlayer extends AsyncTask<String, Integer, String> implements
OnCompletionListener {
private Integer[] soundIDs = {};
private ArrayList<MediaPlayer> sound_player;
// private MediaPlayer[] sound_player;
private Context context;
private static final String DEBUG_TAG = "SoundPlayer";
private Timer timer;
private Activity activity;
public SoundPlayer(Integer[] soundIDs, Context context, Activity activity) {
this.soundIDs = soundIDs;
this.context = context;
this.activity = activity;
}
@Override
protected String doInBackground(String... params) {
sound_player = new ArrayList<MediaPlayer>();
for (int i = 0; i < soundIDs.length; i++) {
MediaPlayer temp = new MediaPlayer();
temp = MediaPlayer.create(context, soundIDs[i]);
sound_player.add(temp);
sound_player.get(i).setOnCompletionListener(SoundPlayer.this);
}
return null;
}
@Override
public void onCompletion(MediaPlayer mp) {
Log.e(DEBUG_TAG, "comp");
}
}
我以这种方式从主要活动中运行它:
soundplayer = new SoundPlayer(soundIDs, this, this);
soundplayer.execute("");
当我在三星Galaxy Gio上运行appliaction时,一切正常,但是当我在Galaxy S2上运行时,它无法正常工作。我注意到问题出在那一行:
sound_player.get(i).setOnCompletionListener(SoundPlayer.this);
没有它一切正常(在两个设备上),但是我必须设置OnCompletionListsner所以这是错的?我也尝试过这样做:
sound_player.get(i).setOnCompletionListener((OnCompletionListener) context);
让我的主要活动实现onCompletionListener方法,但它也不起作用。这是LogCat:
06-18 16:15:27.835 W/MediaPlayer(20250): info/warning (1, 44)
06-18 16:15:27.865 W/MediaPlayer(20250): info/warning (1, 44)
06-18 16:15:27.900 W/MediaPlayer(20250): info/warning (1, 44)
06-18 16:15:27.905 I/MediaPlayer(20250): Info (1,44)
06-18 16:15:27.905 I/MediaPlayer(20250): Info (1,44)
06-18 16:15:27.905 I/MediaPlayer(20250): Info (1,44)
06-18 16:15:27.950 W/MediaPlayer(20250): info/warning (1, 44)
06-18 16:15:27.990 W/MediaPlayer(20250): info/warning (1, 44)
06-18 16:15:28.005 I/MediaPlayer(20250): Info (1,44)
06-18 16:15:28.005 I/MediaPlayer(20250): Info (1,44)
06-18 16:15:28.035 W/MediaPlayer(20250): info/warning (1, 44)
06-18 16:15:28.035 I/MediaPlayer(20250): Info (1,44)
06-18 16:15:28.060 W/MediaPlayer(20250): info/warning (1, 44)
06-18 16:15:28.060 I/MediaPlayer(20250): Info (1,44)
06-18 16:15:28.085 W/MediaPlayer(20250): info/warning (1, 26)
06-18 16:15:28.085 I/MediaPlayer(20250): Info (1,26)
06-18 16:15:28.085 E/MediaPlayer(20250): error (1, -17)
06-18 16:15:28.085 E/MediaPlayer(20250): Error (1,-17)
06-18 16:15:28.090 D/MediaPlayer(20250): create failed:
06-18 16:15:28.090 D/MediaPlayer(20250): java.io.IOException: Prepare failed.: status=0x1
06-18 16:15:28.090 D/MediaPlayer(20250): at android.media.MediaPlayer.prepare(Native Method)
06-18 16:15:28.090 D/MediaPlayer(20250): at android.media.MediaPlayer.create(MediaPlayer.java:691)
06-18 16:15:28.090 D/MediaPlayer(20250): at com.artostolab.scaryringtones.SoundPlayer.doInBackground(SoundPlayer.java:36)
06-18 16:15:28.090 D/MediaPlayer(20250): at com.artostolab.scaryringtones.SoundPlayer.doInBackground(SoundPlayer.java:1)
06-18 16:15:28.090 D/MediaPlayer(20250): at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-18 16:15:28.090 D/MediaPlayer(20250): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
06-18 16:15:28.090 D/MediaPlayer(20250): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
06-18 16:15:28.090 D/MediaPlayer(20250): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
06-18 16:15:28.090 D/MediaPlayer(20250): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
06-18 16:15:28.090 D/MediaPlayer(20250): at java.lang.Thread.run(Thread.java:1019)
和
06-18 16:25:10.330 W/MediaPlayer(21988): info/warning (1, 26)
06-18 16:25:10.330 E/MediaPlayer(21988): error (1, -17)
06-18 16:25:10.335 I/MediaPlayer(21988): Info (1,26)
06-18 16:25:10.335 E/MediaPlayer(21988): Error (1,-17)
06-18 16:25:10.335 D/MediaPlayer(21988): create failed:
06-18 16:25:10.335 D/MediaPlayer(21988): java.io.IOException: Prepare failed.: status=0x1
06-18 16:25:10.335 D/MediaPlayer(21988): at android.media.MediaPlayer.prepare(Native Method)
06-18 16:25:10.335 D/MediaPlayer(21988): at android.media.MediaPlayer.create(MediaPlayer.java:691)
06-18 16:25:10.335 D/MediaPlayer(21988): at com.artostolab.scaryringtones.SoundPlayer.doInBackground(SoundPlayer.java:38)
06-18 16:25:10.335 D/MediaPlayer(21988): at com.artostolab.scaryringtones.SoundPlayer.doInBackground(SoundPlayer.java:1)
06-18 16:25:10.335 D/MediaPlayer(21988): at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-18 16:25:10.335 D/MediaPlayer(21988): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
06-18 16:25:10.335 D/MediaPlayer(21988): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
06-18 16:25:10.335 D/MediaPlayer(21988): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
06-18 16:25:10.335 D/MediaPlayer(21988): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
06-18 16:25:10.335 D/MediaPlayer(21988): at java.lang.Thread.run(Thread.java:1019)
06-18 16:25:10.335 W/dalvikvm(21988): threadid=8: thread exiting with uncaught exception (group=0x4001e578)
06-18 16:25:10.340 E/AndroidRuntime(21988): FATAL EXCEPTION: AsyncTask #1
06-18 16:25:10.340 E/AndroidRuntime(21988): java.lang.RuntimeException: An error occured while executing doInBackground()
06-18 16:25:10.340 E/AndroidRuntime(21988): at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-18 16:25:10.340 E/AndroidRuntime(21988): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
06-18 16:25:10.340 E/AndroidRuntime(21988): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
06-18 16:25:10.340 E/AndroidRuntime(21988): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
06-18 16:25:10.340 E/AndroidRuntime(21988): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
06-18 16:25:10.340 E/AndroidRuntime(21988): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
06-18 16:25:10.340 E/AndroidRuntime(21988): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
06-18 16:25:10.340 E/AndroidRuntime(21988): at java.lang.Thread.run(Thread.java:1019)
06-18 16:25:10.340 E/AndroidRuntime(21988): Caused by: java.lang.NullPointerException
06-18 16:25:10.340 E/AndroidRuntime(21988): at com.artostolab.scaryringtones.SoundPlayer.doInBackground(SoundPlayer.java:40)
06-18 16:25:10.340 E/AndroidRuntime(21988): at com.artostolab.scaryringtones.SoundPlayer.doInBackground(SoundPlayer.java:1)
06-18 16:25:10.340 E/AndroidRuntime(21988): at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-18 16:25:10.340 E/AndroidRuntime(21988): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
06-18 16:25:10.340 E/AndroidRuntime(21988): ... 4 more
06-18 16:25:10.345 E/ (2696): Dumpstate > /data/log/dumpstate_app_error
06-18 16:25:10.345 W/ActivityManager(2696): Force finishing activity com.artostolab.scaryringtones/.MainList
感谢您的帮助
答案 0 :(得分:0)
我认为发生的事情是您的调用MediaPlayer.create(context, soundIDs[i]);
失败了(请参阅堆栈跟踪 - “java.io.IOException:Prepare failed”),因此temp为null。
创建许多MediaPlayer实例可能是根本原因。我有一个播放一系列单个声音样本的应用程序,我会动态创建我的实例。
答案 1 :(得分:0)
我自己找到了一个解决方案,不知道为什么但是在doInBackground方法中设置onCompletionListener会导致某些设备上出现空指针异常,所以我想通过这种方法设置OnCompletionListner,例如当你想播放声音时它工作了
答案 2 :(得分:0)
尝试在setOnCompletionListener
方法之前将onClickCompletion
与On Create
放在一起。然后在onClickCOmpletion
为我工作后致电start()
/**
* This listener gets triggered when the {@link MediaPlayer} has completed
* playing the audio file.
*/
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
// Now that the sound file has finished playing, release the media player resources.
Toast.makeText(getApplicationContext(), "Finish sound", Toast.LENGTH_SHORT).show();
mediaPlayer.release();
}
};