0

我在 android 中播放 mp3 时遇到问题,类似于延迟或滞后,例如:

如果我必须重现以下内容:“你好,你好吗?”,它只会播放“你好吗?” 或者说很低的“你好”。

它发生在 ViewSonic V220 它的 22 英寸平板电脑中,在大多数其他设备中,它工作正常,但在那个似乎失败的设备中。

这很奇怪,因为其他应用程序(如 youtube 或媒体播放器)运行良好。

这是我的代码,也许我做错了什么:

public class SoundManager implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {
private Parent mParent;
private MediaPlayer mediaPlayer;

String[] mp3_array;
int counter = 0;

public SoundManager(Parent parent) {
    mParent = parent;
}

public void playSound(String[] url) throws IllegalArgumentException,
        IllegalStateException, IOException {
    mp3_array = url;
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.reset();
    }

    mediaPlayer.setDataSource(url[0]);
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnPreparedListener(this);

    mediaPlayer.prepareAsync();
}

public void stopMediaPlayer() {
    if (mediaPlayer != null) {
        mediaPlayer.release();
        mediaPlayer = null;
        mp3_array = null;
        counter = 0;
    }
}

@Override
public void onCompletion(MediaPlayer mp) {
    try {

        Integer c = counter;

        if (mp3_array != null && counter + 1 < mp3_array.length) {
            mp.reset(); 
            mp.setOnCompletionListener(this);
            mp.setOnPreparedListener(this);
            counter += 1;
            mp.setDataSource(mp3_array[counter]);
            mediaPlayer.prepareAsync();
        } else {
            if (mParent != null)
                mParent.invokeJs("playSoundEnded()");
            mp.release();
            mp = null;
            mp3_array = null;
            counter = 0;
        }

    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

@Override
public void onPrepared(MediaPlayer mp) {
    if (mParent != null)
        mParent.invokeJs("playSoundStarted()");
    mp.start();

}

}

4

0 回答 0