开发者

Use a Timer or MediaPlayer method?

开发者 https://www.devze.com 2023-03-29 02:54 出处:网络
I am currently calling the .getCurrentPosition a lot to get the current position of my MediaPlayer. This seems to slow down my UI a lot as I\'m running animations. My question is that instead of calli

I am currently calling the .getCurrentPosition a lot to get the current position of my MediaPlayer. This seems to slow down my UI a lot as I'm running animations. My question is that instead of calling that method over and over...would it be better to use a timer and display that, and just stop/start it on click?

Here is my current code:

private void startTimer() { Thread promoThread = new Thread() {

        @Override
        public void run() {

          while(media[1].isPlaying()){

          Message msg = new Message(); 

          String timer1 =

getTimeString(media[1].getCurrentPosition());

          msg.obj = timer1;

          mHandler.sendMessage(msg); 

          }

        }


    };
    promoThread.start();
      }

and:

protected final Handler mHandler = new Handler() {

  @Override public void handleMessage(Message msg) { 

      String text = (String)msg.obj;

      myText.setText(text);
}
开发者_JAVA百科

};


You don't need to call getPosition all the time to track playing time, instead just fire that timer and update your UI according to timer time. You can put also an OnInfoListener() to MediaPlayer to track end of play and kill timer.

Use runOnUiThread() inside Timer to update UI, no need of doing it with Handler.

I coded such a timer fired each 200mS and updated my counter UI with no problems at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消