Monday, January 21, 2013

MediaPlayer prepare failed. status=0x1 or other issues related to MediaPlayer in Android

If you are working with Android's MediaPlayer you must have seen error popping up like this so randomly and it's quite frustrating .

After digging in I figure out the easy way to diagnose issues related to this.

First thing you should do is hook up setOnErrorListener

because it gives you a correct error codes related to the error.


mediaPlayer.setOnErrorListener(new OnErrorListener() {
    public boolean onError(MediaPlayer mp, int what, int extra) {
        onPlaybackError();
        return true;
    }
});

Eg,


E/MediaPlayer(): error (1, -17)

When it comes to error codes, Android documentation sucks. You have to go to the source code to look after the error codes to find out whats wrong

Error codes are located here 

In this case it was

const PVMFStatus PVMFErrResource = (-17);

Error due to general error in underlying resource

After some more digging i found out the reason for this error.

Android has a total of 8 instances of the player to share among all apps.

 You are likely to get this error if you have ran out. so watch out!!

Tuesday, January 15, 2013

How to check whether a content provider is installed ?

Today, I had to research on the calender to read the events from the native Android calender. Problem is different version of Android uses different content providers. So, how to find if calendar content provider exists ?


private boolean isCalenderExisit(Activity activity) {
boolean isSuccess = false;
ContentProviderClient calendarClient = activity.getContentResolver().acquireContentProviderClient(
getContentURI());
ContentProviderClient eventClient = activity.getContentResolver().acquireContentProviderClient(
getContentURI());
if (eventClient == null || calendarClient == null) {
isSuccess = false;
}
else {
calendarClient.release();
eventClient.release();
isSuccess = true;
}

return isSuccess;
}
public static Uri getContentURI() {
final Uri CONTENT_URI_PRE_8 = Uri.parse("content://calendar/calendars");
final Uri CONTENT_URI = Uri.parse("content://com.android.calendar/calendars");
if(Build.VERSION.SDK_INT <= 7) {
return CONTENT_URI_PRE_8;
} else {
return CONTENT_URI;
}
}