0

For my alarm app, this error is coming only for Xiaomi Note 3 & Xiaomi Mi4 devices & working fine for all other devices including my personsal Xiaomi Redmi 3S.

Exception java.lang.RuntimeException: Unable to start activity 

java.lang.IllegalStateException :
Caused by java.lang.IllegalStateException:
android.media.MediaPlayer._prepare (MediaPlayer.java)
android.media.MediaPlayer.prepare (MediaPlayer.java:1408)
android.app.Activity.performCreate (Activity.java:6912)
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1126)
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2877)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2985)
android.app.ActivityThread.-wrap14 (ActivityThread.java)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1635)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:154)
android.app.ActivityThread.main (ActivityThread.java:6692)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1468)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1358)
gaurav.28ch
  • 39
  • 2
  • 6

1 Answers1

0

Let's assume that you provide data-source before MediaPlayer.prepare()

Since MediaPlayer.prepare() blocks the main thread, which results in an exception. To prevent this, try MediaPlayer.prepareAsync() instead of MediaPlayer.prepare(). For this apporach read onPreparedListener() and onErrorListener()
If this didn't help, please have a look at this, this and this

isamirkhaan1
  • 749
  • 7
  • 19
  • Any reason why MediaPlayer.prepare() is failing only for Xiaomi & some Samsung Galaxy devices, but not for others ? Also I am only using device media & not doing online buffering. – gaurav.28ch Sep 10 '17 at 17:21
  • In my app, if the user doesn't set a ringtone/alarm/music-file of his choice in settings, then the default ringtone rings. The problem is only coming in the scenarios where the default ringrone rings & user hasn't set a ringtone/alarm/music-file in app settings . – gaurav.28ch Sep 11 '17 at 07:10
  • Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); This code is working fine on all other devices. – gaurav.28ch Sep 11 '17 at 07:15
  • This fix should work. Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); if (uri == null) { uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); if (uri == null) { // alert backup is null, using 2nd backup uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); } } Looks like TYPE_RINGTONE is returning null only for these devices. – gaurav.28ch Sep 11 '17 at 07:16
  • 1
    import a default tune in your assets and in case of `uri = null` set `uri` to that tune. – isamirkhaan1 Sep 11 '17 at 07:46
  • That's a nice solution. From what I have read, default notification should not return null for any device. If still crashes happen for any device, then I would go ahead with your suggestion. – gaurav.28ch Sep 11 '17 at 08:14
  • yeah, it's a good hack. Also remember to accept this answer :) – isamirkhaan1 Sep 11 '17 at 09:44