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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -