我想打一个简单的MP3文件,用下面的代码:导入原始的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();
}
}
我在“资源”文件夹中添加了一个名为“原始”文件夹和文件Sound1例子.mp3在里面。
我检查了R.java。指定的资源sound1确实存在于该文件中。但是,当我构建它时,出现错误,说明sound1无法解析或不是字段。我经历了论坛,看到了一个建议,从冲动中删除android.R。现在我收到一条错误消息:
方法MediaPlayer(myTest1Main, int)
对于类型myTest1Main
未定义。
看跌的start()上onCreat()是一个坏主意:只需旋转屏幕,可以(根据清单配置)音乐重新开始......又一次......与叠加 – boctulus 2015-03-30 15:25:09
注意:在某些情况下,使用这将不适用于上下文。那么你应该使用getApplicationContext() – boctulus 2015-03-30 15:26:39