SoulRayder SoulRayder - 1 month ago 17
Android Question

How to play a notification sound programatically using the URI without setting it to default?

I am using the following code to get the list of notification sounds available on the android device:

public static ArrayList<String> getNotificationSounds(Activity curActivity) {
RingtoneManager manager = new RingtoneManager(curActivity);
manager.setType(RingtoneManager.TYPE_NOTIFICATION);
Cursor cursor = manager.getCursor();

ArrayList<String> list = new ArrayList<>();
while (cursor.moveToNext()) {
String id = cursor.getString(RingtoneManager.ID_COLUMN_INDEX);
String uri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);
String name = cursor.getString((RingtoneManager.TITLE_COLUMN_INDEX));

list.add(name);
}

return list;
}


Now I want to play one of these notification sounds (no image, or title), just the sound without setting it as default.

I have seen many solutions which suggest setting a sound as default using

RingtoneManager.setActualDefaultRingtoneUri(this,RingtoneManager.TYPE_NOTIFICATION,soundClipUri);


By using this URI that I obtain from the method described above

String uri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);


I get an error saying that datasource is not valid.

So , to sum it up,

1) How to play a notification sound without setting it as default?

2) How to obtain and use the Uri of that notification sound?

EDIT: When I tried to print the URI from the cursor query mentioned above, I get the same result:

content://media/internal/audio/media


for all of the available notification sounds.

Are we supposed to append the notification sound title to this and pass the result as the Uri?

Code to play the notification:

Ringtone r = RingtoneManager.getRingtone(getApplicationContext(),uri);
r.play();

Answer

Found a solution to my problem.

I need to append the notification sound's ID to the URI returned by that method so that the internal notification sound can be properly located.

Here is the solution, I get the notification sound's URI and play it using the following code:

 try 
     {
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), PhoneUtils.getNotificationSoundUri(getApplicationContext(), getSoundClipID()));
        r.play();
     }
     catch (Exception e)
     {
        e.printStackTrace();
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), getSoundClipID()));
       r.play();

     }

Code for the getNotification sound URI method:

public static Uri getNotificationSoundUri(Context context,String soundClipId)
{
    RingtoneManager manager = new RingtoneManager(context);
    manager.setType(RingtoneManager.TYPE_NOTIFICATION);
    Cursor cursor = manager.getCursor();

    ArrayList<NotificationSound> list = new ArrayList<>();
    while (cursor.moveToNext()) {
        if(soundClipId.equals(cursor.getString(RingtoneManager.ID_COLUMN_INDEX)))
        {
            return Uri.parse(cursor.getString(RingtoneManager.URI_COLUMN_INDEX)+"/"+cursor.getString(RingtoneManager.ID_COLUMN_INDEX));
        }

    }

    return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}