MediaPlayer error setDataSource with status = 0x80000000 for Ringtone installed in file path 2.3.4

The name says it.

My application plays ringtones that uri points to, for example content://media/internal/audio/media/387 or content://media/external/audio/media/1655 (for custom tunes on an SDcard, which I suppose ) using both setDataSource(fileInfo) and setDataSource(mContext, Uri.parse(fileInfo)) .

In each case, I received logs with setDataSource failed.: status=0x80000000 exception setDataSource failed.: status=0x80000000 on phones using Android 4.x (different versions).

Having seen that the error only occurs with the ringtones specified in the uri content, but not with the single files specified in the path, I decided to use also the paths for the ringtones that fixed the problem on the above phones (when using setDataSource(mContext, Uri.parse(fileInfo)) )

However, he was having problems with phones with Android 2.3.4-2.3.6 (not on my 2.3.3):

  • I received several logs with the exception: setDataSource failed.: status=0x80000000 for files with paths such as /system/media/audio/ringtones/TwirlAway.ogg
  • I also got a log about calling the MediaPlayer.onErrorListener.onError(int what, int extra) method with what=1 and extra=-2147483648 , which, as I know, suggests either this file is missing or damaged. However i do

     File file = new File(fileInfo); if (!file.exists()) 

check in this situation, and he returned that the file exists - is it damaged? Very unlikely for a music file in internal memory.

Summarizing:

  • works with setDataSource("content://media/internal/audio/media/52")
  • setDataSource failed.: status=0x80000000 exception: setDataSource failed.: status=0x80000000 for setDataSource(mContext, "/system/media/audio/ringtones/TwirlAway.ogg")

Interestingly, the first few lines of the setDataSource(Context context, Uri uri, Headers headers) method setDataSource(Context context, Uri uri, Headers headers) , which is called setDataSource(Context context, Uri uri) , are ( from GrepCode source for 2.3.4 ):

  String scheme = uri.getScheme(); if(scheme == null || scheme.equals("file")) { setDataSource(uri.getPath()); return; } 

So, in the end, it just fails for setDataSource("/system/media/audio/ringtones/TwirlAway.ogg") . I took the paths to ringtones from uris using:

 private 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(); return ringtoneCursor.getString(ringtoneCursor .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); } 

Any ideas that might throw error throwing? Perhaps these are some problems caused by a lack of read permissions? I think the source code for the setDataSource (String path) built-in function would help a lot, but I could not find it.

+16
android android-mediaplayer ringtone
May 6 '13 at 9:17
source share
5 answers

Lorne's answer below was most helpful in solving this problem.

For someone who is struggling with this, here is the code that I have been using for more than 6 months with errors that are almost not reported anymore.

fileinfo can be as 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 + "/" + 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; } 
+9
Nov 13 '14 at 9:32
source share

setDataSource(String path) behavior setDataSource(String path) due to an error in Android 4.1.1. In 4.1.1 or later, you need to use the local path (no protocol). However, in 4.0.4 and earlier, you had to use a URI (for example, with the file: // protocol).

Here is an incomplete piece of code that should illustrate a workaround:

 // as of 4.1.1 (JELLY_BEAN) we need to use a local path (without protocol) // on 4.0.4 and earlier we needed a URI (with file:// protocol) final String cachedFile = android.os.Build.VERSION.SDK_INT >= 16 // android.os.Build.VERSION_CODES.JELLY_BEAN ? getCacheFilePath(file) : getCacheFileUri(file); // for the purpose of this example // assume cacheFolder is a String and getCacheFile returns a String public String getCacheFilePath(String file) { return cacheFolder + getCacheFile(file); } public String getCacheFileUri(String file) { return "file://" + cacheFolder + getCacheFile(file); } 
+4
Mar 03 '14 at 16:58
source share

You must specify the length of your file. Use the overloaded method:
AssetFileDescriptor afd = ctx.getAssets().openFd([your asset name]); mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

+1
Apr 15 '16 at 21:44
source share

I had the same error when I tried to play a WAV file. Let's make an example:

  private void start_player(){ // create raw folder inside the res folder if not present MediaPlayer mPlayer = MediaPlayer.create(activity, R.raw.your_wave_audio_file); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mp.release(); // Your Stuff } }); mPlayer.start(); } } 

I also got the status = 0x80000000. In my case, the solution was to re-encode the audio file (for example, in a 16-BIT PCM for a WAV file), and everything worked as expected.

0
May 05 '14 at 17:40
source share
 MediaPlayer mPlayer = MediaPlayer.create(activity, R.raw.your_wave_audio_file); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 

You cannot do this because preparation was called in the create function, so you cannot change the type of audio stream after.

Below code works fine for me:

 sMediaPlayer = new MediaPlayer(); sMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING); AssetFileDescriptor assetFileDescriptor = context.getResources(). openRawResourceFd(R.raw.cool_song); if(assetFileDescriptor == null) return; try { sMediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength()); assetFileDescriptor.close(); sMediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { if(sMediaPlayer != null){ sMediaPlayer.release(); sMediaPlayer = null; } } }); sMediaPlayer.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { sMediaPlayer.start(); } }); sMediaPlayer.prepare(); } catch (IllegalArgumentException e) { HelpFunctions.showLog("ERROR = " + e); } catch (IllegalStateException e) { HelpFunctions.showLog("ERROR = " + e); } catch (IOException e) { HelpFunctions.showLog("ERROR = " + e); } 
0
Nov 12 '14 at 2:50
source share



All Articles