1)这是我的主要活动:
package com.art.drumdrum;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
SoundPool soundPool = null;
int kickId = 0;
int snareId = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button kick = (Button) findViewById(R.id.kick);
Button snare = (Button) findViewById(R.id.snare);
kick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (kickId != 0)
soundPool.play(kickId, 1, 1, 0, 0, 1);
}
});
snare.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (snareId != 0)
soundPool.play(snareId, 1, 1, 0, 0, 1);
}
});
}
protected void onResume() {
super.onResume();
if (soundPool == null) {
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
kickId = soundPool.load(this, R.raw.kick, 1);
snareId = soundPool.load(this, R.raw.snare, 1);
}
}
protected void onPause() {
super.onPause();
if (soundPool != null) {
soundPool.release();
soundPool = null;
}
}
}
2)这是我的 xml 清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.art.drumdrum"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.art.drumdrum.MainActivity"
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>
3)这是我的主要 xml 布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context=".MainActivity" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button9"
android:layout_marginLeft="20dp"
android:text="@string/button" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_marginTop="86dp"
android:layout_toLeftOf="@+id/button3"
android:text="@string/button" />
<Button
android:id="@+id/kick"
style="@style/style"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/kick" />
<Button
android:id="@+id/snare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="17dp"
android:layout_toRightOf="@+id/kick"
android:text="@string/snare" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/snare"
android:text="@string/button" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button5"
android:layout_below="@+id/button2"
android:layout_marginLeft="24dp"
android:text="@string/button" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/snare"
android:layout_alignParentTop="true"
android:layout_marginTop="29dp"
android:text="@string/button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button5"
android:layout_alignBottom="@+id/button5"
android:layout_marginLeft="33dp"
android:layout_toRightOf="@+id/snare"
android:text="@string/button" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/button2"
android:text="@string/button" />
</RelativeLayout>
4)代码正在运行,但声音是在释放按钮时发出的,我希望它在您触摸它时立即启动......?有任何想法吗?还需要取出默认的安卓点击声音,这样我就可以让它只是我的声音。谢谢你签到