如何在启动活动中设置android mediaPlayer的持续时间

时间:2015-07-16 08:01:26

标签: java android android-mediaplayer

我需要帮助才能在开始活动中设置MediaPlayer的持续时间。

我尝试使用seekTo(),但效果不正确。

以下是我正在做的事情:

protected void Player(String bURL, int seekTo){
    btn = (Button) findViewById(R.id.playpauseBtn);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(bURL);
        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                if (!mediaPlayer.isPlaying()) {
                    if (seekTo != 0) {
                        mediaPlayer.seekTo(seekTo);
                    }
                    mediaPlayer.start();
                }
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:1)

尝试修改为:

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            if (!mp.isPlaying()) {
                if (seekTo != 0) {
                   mp.seekTo(seekTo);
                }
                mp.start();
            }

        }
    });
mediaPlayer.setDataSource(bURL);
mediaPlayer.prepareAsync();