When I try changing the playback params of a MediaPlayer my app crashes. I am not sure why this is happening.
Here is the code I am using to change the playback speed.
PlaybackParams params = new PlaybackParams().allowDefaults();
params.setSpeed(mPlaybackRate.getValue());
mMediaPlayer.setPlaybackParams(params);
In this case mPlaybackRate.getValue() returns 1.25f
This is the crash log:
08-02 21:09:35.184 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hackmodford.bigfinish, PID: 30986
java.lang.IllegalStateException
at android.media.MediaPlayer.setPlaybackParams(Native Method)
at com.hackmodford.bigfinish.mediaPlayerService.MediaPlayerService.incrementPlaybackRate(MediaPlayerService.java:543)
at com.hackmodford.bigfinish.mediaPlayerService.MediaPlayerService.access$1000(MediaPlayerService.java:47)
at com.hackmodford.bigfinish.mediaPlayerService.MediaPlayerService$3.onCustomAction(MediaPlayerService.java:336)
at android.support.v4.media.session.MediaSessionCompat$Callback$StubApi21.onCustomAction(MediaSessionCompat.java:832)
at android.support.v4.media.session.MediaSessionCompatApi21$CallbackProxy.onCustomAction(MediaSessionCompatApi21.java:231)
at android.media.session.MediaSession$CallbackMessageHandler.handleMessage(MediaSession.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
you have to ensure your mediaPlayer is prepared! so you can try set a listener like this:
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
public void onPrepared(MediaPlayer var1) {
PlaybackParams params = mMediaPlayer.getPlaybackParams();
params.setSpeed(3.0f);
mMediaPlayer.setPlaybackParams(params);
}
});
it works for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With