SeekBar and media player in android

前端 未结 12 1988
天涯浪人
天涯浪人 2020-11-28 03:13

I have a simple player and recorder. Everything works great but have a one problem. I want to add seek bar to see progress in playing record and use this seek bar to set pl

相关标签:
12条回答
  • 2020-11-28 03:48

    This works for me:

    seekbarPlayer.setMax(mp.getDuration());
    getActivity().runOnUiThread(new Runnable() {
    
        @Override
        public void run() {
            if(mp != null){
                seekbarPlayer.setProgress(mp.getCurrentPosition());
            }
            mHandler.postDelayed(this, 1000);
        }
    });
    
    0 讨论(0)
  • 2020-11-28 03:52

    Try this Code:

    public class MainActivity extends AppCompatActivity {
    
    MediaPlayer mplayer;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
                //You create MediaPlayer variable ==> set the path and start the audio.
    
        mplayer = MediaPlayer.create(this, R.raw.example);
        mplayer.start();
    
                //Find the seek bar by Id (which you have to create in layout)
                // Set seekBar max with length of audio
               // You need a Timer variable to set progress with position of audio
    
        final SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
        seekBar.setMax(mplayer.getDuration());
    
        new Timer().scheduleAtFixedRate(new TimerTask() {
                    @Override
                    public void run() {
                        seekBar.setProgress(mplayer.getCurrentPosition());
                    }
                }, 0, 1000);
    
    
                seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    
                // Update the progress depending on seek bar
                        mplayer.seekTo(progress);
    
                    }
    
                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
    
                    }
    
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
    
                    }
                });
            }
    
    0 讨论(0)
  • 2020-11-28 03:55

    To add on to @hardartcore's answer.

    1. Instead of calling postDelayed on a Handler, the best approach would be to get callbacks from the MediaPlayer during play-back and then accordingly update the seekBar with the progress.

    2. Also, pause your MediaPlayer at onStartTrackingTouch(SeekBar seekBar) of the OnSeekBarChangeListener and then re-start it on onStopTrackingTouch(SeekBar seekBar).

    0 讨论(0)
  • 2020-11-28 04:01

    Code in Kotlin:

    var updateSongTime = object : Runnable {
                override fun run() {
                    val getCurrent = mediaPlayer?.currentPosition
                    startTimeText?.setText(String.format("%d:%d",
                            TimeUnit.MILLISECONDS.toMinutes(getCurrent?.toLong() as Long),
                            TimeUnit.MILLISECONDS.toSeconds(getCurrent?.toLong()) -
                                    TimeUnit.MINUTES.toSeconds(
                                            TimeUnit.MILLISECONDS.toMinutes(getCurrent?.toLong()))))
                    seekBar?.setProgress(getCurrent?.toInt() as Int)
                    Handler().postDelayed(this, 1000)
                }
            }
    

    For changing media player audio file every second

    If user drags the seek bar then following code snippet can be use

    Statified.seekBar?.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
                override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
                    if(b && Statified.mediaPlayer != null){
                        Statified.mediaPlayer?.seekTo(i)
                    }
    
                }
                override fun onStartTrackingTouch(seekBar: SeekBar) {}
                override fun onStopTrackingTouch(seekBar: SeekBar) {}
            })
    
    0 讨论(0)
  • 2020-11-28 04:01

    Based on previous statements, for better performance, you can also add an if condition

    if (player.isPlaying() {
        handler.postDelayed(..., 1000);
    }
    
    0 讨论(0)
  • 2020-11-28 04:02

    Given the answer hardartcore that worked for me with a small change and did not work before the change:

    private Handler mHandler = new Handler();
    MusicPlayer.this.runOnUiThread(new Runnable() {
         @Override
         public void run() {
              if(player != null){
                   int mCurrentPosition = player.getCurrentPosition();//clear ' /1000 '
                   seekBar.setProgress(mCurrentPosition);
              }
              mHandler.postDelayed(this, 1000);
          }
    });
    
    
    
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
    
        }
    
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
    
        }
    
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
              if(player != null && fromUser){
                  player.seekTo(progress); // clear ' * 1000  '
              }
         }
    });
    
    0 讨论(0)
提交回复
热议问题