我试图在模拟器上点击按钮播放声音文件,但我收到消息“应用程序播放音频已意外停止”
我的代码是:
package com.java4u.android;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class p1 extends Activity {
// creating instance of media player
MediaPlayer mp=MediaPlayer.create(this,R.raw.meow);
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnSound=(Button)this.findViewById(R.id.playSound);
btnSound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent("com.java4u.android.p1"));
mp.start();
}
});
}
}
的Manifest.xml
package="com.java4u.android"
android:versionCode="1"
android:versionName="1.0">
main.xml中
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/playSound"
android:layout_width="fill_parent"
android:layout_height="wrap_content">