Android MediaPlayer SeekTo功能解决方法 [英] Android MediaPlayer SeekTo function workaround

查看:1646
本文介绍了Android MediaPlayer SeekTo功能解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个非常简单的媒体播放器应用程序.我想从使用getCurrentPosition捕获的SeekTo位置恢复歌曲.但是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");

已经有几篇与之相关的文章,但它们似乎声称是Android中的一个错误. 参考: MediaPlayer seekTo不起作用, 有什么解决方法吗?感谢您的帮助.

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());
        }
    }
}

您可以在此之前暂停mediaplayer,然后在此方法之后调用start方法.

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

这篇关于Android MediaPlayer SeekTo功能解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆