Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaPlayer setPlaybackParams crash

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)
like image 546
Hackmodford Avatar asked Aug 03 '16 01:08

Hackmodford


Video Answer


1 Answers

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.

like image 115
LONG Avatar answered Sep 29 '22 15:09

LONG