Android 需要让按钮在同时按下时发出声音

Android 需要让按钮在同时按下时发出声音,android,performance,android-intent,Android,Performance,Android Intent,1) 这是我的主要活动 package com.art.drumdrum; import android.app.Activity; import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.Button;

1) 这是我的主要活动

package com.art.drumdrum;

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.MotionEvent;
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.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction()==MotionEvent.ACTION_DOWN)
                soundPool.play(kickId, 1, 1, 0, 0, 1);
            return false;
        }
    });
    snare.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent Event) {
            if (Event.getAction()==MotionEvent.ACTION_DOWN)
                soundPool.play(snareId, 1, 1, 0, 0, 1);
            return false;
        }
    });


}

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

<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>

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>


4) 声音可以正常工作,但我需要知道如何制作,这样我就可以同时按下两个按钮,让它们都发出声音,有什么想法吗?谢谢

我看不出代码有什么问题。您确定您的测试设备支持多点触摸吗? 如果我没记错的话,多点触摸是在Froyo中引入的

编辑: 再看一遍,我想我发现了问题所在。编辑您的代码,使这一行

if (Event.getAction()==MotionEvent.ACTION_DOWN)
会读

if (Event.getAction()==MotionEvent.ACTION_DOWN || Event.getAction()==MotionEvent.ACTION_POINTER_DOWN)

我会试试看,但我怎么知道multi-touch是开着的呢?是的,很好,非常感谢!但是当你触摸屏幕和声音出来时会有一些延迟……知道为什么吗?可能是线程问题。试着在一个单独的线程中播放声音。是的,我查过了,但我不知道如何线程,如果你能看看这篇文章,让我知道我是否可以对我添加的代码做些什么。谢谢,或者让我知道我从主要活动中提取了什么内容来加入到线程中,以及如何调用它