问题描述
我正在我的应用程序中构建一个MP3播放器,我收到一条错误,说明原始无法解析或不是字段:mMediaPlayer = MediaPlayer.create(this,R.raw.test_cbr) ;
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);
我不确定R.raw.test_cbr是什么(我没有写这段代码)有人可以解释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;
}
}
}
推荐答案
R.raw
指的是原始放在 res / raw
中的资源。 Raw表示资源文件按原样包含在应用程序包中,没有任何编译时修改。
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.)
你得到这个编译时错误,因为你没有 res / raw
文件夹,并且 R.java $ c中未生成
R.raw
嵌套类$ c>。
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
指文件 test_cbr.ext
,其中 res / raw
文件夹中的 ext
只是一些文件扩展名。
R.raw.test_cbr
refers to a file test_cbr.ext
in the res/raw
folder, where ext
is just some file extension.
由于你要喂 MediaPlayer
,你应该放置一些音频媒体文件 test_cbr
例如 text_cbr.mp3
在 res / raw
中并重建您的应用程序。
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.
这篇关于raw无法解析或不是字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!