How to detect when sound effect finish playing?
Asked Answered
O

1

9

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?

Ostium answered 22/3, 2013 at 23:4 Comment(1)
This might guide you in the right direction.Lavaliere
S
9

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**];
Shepp answered 30/3, 2013 at 9:50 Comment(1)
Brilliant! I was going nuts trying to find an answer to this.Acnode

© 2022 - 2024 — McMap. All rights reserved.