Getting a list of available Ringtones in Android
Asked Answered
P

2

15

I've seen plenty of examples of how to set a default ringtone, but what I'm more interested in is being able populate a drop down box list filled with the available ringtones on the phone. So the list that people see when they change their ringtone in the android settings, I want to be able to list all of those.

The closest thing I've found is here, but again this is just for setting the default ringtone. Any ideas anyone? It can be in or out of ringtonemanager.

Psoas answered 22/8, 2011 at 16:55 Comment(1)
Please choose an answerGuildsman
G
27

This will return you the title and uri of all the ringtones available. Do with them what you wish!

public Map<String, String> getNotifications() {
    RingtoneManager manager = new RingtoneManager(this);
    manager.setType(RingtoneManager.TYPE_RINGTONE);
    Cursor cursor = manager.getCursor();

    Map<String, String> list = new HashMap<>();
    while (cursor.moveToNext()) {
        String notificationTitle = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
        String notificationUri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX) + "/" + cursor.getString(RingtoneManager.ID_COLUMN_INDEX);

        list.put(notificationTitle, notificationUri);
    }

    return list;
}
Guildsman answered 17/4, 2015 at 11:21 Comment(3)
To get correct ringtone uri you need to add a value of cursor.getString(RingtoneManager.ID_COLUMN_INDEX)). So it will be Uri uri = Uri.parse(notificationUri + "/" + cursor.getString(RingtoneManager.ID_COLUMN_INDEX)). After that you can get a ringtone by RingtoneManager.getRingtone(context, uri) and play it, for example.Virgy
@Virgy Thank you. You saved my day!Drolet
how to get title/name of ringtoneAffair
E
16

RingtoneManager is what you are looking for. You just need to use setType to set TYPE_RINGTONE and then iterate over the Cursor provided by getCursor.

This is a working example of an hypothetical method that returns an array of URIs, with the only slight difference that it's looking for alarms instead of ringtones:

RingtoneManager ringtoneMgr = new RingtoneManager(this);
ringtoneMgr.setType(RingtoneManager.TYPE_ALARM);
Cursor alarmsCursor = ringtoneMgr.getCursor();
int alarmsCount = alarmsCursor.getCount();
if (alarmsCount == 0 && !alarmsCursor.moveToFirst()) {
    return null;
}
Uri[] alarms = new Uri[alarmsCount];
while(!alarmsCursor.isAfterLast() && alarmsCursor.moveToNext()) {
    int currentPosition = alarmsCursor.getPosition();
    alarms[currentPosition] = ringtoneMgr.getRingtoneUri(currentPosition);
}
alarmsCursor.close();
return alarms;
Emboly answered 25/9, 2012 at 16:39 Comment(6)
I got a StaleDataException (Attempted to access a cursor after it has been closed.) when putting the phone to sleep and waking it up again after using this code. Not closing the cursor seems to fix it — I suppose RingtoneManager returns a managed cursor?Roney
@eWolf The getCursor docs state: The returned cursor will be the same cursor returned each time this method is called, so do not close() the cursor. The cursor can be deactivate() safely. If RingtoneManager(Activity) was not used, the caller should manage the returned cursor through its activity's life cycle to prevent leaking the cursor.Dichlorodiphenyltrichloroethane
@user650881: weird thing is that deactivate is deprecated since API 16 (Android 4.1 JellyBean). Looking at the code it seems like the best solution is to create a new instance of RingtoneManager to have a new valid cursor (or to keep the cursor until it is really used).Emboly
how to get name/title of ringtone I am usingRingtone r = RingtoneManager.getRingtone(this, ringtoneURI) String ringToneName = r.getTitle(this) on oppo I am not getting real name `Affair
@SagarHudge Its easier like this rm.getRingtone(rmCursor.position).getTitle(context)Rogelioroger
Should be accessing ringtone info like this: cursor.getString(RingtoneManager.URI_COLUMN_INDEX);. It is much faster, see answer hereRogelioroger

© 2022 - 2024 — McMap. All rights reserved.