How to detect when sound effect finish playing?(如何检测音效何时播放完毕?)
问题描述
我正在使用 SimpleAudioEngine,并尝试在继续之前检测音效是否播放完毕.
I'm using SimpleAudioEngine and I'm trying to detect if a sound effect is finish playing before continuing.
我正在寻找任何方法,但我尝试实施的方法不起作用!
I'm looking for any method, but the one I'm trying to implement doesn't work!
CDSoundEngine *engine = [CDAudioManager sharedManager].soundEngine;
ALuint soundId = [[SimpleAudioEngine sharedEngine] playEffect:soundId];
float seconds = [engine bufferDurationInSeconds:soundId];
每次我使用 bufferDurationInSeconds 时,它都会返回一个浮点值 -1 到变量秒.我检查了实现,当 ID 无效时它返回 -1,但我 100% ID 是有效的!
Every time I use bufferDurationInSeconds, it returns a float value of -1 to variable seconds. I checked the implementation, and it returns a -1 when the id is not valid, but I'm 100% the ID is valid!
谁能帮我解决这个问题,或者建议我另一种方法来检测声音效果的结束?
Can anyone help me on this problem, or suggest me another way to detect the end of an sound effect?
推荐答案
Violà!获取 CDSoundSource,然后从中获取 soundId.
Violà! Getting CDSoundSource, and then the soundId from that works.
(第二行是可选的,它只是播放声音).
(The second line is optional, it just plays the sound).
CDSoundEngine *engine = [CDAudioManager sharedManager].soundEngine;
[[SimpleAudioEngine sharedEngine] playEffect:@"soundeffect.m4a"];
CDSoundSource *aSound =[[SimpleAudioEngine sharedEngine] soundSourceForFile:@"soundeffect.m4a"];
float seconds = [engine bufferDurationInSeconds:aSound.soundId];
还要在播放完毕后立即运行一个方法,我会使用一个使用秒结果的 NSTimer.
Also to run a method right when it finishes playing I would use an NSTimer that uses the seconds result.
[NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(aMethod) userInfo:nil repeats:NO];
然后最后的方法实现了一些东西.
And then the final method implements something.
-(void)aMethod
{
NSLog(@"finished playing");
}
虽然这不会影响 -1 结果,但我必须指出,soundId 变量在您的代码中以两种不同的类型出现两次,在同一行中,这会导致问题.不过不用担心,我已经成功测试了上面的方法.
Although, this doesn't effect the -1 result, I have to point out that the soundId variable appears twice as two different kinds in your code, in the same line, which will cause problems. No worries though, I have tested my method above with success.
ALuint **soundId** = [[SimpleAudioEngine sharedEngine] playEffect:**soundId**];
这篇关于如何检测音效何时播放完毕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检测音效何时播放完毕?
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01