【问题标题】:Need Button sound to play one Touch not release and null android default sound需要 Button 声音来播放一个 Touch 不释放和 null android 默认声音
【发布时间】:2013-01-31 02:23:54
【问题描述】:

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) 代码正在运行,但声音是在按钮释放时发出的,我希望它在您触摸它时立即启动....?有任何想法吗?还需要取出默认的安卓点击声音,这样我就可以让它只是我的声音。感谢签到

【问题讨论】:

    标签: android performance android-widget


    【解决方案1】:

    使用.setOnTouchListener() 而不是.setOnClickListener()

    例子

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

    这样只有当事件是向下事件时才会播放声音。

    【讨论】:

    • 感谢它的工作,除了现在当我触摸它多次点击时,请注意,当我在按钮上拖动手指时它会一直发出声音
    • 你可以将键设置为按钮并使用 OnKey 方法吗?以及如何在这里实现?
    • @the_qwertyas 我编辑了我的答案以匹配您更新的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多