NotACleverMan
NotACleverMan

Reputation: 12237

Playing multiple audio files in Android

I think 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?

Upvotes: 0

Views: 2431

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007534

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.

Upvotes: 1

Related Questions