0

I upgraded my old nexus device to the latest and now MediaPlayer wont play custom ringtone paths that im retrieving by way of the ringtoneManager picker.

i have two media's as an example:

  1. content://media/internal/audio/media/86 --this one does not play, its a custom ringtone ( a mp3 i downloaded and added to /media/audio/ringtones/)

and

  1. content://media/internal/audio/media/54 --this one plays

im trying to play both with the mediaplayer API in android. Both media paths were returned to me by the RingtoneManager as follows:

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);

        // activity stack history, its a one time deal only
    //  intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,
                "Please Select A  Ringtone");


        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALL);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
        //intent.putExtra(RingtoneManager.EXTRA_RINGTONE_INCLUDE_DRM, true);


        try {// 44 arbitrary number to recognize our intent
            startActivityForResult(intent, 44);
}
//etc

and here is how i retrieve the selected ringtone. Everything works except when the user picks a custom ringtone from the list, i get a mediaplayer IO exception that the data resource cannot be played.

// find out what ringtone the user selected and play the tone.
protected  void onActivityResult(int requestCode, int resultCode, Intent data) {
    Ringtone ringtone;

    if (requestCode == 44 && resultCode == RESULT_OK) {
        Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

}

//the uri is retrieved and its value is: content://media/internal/audio/media/86

here is the mediaplayer error i receive when i try ti play this custom ringtone :

  e = {java.io.IOException@4499} "java.io.IOException: setDataSource failed.: status=0x80000000"
 cause = {java.io.IOException@4499} "java.io.IOException: setDataSource failed.: status=0x80000000"
 detailMessage = {java.lang.String@4503} "setDataSource failed.: status=0x80000000"

any other media works fine just not custom ringtones.

j2emanue
  • 60,549
  • 65
  • 286
  • 456

2 Answers2

0

looks like the path is not accepted and i have to use the full path to get to the custom ringtone for some reason. Google, is there any reason why ? here is my workaround. check if the uri is valid with isValidUri. if its not valid we call getRingtonePathFromContentUri to get the directory path for the resource and use it instead. The issue is reported here

 /**
* checks if a url such as content://media/internal/audio/media/86 can be played.
 * if not returns no and we can fall back to something else.
**/
    public boolean isValidUri(String contentUri){

        boolean result=true;

        MediaPlayer player = new MediaPlayer();
        try {
            player.setDataSource(contentUri);
        } catch (IOException e) {
            e.printStackTrace();
            result = false;
        }

        return result;
    }

    //gets the SD card path for a ringtone uri
    public  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;
    }
Community
  • 1
  • 1
j2emanue
  • 60,549
  • 65
  • 286
  • 456
-1

You could put your mp3 files at res/raw folder:

MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.myringtone);
mediaPlayer.start();
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53