我正在构建一个从Firebase获取音频文件的应用程序,将它们保存在设备上,然后使用该音频文件作为通知声音。
我无法将音频文件作为通知声音播放。
看起来安卓只想要存储在Resources (/res)文件夹中的声音。相反,您似乎不能以编程方式将文件添加到/res文件夹。
希望您能澄清一下这样做的最佳方式。谢谢
发布于 2018-06-08 07:56:57
您可以先使用MediaPlayer
,然后使用setDataSource(String path)
并向其传递文件路径。因为媒体子系统在不同的、非特权进程中运行,所以路径需要是到全局可读文件(例如,在外部存储器上)的路径。例如,您可以将文件转储到Context.getExternalFilesDir()
下的某个位置。
编辑:实际尝试回答有关通知的问题。
Notification.Builder.setSound()
方法接受URI,但没有说明是哪种URI。如果在NotificationManagerService
中跟随代码进入平台,它将使用IRingtonPlayer
播放声音(该接口不在公共SDK中)。它最终使用了另一个类/方法:Rintone.play()
。据我所知,Ringtone
只是包装了一个MediaPlayer
。
因此,长话短说,您应该能够使用全球可读的文件URI调用setSound()
,如下所示:
Uri soundUri = Uri.parse("file://" + filePath.getAbsolutePath());
builder.setSound(uri, ...);
发布于 2018-06-08 10:39:51
这就是你所需要的。
Uri tone = Uri.parse("android.resource://" + this.getPackageName() + "/"+R.raw.notification);
//notification refers to the file name of your mp3 file
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("Message")
.setSound(tone);
https://stackoverflow.com/questions/50751465
复制相似问题