开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜