This example demonstrates how to fetch the URI's of system ringtones (RingtoneManager.TYPE_RINGTONE
):
private List<Uri> loadLocalRingtonesUris() {
List<Uri> alarms = new ArrayList<>();
try {
RingtoneManager ringtoneMgr = new RingtoneManager(getActivity());
ringtoneMgr.setType(RingtoneManager.TYPE_RINGTONE);
Cursor alarmsCursor = ringtoneMgr.getCursor();
int alarmsCount = alarmsCursor.getCount();
if (alarmsCount == 0 && !alarmsCursor.moveToFirst()) {
alarmsCursor.close();
return null;
}
while (!alarmsCursor.isAfterLast() && alarmsCursor.moveToNext()) {
int currentPosition = alarmsCursor.getPosition();
alarms.add(ringtoneMgr.getRingtoneUri(currentPosition));
}
} catch (Exception ex) {
ex.printStackTrace();
}
return alarms;
}
The list depends on the types of requested ringtones. The possibilities are:
RingtoneManager.TYPE_RINGTONE
RingtoneManager.TYPE_NOTIFICATION
RingtoneManager.TYPE_ALARM
RingtoneManager.TYPE_ALL = TYPE_RINGTONE | TYPE_NOTIFICATION | TYPE_ALARM
In order to get the Ringtones as android.media.Ringtone
every Uri
must be resolved by the RingtoneManager
:
android.media.Ringtone osRingtone = RingtoneManager.getRingtone(context, uri);
To play the sound, use the method:
public void setDataSource(Context context, Uri uri)
from android.media.MediaPlayer
. MediaPlayer
must be initialised and prepared according to the State diagram