Getting a list of available Ringtones in Android

后端 未结 2 670

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

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-13 10:28

    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;
    

提交回复
热议问题