MediaPlayer的,进度进度、MediaPlayer

2023-09-06 03:54:01 作者:忘羡

这是播放媒体时更新进度的正确方法?我想会有的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.

推荐答案

看看 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);