How to get songs remaining and total duration time from media player?

问题: friends, I don't know how to get songs remaining and total duration from the media player. In my code, I want to get songs total duration and remaining duration and show in...

问题:

friends, I don't know how to get songs remaining and total duration from the media player. In my code, I want to get songs total duration and remaining duration and show in text view with proper time formatting:

 updateseekbar=new Thread(){

                     @Override public  void run(){
                  int totalduration=mediaPlayer.getDuration();
                  int currnetpostion=0;




                  while (currnetpostion<totalduration){
                      try{
                     sleep(500);
                     currnetpostion=mediaPlayer.getCurrentPosition();
                     seekBar.setProgress(currnetpostion);



                      }catch (Exception e) {
                      e.printStackTrace();
                      }

                  }
            }
        };

What do I need to do in order to achieve this?


回答1:

As i see in your code....

totalduration= the total time of the song;

but if you want to current position of the media player. You have to put your code inside the runnable and update the current position at every second....

 final Handler handler=new Handler();
    handler.post(new Runnable() {
        @Override
        public void run() {
            int duration=mediaplayer.getcurrentPosition();
            //and update your seekbar from handler
            //change your int to time format...
            String time = String.format("%02d:%02d ", TimeUnit.MILLISECONDS.toMinutes(duration), TimeUnit.MILLISECONDS.toSeconds(duration) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)));
             updateseekbar();
            handler.postDelayed(this,1000);
        }
    });

if you are not getting it ask this again


回答2:

You can acheive this by substracting current duration from total duration, In your code I can see you have both Total and current duration.

remainingDuration  = totalduration -   currnetpostion=mediaPlayer.getCurrentPosition();

May be work your case

  • 发表于 2019-03-19 16:54
  • 阅读 ( 182 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除