Android MediaPlayer getCurrentPosition () causes a stutter sound

I use SeekBar to display the progress of the audio file and search at a specific time. For the update, I use Runnable, which calls getCurrentPosition () on the MediaPlayer every second or so. Every time this happens, there is a small amount of delay in the sound. Since I call it often, I get a very noticeable stutter during the game. If relevant, I use setAudioStreamType (AudioManager.STREAM_MUSIC) and the file format is mp4 with AAC audio (no video), and I use Android 2.3.4. Is there a way to get good sound using getCurrentPosition (), or do I need to do my own progress calculations?

Runnable:

private Runnable mUpdateTask = new Runnable(){ @Override public void run() { mSeekBar.setProgress((int) (mPlayer.getCurrentPosition() * 100 / mArrayAdapter.getRecording(mPlayingId).mDuration)); mHandler.postDelayed(mUpdateTask, 999); } }; 
+8
android audio media-player
source share
3 answers

I use this method to calculate progress

 public static int getProgressPercentage(long currentDuration, long totalDuration) { Double percentage = (double) 0; long currentSeconds = (int) (currentDuration / 1000); long totalSeconds = (int) (totalDuration / 1000); // calculating percentage percentage = (((double) currentSeconds) / totalSeconds) * 100; // return percentage return percentage.intValue(); } 

Note: mPlayer.getCurrentPosition () is inaccurate. Some bugs reported. I had a problem that the current position was higher than totalDuration.

-one
source share

you can do something like this:

 private Runnable mUpdateTask = new Runnable(){ @Override public void run() { mSeekBar.setProgress(mMediapPlayer.getCurrentPosition()); mHandler.postDelayed(mUpdateTask, 999); } }; 

you can also use the listener for changes in the progress of the search bar as follows:

 mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) { if (fromUser) { int secProgress = seekBar.getSecondaryProgress(); if (secProgress> progress) { mMediapPlayer.seekTo(progress); } else { seekBar.setProgress(mSeekBar.getProgress()); } } } }); mMediapPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() { @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { mSeekBar.setSecondaryProgress((mSeekBar.getMax()/100)*percent); } }); 
+2
source share

I had the same problem or something similar.

When I used mMediapPlayer.getCurrentPosition() in TimerTask to update the SeekBar, I heard sound problems like an echo, but actually the problem was not there.

The problem is that I also used the SeekBar OnSeekBarChangeListener for manual searching, but the update seekBar from TimerTask also caused a listener that made mp.seekTo(progress) , and this caused mp to return to this position again ..

I fixed it using the fromUser argument, as suggested here, to search only if seekBar changed manually.

Here is my sample code:

TimerTask:

 public void initializeTimerTask() { mTimerTask = new TimerTask() { public void run() { int progress = mp.getCurrentPosition()/1000; runOnUiThread(new Runnable() { @Override public void run() { mSeekBar.setProgress(progress); tvDuration.setText(DateUtils.formatElapsedTime(progress)); } }); } }; } 

LISTENER:

  mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if(mp != null && fromUser){ mp.seekTo(progress * 1000); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); 
0
source share

All Articles