我是android开发者,想要创建一个Android应用程序。在这个应用程序中,它需要显示Android默认铃声列表。但我无法显示它。如何获取Android默认铃声列表。
请帮帮我。
答案 0 :(得分:11)
我赞成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上看到工作应用。
答案 1 :(得分:1)
您可以使用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
}
}
答案 2 :(得分:0)
这个问题/答案应该提供您所追求的内容:How to bring up list of available notification sounds on Android
您只需要替换RingtoneManager.TYPE_NOTIFICATION
RingtoneManager.TYPE_RINGTONE
答案 3 :(得分: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;
}
然后创建一个对话框以供选择:
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设置为您的App中的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();
}
您可以设置和更改铃声,并在需要后可以恢复默认设置。