Android SoundPool.stop 似乎不起作用

发布于 2024-11-27 00:54:06 字数 764 浏览 0 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失退 2024-12-04 00:54:06

我假设您在 MySoundPool 类的构造函数中创建了一个新的 SoundPool 对象?

如果是这样,那么 SoundPool 构造函数采用的第一个参数是同时允许的流数量。例如...

mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);

这将允许同时播放 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...

mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);

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.

强辩 2024-12-04 00:54:06

使用 soundId 播放声音,但使用 StreamId 停止声音。这些并不总是相同的数字!

当您启动声音时,存储返回的streamId:

int myStreamId = mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, index, 0, rate);

然后使用该streamId(而不是soundId)来停止声音:

mSoundPool.stop(myStreamId);

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:

int myStreamId = mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, index, 0, rate);

Then use that streamId (not the soundId) to stop the sound:

mSoundPool.stop(myStreamId);
守护在此方 2024-12-04 00:54:06

正如类文档中所述:

public Final int play (int soundID, float leftVolume, float rightVolume, intpriority, intloop, floatrate)
自以下版本起:API 级别 1

从声音 ID 播放声音。播放soundID指定的声音。这是 load() 函数返回的值。如果成功则返回非零的streamID,如果失败则返回零。 StreamID 可用于进一步控制播放。

返回

non-zero streamID if successful, zero if failed 

遇到同样的问题,为什么不看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

non-zero streamID if successful, zero if failed 

Had the same problem, why does one not read the F M ! :D

-Thanks guys!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文