1

在我的应用程序中,我创建了一个类来处理播放声音,它称为 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

感谢帮助

4

3 回答 3

0

我认为正在发生的事情是您的调用MediaPlayer.create(context, soundIDs[i]);失败(请参阅堆栈跟踪 - “java.io.IOException:Prepare failed”)所以 temp 为空。

创建许多 MediaPlayer 实例可能是根本原因。我有一个应用程序可以播放一系列单独的声音样本,并且我会即时创建我的实例。

于 2013-06-18T14:55:56.550 回答
0

我自己找到了一个解决方案,不知道为什么,但是在 doInBackground 方法中设置 onCompletionListener 会导致某些设备上出现空指针异常,所以我想在此方法之外设置 OnCompletionListner,例如当你想播放声音并且它有效时

于 2013-06-19T11:24:12.500 回答
0

尝试setOnCompletionListener使用onClickCompletionbeforeOn Create方法放置。然后onClickCOmpletionstart()它对我有用后打电话

/**
 * 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();
    }
};
于 2018-05-19T22:29:36.613 回答