gpt4 book ai didi

java - raw 无法解析或不是字段

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:26:42 29 4
gpt4 key购买 nike

我正在我的应用程序中构建一个 MP3 播放器,但我收到一条错误消息,指出“raw cannot be resolved or is not a field”在线:mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr) ;

我不确定 R.raw.test_cbr 到底是什么(我没有写这段代码)有人可以解释一下 R.raw.test_cbr 是什么以及如何解决这个问题吗?

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 是指放置在res/raw 中的“原始”资源。 “原始”意味着资源文件按原样包含在应用程序包中,没有任何编译时修改。

(至少在理论上是这样。我遇到过工具链修改我的原始资源的问题,但这超出了这个问题的范围。)

你得到这个编译时错误是因为你没有 res/raw 文件夹并且 R.raw 嵌套类没有在 R 中生成。 java.

R.raw.test_cbr指的是res/raw文件夹下的一个文件test_cbr.ext,其中ext 只是一些文件扩展名。

因为你正在喂 MediaPlayer,你应该放置一些音频媒体文件 test_cbr 例如res/raw 中的 text_cbr.mp3 并重建您的应用程序。

关于java - raw 无法解析或不是字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20644230/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com