2

I have a "notificationsound.mp3" named file in my raw folder, along with many other sound files. I need to dynamically add the sound files to my generated notifications . My approach included adding finding the resource id of the raw/soundfile ,and adding it . "con" is the context passed . My code goes as follows

    //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

Of course , I am gettning error in the builder.setSound() line , because of res_sound_id. I did go through couple of links including How to set notification with custom sound in android Is there a better(& correct) of doing the same?

Community
  • 1
  • 1
lazy rabbit
  • 1,076
  • 3
  • 11
  • 29
  • shows red underline below this function , hint shows as follows : "The method setSound(Uri) in the type NotificationCompat.Builder is not applicable for the arguments ()" – lazy rabbit Jun 14 '15 at 21:28
  • I'd try the solution from [this answer to your linked-to question](http://stackoverflow.com/a/13784557/115145), which creates a `Uri` and skips the pointless `getIdentifier()` call. – CommonsWare Jun 14 '15 at 21:29
  • because "notificationsound" written here is just an example, string will be passed as a sound name , and thus I cannot append "/notificationsound" – lazy rabbit Jun 14 '15 at 21:30
  • Then append `"/"+yourStringVariable`, where `yourStringVariable` is "string will be passed as a sound name". You still do not need the `getIdentifier()` call. – CommonsWare Jun 14 '15 at 21:31
  • you mean : builder.setSound("android.resource://" + con.getPackageName() + "/" +"notificationsound" ); It doesn'k work. Wrong syntax – lazy rabbit Jun 14 '15 at 21:36
  • You forgot the `raw` part. This is included in [this answer to your linked-to question](http://stackoverflow.com/questions/13760168/how-to-set-notification-with-custom-sound-in-android/13784557#13784557). – CommonsWare Jun 14 '15 at 21:38
  • NO,didn't work here, there notification.sound is called NotificationCompat.Builder builder =new NotificationCompat.Builder(con); builder.setSound(Uri ) is called , what should be the uri? – lazy rabbit Jun 14 '15 at 21:42
  • If [this answer to your linked-to question](http://stackoverflow.com/questions/13760168/how-to-set-notification-with-custom-sound-in-android/13784557#13784557), and the other answers, are correct, it should be `Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/" + yourStringVariable)`, where `yourStringVariable` is "string will be passed as a sound name". – CommonsWare Jun 14 '15 at 21:43
  • 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); – lazy rabbit Jun 14 '15 at 21:47

1 Answers1

2

Well here is the solution for the same. 1) get the resource id 2) make a Uri object 3) call the .setSound() method

   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);
lazy rabbit
  • 1,076
  • 3
  • 11
  • 29