34

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
Krishna Suthar
  • 3,071
  • 6
  • 31
  • 37
  • Does this answer your question? [Control the playback speed of Android MediaPlayer](https://stackoverflow.com/questions/4406169/control-the-playback-speed-of-android-mediaplayer) – Jaswant Singh Jan 20 '22 at 15:25

7 Answers7

57

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).

Community
  • 1
  • 1
Shishir Gupta
  • 1,512
  • 2
  • 17
  • 32
  • 6
    For those of you who are getting an IllegalStateException while calling the 'setPlayParams' method, make sure you're not doing PlaybackParams params = mp.getPlaybackParams(), set the speed and then pass it to mp.setPlaybackParams()! Set the speed DIRECTLY while calling the mp.getPlayParams()! Just like in Shishir Gupta's answer! – DankMemester 'Andrew Servania' Jul 22 '17 at 17:40
19

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);
     }
 });
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Vipul
  • 27,808
  • 7
  • 60
  • 75
  • No MediaPlayer does not provide this functionality. – Vipul Jun 01 '12 at 12:28
  • 3
    If I get it correctly it means pitch will be also changed? Is there method to adjust speed without changing pitch? – AlexKost Dec 20 '16 at 14:24
  • SoundPool is only good for about 5.5 seconds of playback. For Lollipop and earlier, I just don't provide the option for speed control. And, yeah, I just got a band new tablet that happened to be Lollipop. – Philip Young Jul 01 '21 at 16:54
17

soundpool only supports relatively small sound effect files that can be preloaded. You will get heap overflows with any useful length music track.

Bob
  • 171
  • 1
  • 2
10

Now you can use

mediaPlayer.setPlaybackParams(mediaPlayer.getPlaybackParams().setSpeed(speed))

for API 23 and up!

Gumby The Green
  • 603
  • 7
  • 10
Wallace Zhen
  • 175
  • 1
  • 2
  • 9
    Please note if you want to promote your own product/blog you **must disclose your affiliation in the answer**, otherwise your answer may be flagged as spam. Please read [How to not be a spammer](http://stackoverflow.com/help/promotion) – CalvT Jul 04 '17 at 21:35
  • 2
    Also, this answer does already seem to be covered in @ShishirGupta's answer above – CalvT Jul 04 '17 at 23:41
2

The MediaPlayer class doesn't give this functionality. Instead use the SoundPool class. It has a method called setRate (int streamID, float rate). Read this for further info. Here is a sample code for you to work with it.

Community
  • 1
  • 1
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
  • Yeah. Then Can I set other functionalities like next track, previous track, sound control, forward, backward, sleep, repeat, shuffle in SoundPool? Please give me some idea. – Krishna Suthar Jun 01 '12 at 12:21
  • You can search for tutorials on the [Google](http://www.google.com). you will find many examples for this. – Kazekage Gaara Jun 01 '12 at 12:26
2

According to the flow that had mentioned here following codes are for android API 23 (Android 6.0 Marshmallow)

PlaybackParams playbackParams = new PlaybackParams();
playbackParams.setSpeed(2);
playbackParams.setPitch(1);
playbackParams.setAudioFallbackMode(
    PlaybackParams.AUDIO_FALLBACK_MODE_DEFAULT);
mMediaPlayer.setPlaybackParams(playbackParams);
Shailendra Yadav
  • 1,822
  • 1
  • 12
  • 16
  • 2
    Add some explanation to your answer . Code only answer are not really useful . – ADM Apr 20 '18 at 14:25
2

Speed Control by ExoPlayer in Android

//exoPlayer.setPlaybackParameters(PlaybackParameters(//speed in float))
//EXAMPLE->
exoPlayer.setPlaybackParameters(PlaybackParameters(1.5f))
Mohd Danish
  • 186
  • 2
  • 3