獲取系統鈴聲

此示例演示瞭如何獲取系統鈴聲的 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