Android MediaPlayer edit

Getting system ringtones

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:

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


Table Of Contents
39 ACRA
61 MediaPlayer
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close