Search code examples
androidaudio-playermediacontrollerandroid-music-player

Cannot cast MediaPlayerController to music player - getting exception


Hello I've tried to use media player controller in my music player app, but when I use musicplayer.setMediaPlayer I got an exception. Here is my snippet of the code and logcat error.

musicPlayer = MediaPlayer.create(MusicPlayer.this, uri);
        musicPlayer.setWakeMode(this.getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
        MediaController musicControl = new MediaController(this);
        musicControl.show();
        musicControl.setMediaPlayer((MediaPlayerControl) musicPlayer);
        musicPlayer.start();

and this is logcat

Process: com.tproductions.Openit, PID: 11382
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tproductions.Openit/com.tproductions.Openit.MusicPlayer}: java.lang.ClassCastException: android.media.MediaPlayer cannot be cast to android.widget.MediaController$MediaPlayerControl
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:212)
        at android.app.ActivityThread.main(ActivityThread.java:5135)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: android.media.MediaPlayer cannot be cast to android.widget.MediaController$MediaPlayerControl
        at com.tproductions.Openit.MusicPlayer.onCreate(MusicPlayer.java:49)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

any ideas how can I solve this ? Many thanks


Solution

  • The problem is MediaPlayer doesn't implement MediaController.MediaPlayerControl.

    You have two choices that I can think of...

    First choice is to extend MediaPlayer and implement the interface. Example...

    public class MyMediaPlayer extends MediaPlayer
        implements MediaController.MediaPlayerControl {
    
        ...
    
    }
    

    ...that would allow you to use an instance of MyMediaPlayer in the call to musicControl.setMediaPlayer(...).

    Alternatively, just implement the interface on your Activity. Example...

    public class MyActivity extends Activity
        implements MediaController.MediaPlayerControl {
    
        ...
    
    }
    

    When you call musicControl.setMediaPlayer(...) in your Activity you would simply pass this as the parameter.