gpt4 book ai didi

android - 在android中按下按钮时播放循环声音

转载 作者:行者123 更新时间:2023-11-30 04:49:30 25 4
gpt4 key购买 nike

我的问题很简单。我只想在按下按钮时播放循环声音。以下是我尝试过的 switch 语句和 if else 方法。充其量,我可以播放声音,但当不再按下按钮时它不会暂停。

    public boolean onTouch(View v, MotionEvent event) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.clash);

if (v.getId() == R.id.clash && event.getAction() == MotionEvent.ACTION_DOWN){
mp.setLooping(true);
mp.start();

}else if (v.getId() == R.id.clash && event.getAction() == MotionEvent.ACTION_UP)
{mp.pause();

}
}

public boolean onTouch(View v, MotionEvent event) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.clash);

switch (event.getAction()){

case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();

case MotionEvent.ACTION_UP:
mp.pause();
}


return false;
}

最佳答案

尝试在 onTouch 结束时返回 true 而不是 false

现在(可能)发生的是您收到一个启动声音的 ACTION_DOWN 事件。通过返回 false,您告诉框架您没有使用该操作,这意味着您也不会使用 future 的操作。因此,它将停止向您的 View 发送触摸事件。

相关引自android-developers :

When you get the ACTION_DOWN event, are you returning true? If not, this is probably the issue. The framework will only deliver future touch events (ACTION_MOVEs and the ACTION_UP) to that View if its touch listener or onTouchEvent returns true.

关于android - 在android中按下按钮时播放循环声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3918810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com