Hi,
I'm playing sounds using SimpleAudioEngine playEffect:.
Everythings works fine, except that's I need to know when the sound is finished.
Is there a simple way to be notified when the playing is finished ?
Something like :
SimpleAudioEngine playEffect:(NSString*)file withTarget:(id)target andCallback:(SEL)callback
Thanks.
SimpleAudioEngine playEffect: with a callback
(6 posts) (4 voices)-
Posted 1 year ago #
-
No, it is not possible.
You need to use a CDSoundSource object and keep polling the isPlaying property until it returns no. Click on the CDSoundSource tag that I just added for more information.
Posted 1 year ago # -
OK.
Thanks !Posted 1 year ago # -
What I did as I didn't want to keep polling, and I know how long my sounds were was to set up an action callback when I played the sound with a duration set to the length of the sound file.
[fart play];
id action1 = [CCSequence actions:[CCDelayTime actionWithDuration:1.0], [CCCallFunc actionWithTarget:self selector:@selector(callback1)], nil];Works fine for me.
pats
Posted 1 year ago # -
Hi Guys
I'm trying to do a similar thing - I am working on an app that plays relatively short sound effects, triggered by a menu button (I'm using Ray Wenderlich's excellent tutorial at http://www.raywenderlich.com/414/how-to-create-buttons-in-cocos2d-simple-radio-and-toggle).
I'd like the button to toggle back to it's original state once the sound has finished playing.
I'm using SimpleAudioEngine instead of CDSoundSource because I want to preload the effect using preloadEffect to avoid the playback delay when a menu button is tapped the first time.
So, following pats suggestion, I have this:In my Layer's init()
[[SimpleAudioEngine sharedEngine] preloadEffect:@"sound.caf"]; // stuff... CCMenu *toggleMenu = [CCMenu menuWithItems:toggleItem, nil]; toggleMenu.position = ccp(60, 120); [self addChild:toggleMenu];When the menu button is toggled (this sound is 2 seconds long):
- (void)plusMinusButtonTapped:(id)sender { CCMenuItemToggle *toggleItem = (CCMenuItemToggle *)sender; [[SimpleAudioEngine sharedEngine] playEffect:@"sound.caf"]; id action1 = [CCSequence actions:[CCDelayTime actionWithDuration:2.0], [CCCallFunc actionWithTarget:self selector:@selector(mycallbackfunction)], nil]; }and my call back function is:
- (void)mycallbackfunction:(id)sender { // ToDo }In the callback function I want to reset the state of the button once the sound has finished playing.
My questions are:
- is there a better way to do this with CDSoundSource and the isPlaying property and still be able to preload the sounds?
- how do I pass the toggleItem to the callback function so I can tell it to reset it's displayed state?Thanks in advance for any tips :)
Cheers
Lucas
Posted 9 months ago # -
playEffect and CDSoundSource use the same sound pool. Therefore preloadEffect will preload the sound for use by playEffect and CDSoundSource.
As for your other question, I can't help you. I wouldn't do it like that. I'd just use a finite state machine with a single tick method but that is old school (and Birkemose ;) ) style.
Posted 9 months ago #
Reply
You must log in to post.