0

我有很多问题MediaPlayer。它可能与旧的操作系统版本有关,至少在 2.3 之前。

一个声音在某个Activity(在启动另一个之前)播放良好当我弹出第二个Activity以返回第一个时,我在弹出之前播放声音文件(我使用 super.onBackPressed)。

在这些旧设备(~2.3)上,声音文件被切断。我使用相同的方式启动和播放MediaPlayer.

我创建了MediaPlayer使用create(同步)。我MediaPlayer通过将它放在一个静态容器中来保留它。我只在setOnCompletionListener. 这是怎么回事..

这是一些代码...

// Start sound
adhocplaysoundstatic(R.raw.soundfile, getApplicationContext()); 
// Pop activity
super.onBackPressed();

定义

static ArrayList<MediaPlayer> staticplayers = new ArrayList<MediaPlayer>();
static public void adhocplaysoundstatic(final int MEDIA, Context context) {
    MediaPlayer player = getMediaPlayerStatic(MEDIA, context);
    staticplayers.add(player); // Hold on to
    player.setOnCompletionListener(new OnCompletionListener() {
        @Override public void onCompletion(MediaPlayer mp) {
            mp.release();
            staticplayers.remove(mp);
        }
    });
    player.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            Log.d(LOG_TAG, "onPrepared, play!");
            mp.start();             
        }});
    //player.start();
}

更多的...

static protected MediaPlayer getMediaPlayerStatic(int id, Context context) throws RuntimeException {
    MediaPlayer player = MediaPlayer.create(context, id);
    if (player == null) {
        throw new RuntimeException("Failed creating media player from id " + id);
    }
    else {
        player.setOnErrorListener(new OnErrorListener() {
            @Override public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.d(LOG_TAG, "MediaPlayer error: " + mp.toString() + ", what: " + what + ", extra: " + extra);
                return false;
            }
        });
    }
    return player;
}
4

0 回答 0