在我的應用程序中,我創建了一個類來處理播放聲音,它被稱爲SoundPlayer並且擴展AsyncTask以單獨從主線程加載聲音文件。它看起來像這樣:MediaPlayer OnCompletionListener導致錯誤
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");
}
}
我從主要活動運行這樣說:當我在我的三星Galaxy Gio的運行appliaction
soundplayer = new SoundPlayer(soundIDs, this, this);
soundplayer.execute("");
一切工作正常,但是當我在銀河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
感謝您的幫助
但它沒有發生的時候我不使用這行:「sound_player.get(I).setOnCompletionListener(SoundPlayer.this);」 沒有它一切正常,所以我認爲這個問題不是在創建許多MediaPlayer實例 – Janek
是的但(假設我的理論是正確的),你只是把一個空值到數組中,所以sound_player.get(i)將返回null,並且你正在對null對象調用setOnCompletionListener,因此NPE ... – Phil
這將被糾正,但我測試了它,並嘗試使用這個數組播放聲音,它工作正常,所以我認爲這不是問題 – Janek