I need to add notification sound dynamically from raw folder - Android -


i have "notificationsound.mp3" named file in raw folder, along many other sound files. need dynamically add sound files generated notifications . approach included adding finding resource id of raw/soundfile ,and adding . "con" context passed . code goes follows

    //blah blah blah lines of code    string soundname="notificationsound"; // name changed dynamically 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 , gettning error in builder.setsound() line , because of res_sound_id. did go through couple of links including how set notification custom sound in android there better(& correct) of doing same?

well here solution same. 1) resource id 2) make uri object 3) call .setsound() method

   string s="soundname" // can change 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); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -