Playing multiple audio files in Android
I thi开发者_运维技巧nk I should know this, but my mind has gone blank. I'm making an app that has a few hundred small sound files and I want it to play a certain file dependent on a String I pass to the function. Where I hit the brick wall is getting the resId of the sound file.
I am using this code:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
Gotten from here: http://developer.android.com/guide/topics/media/index.html
All I need is some help passing in a variable resId. Can anyone give me a hand?
Use getResources().getIdentifier()
to convert the String
into a resource ID. Please cache the result, as this lookup is done using reflection and therefore is a bit expensive in terms of CPU.
精彩评论