gpt4 book ai didi

android - 我需要从原始文件夹动态添加通知声音 - Android

转载 作者:行者123 更新时间:2023-11-29 00:10:15 25 4
gpt4 key购买 nike

我的原始文件夹中有一个名为“notificationsound.mp3”的文件,以及许多其他声音文件。我需要将声音文件动态添加到我生成的通知中。我的方法包括添加查找原始/声音文件的资源 ID,并将其添加。 “con”是传递的上下文。我的代码如下

    //blah blah blah lines of code
String soundname="notificationsound"; // this name will be changed dynamically which is passed via constructor
int res_sound_id = con.getResources().getIdentifier(soundname, "raw", con.getPackageName());
builder.setSound("android.resource://" + con.getPackageName() + "/"+ res_sound_id );
//blah blah lines of code

当然,由于 res_sound_id,我在 builder.setSound() 行中遇到了错误。我确实浏览了几个链接,包括 How to set notification with custom sound in android是否有更好(和正确)的做法?

最佳答案

这是同样的解决方案。1)获取资源id2) 制作一个 Uri 对象3) 调用.setSound()方法

   String s="soundname" // you can change it dynamically
int res_sound_id = con.getResources().getIdentifier(soundname, "raw", con.getPackageName());
Uri u= Uri.parse("android.resource://" + con.getPackageName() + "/" +res_sound_id );
builder.setSound(u);

关于android - 我需要从原始文件夹动态添加通知声音 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30834710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com