Java Android: How to give media player a string value to a resource -
i have function run audio
public void playsound(string filename){ mediaplr = mediaplayer.create(context, r.raw.s1); mediaplr.start(); }
the problem r.raw.s1
, want replace s1
filename
, doesn't take string. can do???
thanks help
if file stored in res/raw/
directory, can retrieve identifier through following code:
public void playsound(string filename){ int sound_id = context.getresources().getidentifier(filename, "raw", context.getpackagename()); if(sound_id != 0) { mediaplr = mediaplayer.create(context, sound_id); mediaplr.start(); } }
i prefer start variable names small letters, rather capital. helps distinguish static class.
you can create map store id further reuse instead of retrieving identifier each time.
Comments
Post a Comment