5

我是 android 开发人员,想创建一个 android 应用程序。在这个应用程序中,它需要显示 android 默认铃声列表。但我不能显示它。如何获取 android 默认铃声列表。

请帮助我。

4

4 回答 4

12

我赞成 Yuichi 的回答,但它并不完全适合我。对于每个铃声,我都得到了相同的 URI(不同的标题)。以下代码对我有用 -

public void listRingtones() {
  RingtoneManager manager = new RingtoneManager(this);
  manager.setType(RingtoneManager.TYPE_RINGTONE);
  Cursor cursor = manager.getCursor();
  while (cursor.moveToNext()) {
    String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
    Uri ringtoneURI = manager.getRingtoneUri(cursor.getPosition());
    // Do something with the title and the URI of ringtone
  }
}

您可以在playstore上看到正在运行的应用程序。

于 2015-05-02T19:43:25.147 回答
0

这个问题/答案应该提供您所追求的:如何在 Android 上显示可用通知声音列表

你只需要换掉RingtoneManager.TYPE_NOTIFICATIONforRingtoneManager.TYPE_RINGTONE

于 2013-09-11T03:33:08.323 回答
0

您可以使用获取铃声列表RingtoneManager

public void listRingtones() {
  RingtoneManager manager = new RingtoneManager(this);
  manager.setType(RingtoneManager.TYPE_RINGTONE);
  Cursor cursor = manager.getCursor();
  while (cursor.moveToNext()) {
    String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
    String uri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);
    // Do something with the title and the URI of ringtone
  }
}
于 2013-09-11T03:41:14.490 回答
0

获取列表中具有默认铃声的所有铃声列表:

public static Map<String, Uri> getRingtones(Activity activity) {
    RingtoneManager manager = new RingtoneManager(activity);
    manager.setType(RingtoneManager.TYPE_RINGTONE);
    Cursor cursor = manager.getCursor();

    Map<String, Uri> list = new LinkedHashMap<>();
    String title = "Set to Default";
    Uri defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(activity, RingtoneManager.TYPE_RINGTONE);
    list.put(title, defaultRingtoneUri); // first add the default, to get back if select another 

    while (cursor.moveToNext()) {
      String notificationTitle = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
      Uri notificationUri = manager.getRingtoneUri(cursor.getPosition());

      list.put(notificationTitle, notificationUri);
    }



    return list;
  }

然后为 select 创建一个对话框:

public static void getRingtonesDialog(Activity activity, Collection<String> items, int selected, DialogInterface.OnClickListener itemClickListener, DialogInterface.OnClickListener okClickListener, final DialogInterface.OnClickListener cancelClickListener) {
    AlertDialog.Builder builderSingle = new AlertDialog.Builder(activity);
    builderSingle.setTitle("Select Ringtone");
    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(activity, android.R.layout.select_dialog_singlechoice);
    arrayAdapter.addAll(items);

    builderSingle.setNegativeButton(android.R.string.cancel, cancelClickListener);

    builderSingle.setPositiveButton(android.R.string.ok, okClickListener);

    builderSingle.setSingleChoiceItems(arrayAdapter, selected, itemClickListener);

    builderSingle.setCancelable(false);

    builderSingle.show();
  }

然后选择并播放它们进行测试:(这里 AppSettings 设置是您的应用程序中的 SharedPreferences ):

Map<String, Uri> mRingtonesMap;
    private Uri mLastSelectedRingtone = null;
    private String mLastSelectedRingtoneName = "Default";
    private MediaPlayer mMediaPlayer;
    AppSettings settings;
    TextView mRingtone ;

在 Oncreate 中:

mRingtone = (TextView)findViewById(R.id.mRingtone);
        settings = AppSettings.getInstance(this);
        mRingtone.setText(getString(R.string.set_alarm_select_ringtone,
                settings.getString(AppSettings.Key.SELECTED_RINGTONE_NAME, mLastSelectedRingtoneName)));
        mRingtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setupRingtoneSelection();
            }
        });

然后 :

 private void setupRingtoneSelection() {
        mRingtonesMap = AlarmUtils.getRingtones(SelectionsActivity.this);
    mRingtone.setText(getString(R.string.set_alarm_select_ringtone,
            settings.getString(AppSettings.Key.SELECTED_RINGTONE_NAME, mLastSelectedRingtoneName)));

    mRingtone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final List<String> ringtoneKeys = new ArrayList<>(mRingtonesMap.keySet());
            String ringtoneUri = settings.getString(AppSettings.Key.SELECTED_RINGTONE, "");
            int selected = -1;
            for (int i = 0; i < ringtoneKeys.size(); i++) {
                String ring = ringtoneKeys.get(i);
                if (mRingtonesMap.get(ring).toString().equalsIgnoreCase(ringtoneUri)) {
                    selected = i;
                    break;
                }
            }

            AlarmUtils.getRingtonesDialog(SelectionsActivity.this, ringtoneKeys, selected, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) { //item selected
                    String name = ringtoneKeys.get(which);
                    mLastSelectedRingtoneName = name;
                    mLastSelectedRingtone = mRingtonesMap.get(name);

                    try {
                        if (mMediaPlayer == null) {
                            mMediaPlayer = new MediaPlayer();
                        } else {
                            mMediaPlayer.stop();
                            mMediaPlayer.reset();
                        }
                        mMediaPlayer.setDataSource(SelectionsActivity.this, mLastSelectedRingtone);
                        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        mMediaPlayer.setLooping(false);
                        mMediaPlayer.prepare();
                        mMediaPlayer.start();
                    } catch (IOException ioe) {
                        //do nothing
                    }

                }
            }, new DialogInterface.OnClickListener() { //ok
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (mMediaPlayer != null) {
                        mMediaPlayer.stop();
                        mMediaPlayer.release();
                        mMediaPlayer = null;
                    }
                    settings.set(AppSettings.Key.SELECTED_RINGTONE, mLastSelectedRingtone.toString());
                    settings.set(AppSettings.Key.SELECTED_RINGTONE_NAME, mLastSelectedRingtoneName);
                    //Update the textview
                    mRingtone.setText(getString(R.string.set_alarm_select_ringtone,
                            settings.getString(AppSettings.Key.SELECTED_RINGTONE_NAME, mLastSelectedRingtoneName)));
                    dialog.dismiss();
                }
            }, new DialogInterface.OnClickListener() { //cancel
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (mMediaPlayer != null) {
                        mMediaPlayer.stop();
                        mMediaPlayer.release();
                        mMediaPlayer = null;
                    }
                    dialog.dismiss();
                }
            });
        }
    });
}

@Override
protected void onDestroy() {
    if (mMediaPlayer != null) {
        if (mMediaPlayer.isPlaying()) {
            mMediaPlayer.stop();
        }

        mMediaPlayer.release();
        mMediaPlayer = null;
    }

    mRingtone.setOnClickListener(null);

    super.onDestroy();
}

您可以设置和更改铃声,并且在您需要后,可以恢复默认值。

于 2020-10-30T23:36:22.690 回答