epsilones
epsilones

Reputation: 11609

mediaplayer failing on R.raw - Android

Importing import android.media.MediaPlayer, I am told raw cannot be resolved in

 private void playSound(){
        MediaPlayer mp = MediaPlayer.create(this, R.raw.Jam);
...

I am really new to Android, what is raw for ? and how can I fix this problem ?

Upvotes: 3

Views: 14298

Answers (3)

Chinmai Kulkarni
Chinmai Kulkarni

Reputation: 157

  • You need to right-click on res->new->Android resource directory.

  • Select raw in resource type and it automatically selects raw as the directory name.

  • Drag and drop your .mp3 music file inside the res folder. Make sure that it starts with a small letter.

Upvotes: 0

William Morrison
William Morrison

Reputation: 11006

A raw folder holds files of any type. You need a raw folder under your resources folder (res). In your example, jam is expected as a resource in the raw folder, and will likely be a type of music file as you're using MediaPlayer to try and read it.

You've this error raw cannot be resolved because raw folder doesn't exist, and so the variable raw in class R is not being auto-generated.

Fix the problem by creating the raw folder.

If you already have created res/raw try cleaning the project. Sometimes Eclipse gets confused. If that doesn't work, make a small change to a source file, and save it so the auto-build process kicks off. Sometimes cleaning manually hasn't fixed the problem for me, its a known bug for Eclipse.

Upvotes: 8

Philipp Jahoda
Philipp Jahoda

Reputation: 51411

"raw" is a (or should be) a folder in your Android Project, containing the file "Jam" in your case. ("Jam" will most likely be a .wav or .mp3 file)

Since "raw" cannot be resolved you probably do not have a folder called "raw" in your project. In order to get rid of the error create a folder called "raw" in your Android Project folder.

I am not 100% sure about this but I am also quite sure that files inside the "raw" folder cannot contain CAPITAL letters. (only a-z0-9)

Upvotes: 4

Related Questions