Mediaplayer progress update to seekbar not smooth?

前端 未结 6 2078
情话喂你
情话喂你 2021-02-01 07:00

I am working on an app with recorder and player. I am using mediaplayer to play the recorded .wav file and meantime I want to update to a seekbar. Everything is working fine But

6条回答
  •  耶瑟儿~
    2021-02-01 07:41

    private void startPlaying() {
            mediaPlayer = new MediaPlayer();
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(audioPlayerName);
                mediaPlayer.prepare();
                mediaPlayer.start();
    
                setAudioProgress(); //call method
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    
    public void setAudioProgress() {
        total_duration = mediaPlayer.getDuration();
    
        binding.total.setText(timeConversion((long) total_duration));
        binding.current.setText(timeConversion((long) current_pos));
        binding.seekbar.setMax((int) total_duration);
    
        runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    current_pos = mediaPlayer.getCurrentPosition();
                    binding.current.setText(timeConversion((long) current_pos));
                    binding.seekbar.setProgress((int) current_pos);
                    handlerProgressBar.postDelayed(this, 1000);
    
                    Log.e(LOG_TAG, "11111");
                } catch (IllegalStateException ed) {
                    ed.printStackTrace();
                }
            }
        };
        handlerProgressBar.postDelayed(runnable, 1000);
    }
    

提交回复
热议问题