android seekto实现_Android MediaPlayer SeekTo函数解决方法

I am creating a very simple media player app. I would like to resume the song from the SeekTo position that I have captured using getCurrentPosition. However seekTo does not start from the position I have captured but from the beginning.

Code to capture current position, pause and change button text to Click to resume

int media_length = mediaplayer.getCurrentPosition();

Toast.makeText(this,"media length is"+media_length, Toast.LENGTH_LONG).show();

mbutton.setText("Click to Resume");

mediaplayer.pause();

Code to seekTo captured position, start and change button text to Click to Pause

mediaplayer.seekTo(media_length);

mediaplayer.start();

mbutton.setText("Click to Pause");

A couple of posts are there already related to it but they seem to claim a bug in Android.

Ref: MediaPlayer seekTo doesn't work and

is there any workaround for this? appreciate any help.

解决方案

You can try below code. It is working for me...

public void forwardSong() {

if (mPlayer != null) {

int currentPosition = mPlayer.getCurrentPosition();

if (currentPosition + seekForwardTime <= mPlayer.getDuration()) {

mPlayer.seekTo(currentPosition + seekForwardTime);

} else {

mPlayer.seekTo(mPlayer.getDuration());

}

}

}

You can pause mediaplayer before this and just call start method after this method.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值