MediaPlayer setDataSource failed with status=0x80000000 for Ringtone set by filepath on 2.3.4

Answer by Lorne below was most helpful when dealing with this problem.

For anyone else struggling with it, here is the code that I have been using for over 6 months now with errors almost not reported anymore.

fileinfo can be both of below (examples):

/system/media/audio/alarms/Walk_in_the_forest.ogg

content://media/internal/audio/media/20

public static void setMediaPlayerDataSource(Context context,
        MediaPlayer mp, String fileInfo) throws Exception {

    if (fileInfo.startsWith("content://")) {
        try {
            Uri uri = Uri.parse(fileInfo);
            fileInfo = getRingtonePathFromContentUri(context, uri);
        } catch (Exception e) {
        }
    }

    try {
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
            try {
                setMediaPlayerDataSourcePreHoneyComb(context, mp, fileInfo);
            } catch (Exception e) {
                setMediaPlayerDataSourcePostHoneyComb(context, mp, fileInfo);
            }
        else
            setMediaPlayerDataSourcePostHoneyComb(context, mp, fileInfo);

    } catch (Exception e) {
        try {
            setMediaPlayerDataSourceUsingFileDescriptor(context, mp,
                    fileInfo);
        } catch (Exception ee) {
            String uri = getRingtoneUriFromPath(context, fileInfo);
            mp.reset();
            mp.setDataSource(uri);
        }
    }
}

private static void setMediaPlayerDataSourcePreHoneyComb(Context context,
        MediaPlayer mp, String fileInfo) throws Exception {
    mp.reset();
    mp.setDataSource(fileInfo);
}

private static void setMediaPlayerDataSourcePostHoneyComb(Context context,
        MediaPlayer mp, String fileInfo) throws Exception {
    mp.reset();
    mp.setDataSource(context, Uri.parse(Uri.encode(fileInfo)));
}

private static void setMediaPlayerDataSourceUsingFileDescriptor(
        Context context, MediaPlayer mp, String fileInfo) throws Exception {
    File file = new File(fileInfo);
    FileInputStream inputStream = new FileInputStream(file);
    mp.reset();
    mp.setDataSource(inputStream.getFD());
    inputStream.close();
}

private static String getRingtoneUriFromPath(Context context, String path) {
    Uri ringtonesUri = MediaStore.Audio.Media.getContentUriForPath(path);
    Cursor ringtoneCursor = context.getContentResolver().query(
            ringtonesUri, null,
            MediaStore.Audio.Media.DATA + "='" + path + "'", null, null);
    ringtoneCursor.moveToFirst();

    long id = ringtoneCursor.getLong(ringtoneCursor
            .getColumnIndex(MediaStore.Audio.Media._ID));
    ringtoneCursor.close();

    if (!ringtonesUri.toString().endsWith(String.valueOf(id))) {
        return ringtonesUri + "https://stackoverflow.com/" + id;
    }
    return ringtonesUri.toString();
}

public static String getRingtonePathFromContentUri(Context context,
        Uri contentUri) {
    String[] proj = { MediaStore.Audio.Media.DATA };
    Cursor ringtoneCursor = context.getContentResolver().query(contentUri,
            proj, null, null, null);
    ringtoneCursor.moveToFirst();

    String path = ringtoneCursor.getString(ringtoneCursor
            .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));

    ringtoneCursor.close();
    return path;
}

Leave a Comment