Android Audio SeekBar

前端 未结 2 1688
暖寄归人
暖寄归人 2021-01-02 11:05

I\'m trying to create something where a single audio file is played and can be paused and manipulated with a progress/seek bar. I want to have an image taking up most of the

2条回答
  •  被撕碎了的回忆
    2021-01-02 11:34

    I think this can be work for you.

    Create one thread that will check song position and will move seekbar like this,

    private Runnable moveSeekBarThread = new Runnable() {
    
        public void run() {
            if(mediaPlayer.isplaying){
    
            int mediaPos_new = mediaPlayer.getCurrentPosition();
            int mediaMax_new = mediaPlayer.getDuration();
            seekBar.setMax(mediaMax_new);
            seekBar.setProgress(mediaPos_new);
    
            handler.postDelayed(this, 100); //Looping the thread after 0.1 second
                                            // seconds
            }  
        }
    };
    

    And call that thread like this,

      mediaPos = mediaPlayer.getCurrentPosition();
      mediaMax = mediaPlayer.getDuration();
    
      seekBar.setMax(mediaMax); // Set the Maximum range of the
      seekBar.setProgress(mediaPos);// set current progress to song's
    
      handler.removeCallbacks(moveSeekBarThread);
      handler.postDelayed(moveSeekBarThread, 100);
    

提交回复
热议问题