我試圖發揮對按鈕的點擊模擬器的聲音文件,但我得到的消息「應用程序播放音頻意外停止」如何播放聲音按鈕的點擊
我的代碼是:
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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.java4u.android"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".p1" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
的main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="play audio"
android:id="@+id/playSound"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
接受哪個幫你這樣,這將是幫助他人,也將提高你的聲譽答案。 @akash gupta – Lavanya