'Speed Control of MediaPlayer in 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?
Solution 1:[1]
Beginning API 23, MediaPlayer can set playback speed using this method.
Class MediaPlayer
public void setPlaybackParams (PlaybackParams params)Added in API level 23Sets 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).
Solution 2:[2]
soundpool only supports relatively small sound effect files that can be preloaded. You will get heap overflows with any useful length music track.
Solution 3:[3]
Now you can use
mediaPlayer.setPlaybackParams(mediaPlayer.getPlaybackParams().setSpeed(speed))
for API 23 and up!
Solution 4:[4]
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.
Solution 5:[5]
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);
Solution 6:[6]
Speed Control by ExoPlayer in Android
//exoPlayer.setPlaybackParameters(PlaybackParameters(//speed in float))
//EXAMPLE->
exoPlayer.setPlaybackParameters(PlaybackParameters(1.5f))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | Bob |
| Solution 3 | Gumby The Green |
| Solution 4 | Community |
| Solution 5 | |
| Solution 6 | Mohd Danish |
