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;
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With