Use a Timer or MediaPlayer method?
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() {
开发者_JAVA百科@Override public void handleMessage(Message msg) { String text = (String)msg.obj; myText.setText(text); }
};
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.
精彩评论