【发布时间】:2017-06-08 08:40:46
【问题描述】:
我必须在我的应用程序中更改通知声音。声音是从服务器下载的,所以它存储在我的应用程序存储中。不幸的是,没有播放声音。
File folder = new File(context.getFilesDir() + File.separator + "sounds");
File sound = new File(folder, "my-sound.mp3");
Notification notification = new Builder(context)
.setContentText("test")
.setContentTitle(context.getText(R.string.app_name))
.setSmallIcon(R.drawable.notification_icon)
.setSound(Uri.parse(sound.toString()))
.build();
我也试图以不同的方式获得这个Uri:
.setSound(Uri.parse(sound.toString()))
或者保存在MediaStore:
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, sound.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
values.put(MediaStore.Audio.Media.ARTIST, "Some Artist");
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(sound.getAbsolutePath());
Uri soundUri = context.getContentResolver().insert(uri, values);
声音 Uri 路径现在等于 content://media/external/audio/media/1265,但仍然无法正常工作。
如果我设置默认声音,那么它可以正常工作。 .setSound(RingtoneManager.getDefaultUri(TYPE_NOTIFICATION))
【问题讨论】:
-
检查文件没有损坏。可能在下载过程中损坏了。
-
文件没有损坏
-
context.getContentResolver().insert() 的结果总是 null
标签: android audio notifications