views:

272

answers:

1

I added some audio files to the res file. first I created a raw dir and then I added the folder with the audio files.

How can I get each file path? (I want to use the path to play this audio files)

I found that I need to use

classLoader.getResource

but I don't understand how. what is the argument for this function?

+1  A: 

A reference should be added to your R class that you can get as something like R.raw.sound_file_1. Check out the Audio and Video section of the Android Developer's Guide for more details.

Once you have the reference to your audio clip you can play it by:

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();

So you don't really need the path to the resource at all, just use the reference.

Bill the Lizard
the link you gave me didn't say anything about using an existing file, it talks only about creating new files.How can I regenerate the R class so it will contain all the files and folders that I added to the Resources?
@masterkapu: R should be auto-generated when you build your project. It gets build using what you put in your /res directory. Clean and Build your project then look in the /gen directory.
Bill the Lizard
I did it and it didn't change the R class
@masterkapu: Sorry, I don't have the Android SDK on this machine, so I'm doing this from memory. Are you using Eclipse with the Android plug-in?
Bill the Lizard
yes I I'm using it all
It didn't changed cause I put in it only folders with no filesHow can I make it to see folers?
If you put an audio file directly in your /raw directory (instead of a sub-folder) then build, does R get updated?
Bill the Lizard
yes. but I still don't know how to get the path of the file...
If `R` got updated with something like `R.raw.sound_file_1` then you shouldn't need the path. You can use the reference to the resource to access it.
Bill the Lizard
How do I do it? Can write it in code please?How can I divide the res (to folders or anything else)?
Typically the res folder has subfolders for each type of resource (layouts, drawables, xml files, etc.). You can look at http://developer.android.com/guide/topics/resources/index.html in the Android developers guide.
Feanor
sorry...,I meant the raw folder. Can I do that?
I don't want to play this file. I need to assign it as a contact ringtone