Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed Control of MediaPlayer in Android

Tags:

android

I am developing a player app and I am using MediaPlayer for that.

Now, I want to change the speed of the playing track.

I've seen so many apps with this functionality. How can I do this?

like image 618
Krishna Suthar Avatar asked Jun 01 '12 12:06

Krishna Suthar


People also ask

Which video player can change speed?

The VLC video player allows you to speed up and slow down playback of movies with the >> and << buttons.

Which video player has playback speed control?

No. 4: Clideo - Online Video Speed Controller. The online video speed controller is very convenient to use on different platforms including Android, iOS, Windows and macOS at will without downloading any software.


2 Answers

Beginning API 23, MediaPlayer can set playback speed using this method.

Class MediaPlayer

public void setPlaybackParams (PlaybackParams params) Added in API level 23

Sets playback rate using PlaybackParams. Parameters params PlaybackParams: the playback params. Throws IllegalStateException if the internal player engine has not been initialized. IllegalArgumentException if params is not supported.

Sample code:

MediaPlayer mp = ...; //Whatever float speed = 0.75f;      mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(speed)); 

For API < 23, refer to Vipul Shah's answer above (or below).

like image 161
Shishir Gupta Avatar answered Oct 02 '22 18:10

Shishir Gupta


The MediaPlayer does not provide this feature but SoundPool has this functionality. The SoundPool class has a method called setRate (int streamID, float rate). If you are interested in the API have a look here.

This Snippet will work.

 float playbackSpeed=1.5f; 
 SoundPool soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);

 soundId = soundPool.load(Environment.getExternalStorageDirectory()
                         + "/sample.3gp", 1);
 AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
 final float volume = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

 soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener()
 {
     @Override
     public void onLoadComplete(SoundPool arg0, int arg1, int arg2)
     {
         soundPool.play(soundId, volume, volume, 1, 0, playbackSpeed);
     }
 });
like image 30
Vipul Avatar answered Oct 02 '22 17:10

Vipul