Android 为mediaplayer导入原始mp3资源

Android 为mediaplayer导入原始mp3资源,android,resources,media-player,Android,Resources,Media Player,我正在尝试用以下代码播放一个简单的mp3文件: package swalehm.android.examples.myTest1; import swalehm.android.examples.myTest1.R; import android.app.Activity; import android.content.Context; import android.media.MediaPlayer; import android.os.Bundle; import android.widge

我正在尝试用以下代码播放一个简单的mp3文件:

package swalehm.android.examples.myTest1;
import swalehm.android.examples.myTest1.R;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.TextView;

public class myTest1Main extends Activity 
{
    Context context;

   public MediaPlayer mp = MediaPlayer.create(this, R.raw.sound1);

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText("Hello, Android");
        setContentView(tv);
        mp.start();
    }
}
我在“res”文件夹中添加了一个名为“raw”的文件夹,其中包含sound1.mp3文件

我检查了R.java。该文件中不存在命名的资源sound1。但是,什么时候? 我构建它时,会收到一个错误,说sound1无法解析或不是字段。我浏览了论坛,看到一个建议,从imprts中删除android.R。现在我得到一个错误,说:

方法
MediaPlayer(myTest1Main,int)
对于类型
myTest1Main

未定义,它应该是:

public MediaPlayer mp = MediaPlayer.create(this, R.raw.sound1);

嗨,拉贾斯,谢谢你的帮助。我一定是在尝试不同的东西时搞砸了队伍。我确实成功地构建了这个应用程序。但当它开始在模拟器上运行时,它给了我以下错误:Hello Android应用程序(process swalehm.Android.examples.helloAndroid)意外停止。请再试一次。有人知道为什么会发生这种情况吗?谢谢Swalehm抱歉,我不得不问同样的问题什么是堆栈跟踪。再次感谢您的帮助您的应用程序崩溃了,对吗?因此,在logcat中,搜索导致崩溃的函数列表。如果您使用的是Eclipse,请看一看如何做到这一点。将start()放在onCreat()上是个坏主意:只需旋转屏幕,音乐就可以(取决于清单配置)一次又一次地开始。。。使用叠加注意:在某些上下文中,使用该选项对上下文不起作用。然后您应该使用getApplicationContext()