使用Media Player在一段时间后停止缓冲数据

时间:2015-02-21 17:48:03

标签: java android media-player

我使用Media Player进行流式传输。有时因为连接速度慢或者因为其他因素导致缓冲花费了太多时间来调用onPrepared方法。我想在Media Player中停止一段时间后暂停数据。

例如:

public void play() {
     try{
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setDataSource(source);
        mMediaPlayer.prepareAsync();
    } catch (Exception e) {
       // ....
    }
}

@Override
public void onPrepared(MediaPlayer arg0) {

    // Send a message to activity to end progress dialogue
    sendBufferCompleteBroadcast();
    playMedia();

}

最好的方法是什么?

1 个答案:

答案 0 :(得分:0)

我在mMediaPlayer.prepareAsync()之后添加了以下方法。

// Prevent Media Player taking so long time
    private void postDelayed() {

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                mMediaPlayer.reset();
            }}, 5000);  // 5 seconds

    }

如果在5秒后我没有得到回复,mediaplayer被重置。