简体   繁体   中英

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 ?

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.

"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)

  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM