MediaPlayer, ProgressBar

前端 未结 3 614
心在旅途
心在旅途 2020-12-03 08:18

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.

mediaPla         


        
相关标签:
3条回答
  • 2020-12-03 09:02

    Personally, I start off a Thread that checks getCurrentPosition() every 200ms or so until the onCompletion() event gets fired off:

    private class MediaObserver implements Runnable {
      private AtomicBoolean stop = new AtomicBoolean(false);
    
      public void stop() {
        stop.set(true);
      }
    
      @Override
      public void run() {
        while (!stop.get()) {
          progress.setProgress(mediaPlayer.getCurrentPosition());
          Thread.sleep(200);
        }
      }
    }
    
    private MediaObserver observer = null;
    
    public void runMedia() {
      mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener{
        @Override
        public void onCompletion(MediaPlayer mPlayer) {
          observer.stop();
          progress.setProgress(mPlayer.getCurrentPosition());
        }
      });
      observer = new MediaObserver();
      mediaPlayer.start();
      new Thread(observer).start();
    }
    
    0 讨论(0)
  • 2020-12-03 09:16

    Take a look at android.widget.MediaController, it has a progress bar.

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

    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;
    
                 // ...
            }
        }
    };
    

    To start it off use:

    mHandler.sendEmptyMessage(SHOW_PROGRESS);
    

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

    mHandler.removeMessages(SHOW_PROGRESS);
    
    0 讨论(0)
  • 2020-12-03 09:21

    The most efficient way is to use JRaymond's answer and EventBus

    private class MediaObserver implements Runnable {
        private AtomicBoolean stop = new AtomicBoolean(false);
        public void stop() {
            stop.set(true);
        }
    
        @Override public void run() {
            while (!stop.get()) {
                try {
                  if (player.isPlaying())
                     sendMsgToUI(player.getCurrentPosition(),
                                 player.getDuration());
                } catch (Exception e){e.printStackTrace();}
                try { 
                  Thread.sleep(100);
                } catch (InterruptedException e) { e.printStackTrace(); }
            }
        }
    }
    
    private MediaObserver observer = null;
    
    public void runMedia() {
        observer = new MediaObserver();
        new Thread(observer).start();
    }
    
    //handles all the background threads things for you
    private void sendMsgToUI(int position) {
        ChangingEvent event = new ChangingEvent(position);
        EventBus.getDefault().post(event);
    }
    

    ChangingEvent class would look something like that:

    public class ChangingEvent {
      private int position;
    
      public ChangingEvent(int position) {
          this.position= position;
      }
    
      public int getPosition() {
          return position;
      }
    }
    

    and in your Activity or Fragment all you have to do is

    class YouClass extends Activity {
      private EventBus eventBus = EventBus.getDefault();
    }
    
    @Override protected void onCreate(Bundle savedInstanceState) {
        //your code
        eventBus.register(this);
    }
    //eventbus that updates UI
    public void onEventMainThread(ChangingEvent event) {
        //seekbar or any other ui element
        seekBar.setProgress(event.getPosition());
    }
    
    0 讨论(0)
提交回复
热议问题