我正在使用 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];
然后最终的方法实现一些东西。
-(void)aMethod
{
NSLog(@"finished playing");
}
虽然,这不会影响 -1 结果,但我必须指出 soundId 变量在代码中作为两种不同类型出现两次,在同一行中,这将导致问题。不过不用担心,我已经成功测试了上面的方法。
ALuint **soundId** = [[SimpleAudioEngine sharedEngine] playEffect:**soundId**];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)