Android音频延迟

时间:2013-08-29 00:08:56

标签: android audio android-mediaplayer

我在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();

}

}

0 个答案:

没有答案