Android Soundpool - 触摸事件
Android Soundpool - Touch events
我正在制作一个带有八个图像按钮的鼓垫应用程序,可以在按下按钮时播放声音。我一直在试图弄清楚如何让按钮的按下更具响应性,因为问题是 Soundpool 仅在释放按钮时才播放文件。
我发现有一种方法可以使用 onTouchEvent 使按钮对触摸做出反应,但我不知道如何在我的代码中实现它。我试过对声音同时使用 .ogg 和 .wav 文件类型,但这不是问题所在。我还计划创建在按下按键时循环播放声音文件的按钮,但我的主要问题是如何让所有按钮响应触摸,而不是释放。代码:
package com.example.user.soundboard;
import android.os.Bundle;
import android.view.View;
import android.view.MotionEvent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
SoundPool mySound, mySound2, mySound3, mySound4, mySound5, mySound6, mySound7, mySound8;
int hihatId, kickId, snareId, clapId, snare2Id, hihat2Id, kick2Id, openHatId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mySound2 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mySound3 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mySound4 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mySound5 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mySound6 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mySound7 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mySound8 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
hihatId = mySound.load(this, R.raw.ogghat30, 1);
hihat2Id = mySound.load(this, R.raw.ogghihat14, 1);
kickId = mySound.load(this, R.raw.oggkick, 1);
kick2Id = mySound.load(this, R.raw.oggkick2, 1);
snareId = mySound.load(this, R.raw.oggsnare8, 1);
snare2Id = mySound.load(this, R.raw.oggsnare8, 1);
clapId = mySound.load(this, R.raw.oggclap5, 1);
openHatId = mySound.load(this, R.raw.ogghat24, 1);
}
public void playHihat(View view) {
mySound.play(hihatId, 1, 1, 1, 0, 1);
}
public void playHihat2(View view) {
mySound.play(hihat2Id, 1, 1, 1, 0, 1);
}
public void playOpenHat(View view) {
mySound.play(openHatId, 1, 1, 1, 0, 1);
}
public void playKick(View view) {
mySound.play(kickId, 1, 1, 1, 0, 1);
}
public void playKick2(View view) {
mySound.play(kick2Id, 1, 1, 1, 0, 1);
}
public void playSnare(View view) {
mySound.play(snareId, 1, 1, 1, 0, 1);
}
public void playSnare2(View view) {
mySound.play(snare2Id, 1, 1, 1, 0, 1);
}
public void playClap(View view) {
mySound.play(clapId, 1, 1, 1, 0, 1);
}
对于 ImageButtons,我目前在布局中使用 onClick 方法。
感谢您的帮助!
而不是 OnClickListener
你必须使用 OnTouchListener
.
yourButton.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
// Release sound on touch up
return true;
} else if(event.getAction() == MotionEvent.ACTION_DOWN){
// Play sound
return true;
}
return false;
}
});
您可以在您的视图上设置触摸事件监听器,这样您就可以播放响应触摸事件的声音。
这里有一个例子:
yourButtonView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
//Fired when you touch the button
break;
case MotionEvent.ACTION_MOVE:
//Fired once you are touching button and you move your finger without release the button
break;
case MotionEvent.ACTION_UP:
//Fired when you stop touching the button
break;
}
return true;
}
});
我正在制作一个带有八个图像按钮的鼓垫应用程序,可以在按下按钮时播放声音。我一直在试图弄清楚如何让按钮的按下更具响应性,因为问题是 Soundpool 仅在释放按钮时才播放文件。
我发现有一种方法可以使用 onTouchEvent 使按钮对触摸做出反应,但我不知道如何在我的代码中实现它。我试过对声音同时使用 .ogg 和 .wav 文件类型,但这不是问题所在。我还计划创建在按下按键时循环播放声音文件的按钮,但我的主要问题是如何让所有按钮响应触摸,而不是释放。代码:
package com.example.user.soundboard;
import android.os.Bundle;
import android.view.View;
import android.view.MotionEvent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
SoundPool mySound, mySound2, mySound3, mySound4, mySound5, mySound6, mySound7, mySound8;
int hihatId, kickId, snareId, clapId, snare2Id, hihat2Id, kick2Id, openHatId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mySound2 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mySound3 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mySound4 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mySound5 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mySound6 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mySound7 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mySound8 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
hihatId = mySound.load(this, R.raw.ogghat30, 1);
hihat2Id = mySound.load(this, R.raw.ogghihat14, 1);
kickId = mySound.load(this, R.raw.oggkick, 1);
kick2Id = mySound.load(this, R.raw.oggkick2, 1);
snareId = mySound.load(this, R.raw.oggsnare8, 1);
snare2Id = mySound.load(this, R.raw.oggsnare8, 1);
clapId = mySound.load(this, R.raw.oggclap5, 1);
openHatId = mySound.load(this, R.raw.ogghat24, 1);
}
public void playHihat(View view) {
mySound.play(hihatId, 1, 1, 1, 0, 1);
}
public void playHihat2(View view) {
mySound.play(hihat2Id, 1, 1, 1, 0, 1);
}
public void playOpenHat(View view) {
mySound.play(openHatId, 1, 1, 1, 0, 1);
}
public void playKick(View view) {
mySound.play(kickId, 1, 1, 1, 0, 1);
}
public void playKick2(View view) {
mySound.play(kick2Id, 1, 1, 1, 0, 1);
}
public void playSnare(View view) {
mySound.play(snareId, 1, 1, 1, 0, 1);
}
public void playSnare2(View view) {
mySound.play(snare2Id, 1, 1, 1, 0, 1);
}
public void playClap(View view) {
mySound.play(clapId, 1, 1, 1, 0, 1);
}
对于 ImageButtons,我目前在布局中使用 onClick 方法。
感谢您的帮助!
而不是 OnClickListener
你必须使用 OnTouchListener
.
yourButton.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
// Release sound on touch up
return true;
} else if(event.getAction() == MotionEvent.ACTION_DOWN){
// Play sound
return true;
}
return false;
}
});
您可以在您的视图上设置触摸事件监听器,这样您就可以播放响应触摸事件的声音。
这里有一个例子:
yourButtonView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
//Fired when you touch the button
break;
case MotionEvent.ACTION_MOVE:
//Fired once you are touching button and you move your finger without release the button
break;
case MotionEvent.ACTION_UP:
//Fired when you stop touching the button
break;
}
return true;
}
});