How to detect when sound effect finish playing?

久未见 提交于 2019-12-03 06:34:24

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];

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");
}

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**];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!