JavaJavaJava
JavaJavaJava

Reputation: 67

raw cannot be resolved or is not a field

I'm building an MP3 player into my app and I'm getting an error stating "raw cannot be resolved or is not a field" on the line: mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr);

I'm not sure exactly what R.raw.test_cbr is (I did not write this code) can someone explain what R.raw.test_cbr is as well as how this can be resolved?

JAVA:

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.idg.omv.R;

public class MediaPlayerDemo extends Activity {

    private static final String TAG = "MediaPlayerDemo";
    private MediaPlayer mMediaPlayer;
    private static final String MEDIA = "media";
    private static final int LOCAL_AUDIO = 1;
    private static final int STREAM_AUDIO = 2;
    private static final int RESOURCES_AUDIO = 3;
    private static final int LOCAL_VIDEO = 4;
    private static final int STREAM_VIDEO = 5;
    private String path;

    private TextView tx;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        tx = new TextView(this);
        setContentView(tx);
        Bundle extras = getIntent().getExtras();
        playAudio(extras.getInt(MEDIA));
    }

    private void playAudio(Integer media) {
        try {
            switch (media) {
                case LOCAL_AUDIO:
                    /**
                     * TODO: Set the path variable to a local audio file path.
                     */
                    path = "";
                    if (path == "") {
                        // Tell the user to provide an audio file URL.
                        Toast
                                .makeText(
                                        MediaPlayerDemo.this,
                                        "Please edit MediaPlayer_Audio Activity, "
                                                + "and set the path variable to your audio file path."
                                                + " Your audio file must be stored on sdcard.",
                                        Toast.LENGTH_LONG).show();

                    }
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setDataSource(path);
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
                    break;
                case RESOURCES_AUDIO:
                    /**
                     * TODO: Upload a audio file to res/raw folder and provide
                     * its resid in MediaPlayer.create() method.
                     */
                    mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr);
                    mMediaPlayer.start();

            }
            tx.setText("Playing audio...");

        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // TODO Auto-generated method stub
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }

    }
}

Upvotes: 5

Views: 10143

Answers (3)

user417397
user417397

Reputation: 49

When I faced the issue, there was another issue. The name of the resource should contain only lowercase letters. If not, any reference to R causes an issue. Also extension is compulsory

Upvotes: 0

Pedro Varela
Pedro Varela

Reputation: 2425

The raw folder goes inside res folder of your Android Project. Onces inside /res, you cand find it through R.raw

Upvotes: 0

laalto
laalto

Reputation: 152817

I'm not sure exactly what R.raw.test_cbr is (I did not write this code) can someone explain what R.raw.test_cbr is as well as how this can be resolved?

R.raw refers to the "raw" resources placed in res/raw. "Raw" means that the resource file is included in the application package as-is, without any compile-time modifications.

(In theory at least. I've had problems where the toolchain modified my raw resources but that's beyond the scope of this question.)

You get this compile time error because you don't have the res/raw folder and the R.raw nested class is not generated in R.java.

R.raw.test_cbr refers to a file test_cbr.ext in the res/raw folder, where ext is just some file extension.

Since you're feeding MediaPlayer, you should place some audio media file test_cbr e.g. text_cbr.mp3 in res/raw and rebuild your application.

Upvotes: 7

Related Questions