我正在使用 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/