Android SoundPool.stop 似乎不起作用
我创建了一个 MySoundPool 类(我使用该类作为 sigelton,但不认为这与其他所有内容都相关)。我正在初始化 SoundPool(一个 HashMap),并获取 AudioManager 的上下文。此后我加载两个声音。 MySoundpool 通过 MySoundPool.playSound(int index, float rates) 方法使用 playSound 将速率剪辑为 0.5 <= 速率 >= 2.0 并执行语句
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, index, 0, rate);
到目前为止一切顺利。一切正常。 不,当以前的声音仍在播放时, playSound 被调用,我想在播放新声音之前停止它。在上面的代码片段之前我尝试过
mSoundPool.stop(mSoundPoolMap.get(index));
但
mSoundPool.autoPause();
没有成功。声音一直持续到结束。 任何意见将不胜感激
I have created a class MySoundPool (I am using this class as sigelton, but don't think this is relevant as everyting else works). I am initianalizing SoundPool, a HashMap, and get the context for AudioManager. Thereafter I am loading two sounds.
MySoundpool is used by method MySoundPool.playSound(int index, float rate)
playSound clips rate to 0.5 <= rate >= 2.0 an executes statements
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, index, 0, rate);
So far so good. Everything works fine.
No it happens that playSound is called while the previous sound still plays, and I want to stop that before playing the new sound. Prior to the above code snippet I tried
mSoundPool.stop(mSoundPoolMap.get(index));
and
mSoundPool.autoPause();
with no success. The sound just continues to play to its end.
Any comments will be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您在 MySoundPool 类的构造函数中创建了一个新的 SoundPool 对象?
如果是这样,那么 SoundPool 构造函数采用的第一个参数是同时允许的流数量。例如...
这将允许同时播放 10 个声音,因此只需将 10 更改为 1 即可。
编辑:
stop() 方法采用流 ID 作为参数,该参数应该是从 play() 方法返回的数字。您可以尝试设置一个等于 play() 返回的变量,然后在停止声音时使用该变量。
I assume that you create a new SoundPool object in the constructor of your MySoundPool class?
If so then the first argument that SoundPool's constructor takes is the number of streams to allow at the same time. for example...
That will allow 10 sounds to play at once so just change the 10 to a 1 and that should do it.
Edit:
The stop() method takes a stream id as an argument, which should be the number returned from the play() method. You could try setting a variable equal to what play() returns and then use that variable when stopping the sound.
使用 soundId 播放声音,但使用 StreamId 停止声音。这些并不总是相同的数字!
当您启动声音时,存储返回的streamId:
然后使用该streamId(而不是soundId)来停止声音:
Use the soundId to play a sound but use the streamId to stop it. These are not always the same number!
When you start the sound, store the returned streamId:
Then use that streamId (not the soundId) to stop the sound:
正如类文档中所述:
public Final int play (int soundID, float leftVolume, float rightVolume, intpriority, intloop, floatrate)
自以下版本起:API 级别 1
从声音 ID 播放声音。播放soundID指定的声音。这是 load() 函数返回的值。如果成功则返回非零的streamID,如果失败则返回零。 StreamID 可用于进一步控制播放。
返回
遇到同样的问题,为什么不看FM! :D
- 谢谢大家!
As it says in the class document:
public final int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
Since: API Level 1
Play a sound from a sound ID. Play the sound specified by the soundID. This is the value returned by the load() function. Returns a non-zero streamID if successful, zero if it fails. The streamID can be used to further control playback.
Returns
Had the same problem, why does one not read the F M ! :D
-Thanks guys!