需要按钮声音播放一个触摸不释放和空安卓默认声音

时间:2013-01-31 02:23:54

标签: android performance android-widget

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)代码正常工作,但按钮释放时声音响起,我想让它在触摸时发射正确....有任何想法吗?还需要取出defualt andriod click声音,这样我才能拥有它的声音。谢谢你签出

1 个答案:

答案 0 :(得分:0)

使用.setOnTouchListener()代替.setOnClickListener()

实施例

button.setOnClickListener(new OnClickListener(){
    onTouch(View v, MotionEvent event){
        if (event.getAction()==MotionEvent.ACTION_DOWN){
            //Play the sound here
        }
    }
}
);

这样,只有在事件发生故障时才会播放声音。