我正在使用以下方法从应用程序目录中选择铃声。.适用于Oreo和牛轧糖,但不适用于牛轧糖。
private void setRingtone(String filePath) {
File k = new File(filePath);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, " ");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
itemView.getContext().getContentResolver().delete(uri,
MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
try {
Uri pathd = Uri.parse(filePath);
// The line below will set it as a default ring tone replace
// RingtoneManager.TYPE_RINGTONE with
RingtoneManager.TYPE_NOTIFICATION
// to set it as a notification tone
RingtoneManager.setActualDefaultRingtoneUri(
itemView.getContext(), RingtoneManager.TYPE_RINGTONE,pathd);
}
catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
尝试以下格式:类型1:
Uri ringToneUri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
mediaPlayer.setLooping(true);
try {
mediaPlayer.setDataSource(this, ringToneUri);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
类型2:
mediaPlayer =新的MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.ringtone);
if (afd == null) {
return;
}
try {
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setLooping(true);
mediaPlayer.start();