我想这样做,当按下按钮时,它开始播放声音,当释放停止时。我知道有一些人有同样的问题,但我没有找到问题的解决方案。
这是我的代码:
private ImageButton pad1, pad2, pad3, pad4, pad5, pad6, pad7, pad8, pad9, pad10, pad11, pad12;
SoundPool soundPool = null;
int sound1_id;
int sound2_id;
int sound3_id;
int sound4_id;
int sound5_id;
int sound6_id;
int sound7_id;
int sound8_id;
int sound9_id;
int sound10_id;
int sound11_id;
int sound12_id;
pad1 = (ImageButton) findViewById(R.id.pad1);
pad2 = (ImageButton) findViewById(R.id.pad2);
pad3 = (ImageButton) findViewById(R.id.pad3);
pad4 = (ImageButton) findViewById(R.id.pad4);
pad5 = (ImageButton) findViewById(R.id.pad5);
pad6 = (ImageButton) findViewById(R.id.pad6);
pad7 = (ImageButton) findViewById(R.id.pad7);
pad8 = (ImageButton) findViewById(R.id.pad8);
pad9 = (ImageButton) findViewById(R.id.pad9);
pad10 = (ImageButton) findViewById(R.id.pad10);
pad11 = (ImageButton) findViewById(R.id.pad11);
pad12 = (ImageButton) findViewById(R.id.pad12);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
sound1_id = soundPool.load(this, R.raw.p1, 1);
sound2_id = soundPool.load(this, R.raw.p2, 1);
sound3_id = soundPool.load(this, R.raw.p3, 1);
sound4_id = soundPool.load(this, R.raw.p4, 1);
sound5_id = soundPool.load(this, R.raw.p5, 1);
sound6_id = soundPool.load(this, R.raw.p6, 1);
sound7_id = soundPool.load(this, R.raw.p7, 1);
sound8_id = soundPool.load(this, R.raw.p8, 1);
sound9_id = soundPool.load(this, R.raw.p9, 1);
sound10_id = soundPool.load(this, R.raw.p10, 1);
sound11_id = soundPool.load(this, R.raw.p11, 1);
sound12_id = soundPool.load(this, R.raw.p12, 1);
public void onPad1Click(View view) {
soundPool.play(sound1_id, (float) 0.5, (float) 0.5, 1, 0, 1.f);
}
public void onPad2Click(View view) {
soundPool.play(sound2_id, (float) 0.5, (float) 0.5, 1, 0, 1.f);
}
public void onPad3Click(View view) {
soundPool.play(sound3_id, (float) 0.5, (float) 0.5, 1, 0, 1.f);
}
public void onPad4Click(View view) {
soundPool.play(sound4_id, (float) 0.5, (float) 0.5, 1, 0, 1.f);
}
public void onPad5Click(View view) {
soundPool.play(sound5_id, (float) 0.5, (float) 0.5, 1, 0, 1.f);
}
public void onPad6Click(View view) {
soundPool.play(sound6_id, (float) 0.5, (float) 0.5, 1, 0, 1.f);
}
public void onPad7Click(View view) {
soundPool.play(sound7_id, (float) 0.5, (float) 0.5, 1, 0, 1.f);
}
public void onPad8Click(View view) {
soundPool.play(sound8_id, (float) 0.5, (float) 0.5, 1, 0, 1.f);
}
public void onPad9Click(View view) {
soundPool.play(sound9_id, (float) 0.5, (float) 0.5, 1, 0, 1.f);
}
public void onPad10Click(View view) {
soundPool.play(sound10_id, (float) 0.5, (float) 0.5, 1, 0, 1.f);
}
public void onPad11Click(View view) {
soundPool.play(sound11_id, (float) 0.5, (float) 0.5, 1, 0, 1.f);
}
public void onPad12Click(View view) {
soundPool.play(sound12_id, (float) 0.5, (float) 0.5, 1, 0, 1.f);
}
}
答案 0 :(得分:0)
看看下面的代码。不幸的是我没有时间检查它是否有效,但如果你遇到麻烦请在这里发表评论,我会帮忙调试它。
import android.media.SoundPool;
import android.view.MotionEvent;
import android.view.View;
public class LoopSoundWhilePressing implements View.OnTouchListener {
private SoundPool soundPool;
private int soundId;
private int playedSoundId; //You need the id that's returned from SoundPool.play() to stop it later on
private boolean isPlaying;
public LoopSoundWhilePressing(SoundPool soundPool, int soundId) {
this.soundPool = soundPool; //You give the soundPool and soundId for each instance.
this.soundId = soundId; //The sound you will be playing
}
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean handled = false;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
if(!isPlaying) {
//The -1 will make it loop until we stop it on ActionUp
playedSoundId = soundPool.play(soundId, (float) 0.5, (float) 0.5, 1, -1, 1.f);
isPlaying = true;
}
handled = true;
break;
}
case MotionEvent.ACTION_UP: {
if(isPlaying) {
soundPool.stop(playedSoundId);
isPlaying = false;
}
handled = true;
break;
}
}
return handled;
}
}