在我的应用程序中,我创建了一个类来处理播放声音,它称为 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 上运行应用程序时一切正常,但是当我在 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
感谢帮助