MediaPlayer的,进度 [英] MediaPlayer, ProgressBar

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

问题描述

这是播放媒体时更新进度的正确方法?我想会有的MediaPlayer的回调,但我无法找到它。

Is this the correct way to update a ProgressBar when playing Media? I figured there would be a callback in MediaPlayer, but I couldn't find it.

mediaPlayer.start();
final SeekBar progress = (SeekBar) dialog.findViewById(R.id.seekBar1);
progress.setMax(mediaPlayer.getDuration());
new CountDownTimer(mediaPlayer.getDuration(), 250) {
  public void onTick(long millisUntilFinished) {
    progress.setProgress(progress.getProgress() + 250);
  }
  public void onFinish() {}
}.start();

最好的问候。

Best regards.

推荐答案

看看 <一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/MediaController.java">android.widget.MediaController,它有一个进度条。 (链接的版本是旧的,你可以在这里找到新的 - 不知道,如果它的改变)。

Take a look at android.widget.MediaController, it has a progress bar. (the linked version is old, you can find the new one here - not sure if it's changed).

他们使用一个处理程序,递归调用自身,如果合适。您可以设置延迟但往往你想要的进度条进行更新。

They use a handler that recursively calls itself if appropriate. You can set the delay to however often you want the progress bar updated.

请注意,控制器可以显示或隐藏以及由用户拖动,以及 - 当然 - 视频可以停止。这些都是原因,各种检查( mDragging和放大器;!&安培; mShowing和放大器;&安培; mVideoView.isPlaying())。另一个递归调用,更新前杠

Note that the controller can be shown or hidden as well as dragged by the user, and - of course - the video can stop. These are the reasons for the various checks (!mDragging && mShowing && mVideoView.isPlaying()) before another recursive call to update the bar.

protected Handler mHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        int pos;
        switch (msg.what)
        {
            // ...

            case SHOW_PROGRESS:
                pos = setProgress();
                if (!mDragging && mShowing && mVideoView.isPlaying())
                {
                    msg = obtainMessage(SHOW_PROGRESS);
                    sendMessageDelayed(msg, 1000 - (pos % 1000));
                }
                break;

             // ...
        }
    }
};

要启动它关闭使用:

mHandler.sendEmptyMessage(SHOW_PROGRESS);

他会自己停下来,但你应该使用取消最后一个悬而未决的要求:

It will stop by itself, but you should cancel the last pending request using:

mHandler.removeMessages(SHOW_PROGRESS);

这篇关于MediaPlayer的,进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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