0

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) 声音有效,但我需要知道如何处理并制作它,这样我就可以一次按下 2 个按钮并让它们都输出声音,有什么想法吗?谢谢

4

1 回答 1

0

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

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

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

会读

if (Event.getAction()==MotionEvent.ACTION_DOWN || Event.getAction()==MotionEvent.ACTION_POINTER_DOWN)
于 2013-01-31T04:14:18.443 回答