Reputation: 135
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];
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?
Upvotes: 9
Views: 1657
Reputation: 2194
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**];
Upvotes: 9