I am creating an Android app which requires the real time change of playback rate/speed. I have searched all over the internet and have seen many solutions including
Tutorials for OpenSL ES for Android
How to change audio tempo and pitch individuality using ffmpeg?
As i am relatively new to development and android some of these are simply out of my leauge.
Many of these examples talk about Soundpool but this api does not work with large files.
After looking through API's and classes i found the method setPlaybackRate(int sampleRateInHz)
under the AudioTrack class.
http://developer.android.com/reference/android/media/AudioTrack.html#setPlaybackRate(int)
However in all these examples online none of them mention this. Am i missing something and can it be used?
Any help is much appreciated
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));
The setPlaybackRate()
can indeed be used but it's probably not what you are looking for.
Suppose you have an audio stream at 44100. You can use this function to set the playbackrate to 22050 Hz, that would mean that the samples will be fed to the audio output at half the rate, and it will indeed slow down the music. The downside is that the frequency of the audio will also drop to half, because of the same fact that the samples are fed at half the rate.
There's currently no direct Android API to do time-stretching. These algorithms are very heavy math-wise and even if you could code them in Java, the performance would be horrible.
THe best way is to look for an implementation in C++ using JNI, and this would probably include libraries like ffmpeg.