【发布时间】:2016-09-26 19:58:28
【问题描述】:
我想在按住按钮时播放声音,但是当我松开按钮时,我希望停止播放相同的声音。像这样:
private final SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
...
btn1_row1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
sp.play(sndTest, 1, 1, 0, 0, 1);
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
sp.stop(sndTest); // It requires a Stream ID, not a Sound ID
}
return false;
}
});
我已经尝试了以下
final int streamId = sp.play(sndTest, 1, 1, 0, 0, 1);
sp.stop(streamId);
但它似乎不起作用,如果有人可以帮助我,提前谢谢。
【问题讨论】: