获取系统铃声

此示例演示了如何获取系统铃声的 URI(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;
    }

该列表取决于所请求铃声的类型。可能性是:

  • RingtoneManager.TYPE_RINGTONE
  • RingtoneManager.TYPE_NOTIFICATION
  • RingtoneManager.TYPE_ALARM
  • RingtoneManager.TYPE_ALL = TYPE_RINGTONE | TYPE_NOTIFICATION | TYPE_ALARM

为了获得铃声作为时间,每个节目必须由 RingtoneManager 解决:

android.media.Ringtone osRingtone = RingtoneManager.getRingtone(context, uri);

要播放声音,请使用以下方法:

public void setDataSource(Context context, Uri uri)

来自 android.media.MediaPlayer。必须根据状态图初始化和准备 MediaPlayer