如何停止通过Soundpool播放声音? [英] How to stop playing a Sound via Soundpool?

查看:120
本文介绍了如何停止通过Soundpool播放声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看一下这些代码段:

 私有SoundPool soundPool;私有int soundID;soundPool =新的SoundPool(10,AudioManager.STREAM_MUSIC,0);soundID = soundPool.load(this,R.raw.batimanbailedosenxutos,1); 

当我按下按钮时:

  soundPool.play(soundID,音量,音量,1、0、1f); 

如果我同时按下两次此按钮,它将播放声音提示音,如果用户在播放时按下该按钮,我想停止播放声音并再次播放

我尝试放一个

  soundPool.stop(soundID); 

在soundPool.play之前,但我不知道为什么它只在歌曲播放1次时起作用,你们是否有任何想法认为它仅在1次播放时起作用?以及我该如何解决?谢谢

解决方案

方法 stop()使用当前正在播放的流的流ID,而不是您已加载的声音ID.像这样:

  int streamId = soundPool.play(soundID,音量,音量,1、0、1f);soudPool.stop(streamId);streamId = soundPool.play(soundID,音量,音量,1、0、1f); 

将停止当前播放的曲目,然后开始播放另一首曲目.第二种选择是限制您的 SoundPool 允许的流的数量.如果您将 SoundPool maxStreams 值设置为1,则当您尝试启动另一个声音时,任何当前播放的声音都会停止,这可以更清晰地实现所需的行为./p>

Please, have a look at those pieces of code:

private SoundPool soundPool;
private int soundID;

soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundID = soundPool.load(this, R.raw.batimanbailedosenxutos, 1);

and when I press the button:

soundPool.play(soundID, volume, volume, 1, 0, 1f);

if I press this button twice in the same time, it plays the sound togheter, I want to stop the sound and play again if the user press the button while playing

I tryed puting an

soundPool.stop(soundID); 

before the soundPool.play but I don't know why it only work for the 1 time the song is played, do you guys have any ideia why this works only for the 1 time? and how I can solve this? thanks

解决方案

The method stop() takes the stream ID of a currently playing stream, not the sound ID you have loaded. So something like this:

int streamId = soundPool.play(soundID, volume, volume, 1, 0, 1f);

soudPool.stop(streamId);
streamId = soundPool.play(soundID, volume, volume, 1, 0, 1f);

Would stop the currently playing track and start another. A second option would be to limit the number of streams your SoundPool allows. If you instantiate your SoundPool with a maxStreams value of 1, then any currently playing sound will stop when you attempt to start another, which may implement your desired behavior more cleanly.

这篇关于如何停止通过Soundpool播放声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆