ios - 如何检测音效何时播放完毕?

标签 ios objective-c audio cocos2d-iphone simpleaudioengine

我正在使用 SimpleAudioEngine 并尝试在继续之前检测音效是否已完成播放。

我正在寻找任何方法,但我尝试实现的方法不起作用!

CDSoundEngine *engine = [CDAudioManager sharedManager].soundEngine;    
ALuint soundId = [[SimpleAudioEngine sharedEngine] playEffect:soundId];

float seconds = [engine bufferDurationInSeconds:soundId];

每次我使用 bufferDurationInSeconds 时,它都会返回一个浮点值 -1 到可变秒数。我检查了实现,当 ID 无效时它返回 -1,但我 100% 确定 ID 有效!

谁能帮我解决这个问题,或者建议我另一种检测音效结束的方法?

最佳答案

中提琴!获取 CDSoundSource,然后从中获取 soundId。

(第二行是可选的,它只是播放声音)。

CDSoundEngine *engine = [CDAudioManager sharedManager].soundEngine;
[[SimpleAudioEngine sharedEngine] playEffect:@"soundeffect.m4a"];
CDSoundSource *aSound =[[SimpleAudioEngine sharedEngine] soundSourceForFile:@"soundeffect.m4a"];
float seconds = [engine bufferDurationInSeconds:aSound.soundId];

另外,为了在播放结束时立即运行一个方法,我会使用一个使用秒结果的 NSTimer。

[NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(aMethod) userInfo:nil repeats:NO];

然后 final 方法实现了一些东西。

-(void)aMethod 
{
NSLog(@"finished playing");
}

尽管这不会影响 -1 结果,但我必须指出,soundId 变量在您的代码中以两种不同的形式出现两次,在同一行中,这会导致问题。不过不用担心,我已经成功地测试了上面的方法。

ALuint **soundId** = [[SimpleAudioEngine sharedEngine] playEffect:**soundId**];

关于ios - 如何检测音效何时播放完毕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15581225/

相关文章:

ios - NSFileHandle fileHandleForWriting 返回 nil,错误代码 24

ios - 我如何删除 NSInvalidArgumentException Trace : <redacted> <redacted> . 。我的应用程序名称.. <redacted>.. 来自 Google Analytics?

iphone - CIFilter 集成仅适用于 CISepiaTone

ios - UIKit 动态 : Attachment inside UITableViewCell

javascript - 在移动浏览器中播放声音?

javascript - 在ReactJS中使用Howler读取本地声音文件

ios - 仪器指出内存不足警告,但内存使用率非常低

ios - 我想在 Swift 中实现 ASMediasFocusDelegate

ios - 将 NSString 转换为 NSDate 时出错

ios - 如何从音频处理中锁定数组以进行分析