Play sound in Sequence in cocos2d - iphone

how to play sound in cocos2d in sequence does any body have any idea for example i have three sound like
[[SimpleAudioEngine sharedEngine] playEffect:[CommanMethods GetCompleteSoundPath:objplate.OrderOneSound]];
[[SimpleAudioEngine sharedEngine] playEffect:[CommanMethods GetCompleteSoundPath:objplate.WithSound]];
[[SimpleAudioEngine sharedEngine] playEffect:[CommanMethods GetCompleteSoundPath:objplate.OrderTwoSound]];
this is the code of sound effect play how can i play when one is stop and then another play after that

CCSequence* sequence = [CCSequence actions:
[CCCallBlock actionWithBlock:^(void)
{
[[SimpleAudioEngine sharedEngine] playEffect:[CommanMethods GetCompleteSoundPath:objplate.OrderOneSound]];
}],
[CCDelayTime actionWithDuration:3],
[CCCallBlock actionWithBlock:^(void)
{
[[SimpleAudioEngine sharedEngine] playEffect:[CommanMethods GetCompleteSoundPath:objplate.WithSound]];
}],
[CCDelayTime actionWithDuration:3],
[CCCallBlock actionWithBlock:^(void)
{
[[SimpleAudioEngine sharedEngine] playEffect:[CommanMethods GetCompleteSoundPath:objplate.OrderTwoSound]];
}],
nil];
[self runAction:sequence];
this way can solve your problem, but only drawback is you have to insert delay time by yourself. hope it work for your situation.

Related

Trying to play a Random Sound using SimpleAudioEngine, no sound

So initially the app I was going to produce only made one sound upon swiping up. The code for that was
[[SimpleAudioEngine sharedEngine] playEffect:#"Swoosh.mp3"];
CCMoveTo *moveto=[CCMoveTo actionWithDuration:0.50 position:ccp(currentSkewed.position.x, [UIScreen mainScreen].bounds.size.height+300)];
CCScaleTo *scalto=[CCScaleTo actionWithDuration:0.30 scale:0.5];
CCSequence *seq=[CCSequence actionOne:moveto two:[CCCallFuncN actionWithTarget:self selector:#selector(RemoveSprite:)]];
[currentSkewed runAction:seq];
[currentSkewed runAction:scalto];
[skewdArray removeObject:currentSkewed];
currentSkewed=nil;
I decided I want there to be three sounds that randomly play upon each swipe rather than just one. So here's what I did.
NSString *Soundname;
int randSound = arc4random() % 3;
switch (randSound) {
case 0:
Soundname = #"Swoosh1.mp3";
break;
case 1:
Soundname = #"Swoosh2.mp3";
break;
case 2:
Soundname = #"Swoosh3.mp3";
break;
}
[[SimpleAudioEngine sharedEngine] playEffect:#"Soundname"];
CCMoveTo *moveto=[CCMoveTo actionWithDuration:0.50 position:ccp(currentSkewed.position.x, [UIScreen mainScreen].bounds.size.height+300)];
CCScaleTo *scalto=[CCScaleTo actionWithDuration:0.30 scale:0.5];
CCSequence *seq=[CCSequence actionOne:moveto two:[CCCallFuncN actionWithTarget:self selector:#selector(RemoveSprite:)]];
[currentSkewed runAction:seq];
[currentSkewed runAction:scalto];
[skewdArray removeObject:currentSkewed];
currentSkewed=nil;
I made the string, then replaced playEffect:#"Swoosh.mp3"]; with playEffect:#"Soundname"];
I've tried multiple variations of Soundname, none seem to me working. I'm fairy new at coding, I know this is something silly that i'm missing. Does anybody have any ideas ?
I'm pretty sure the problem is because you are still using a literal string for the playEffect parameter but your intent was to use the newly defined string variable.
Try changing:
[[SimpleAudioEngine sharedEngine] playEffect:#"Soundname"];
to this (note the lack of quotes):
[[SimpleAudioEngine sharedEngine] playEffect:Soundname];

simple audio engine isBackgroundMusicPlaying is still YES after done playing

I need to check whether the background music is playing every seconds, and if it's not playing, then i will randomly start to play another song.
This method gets called every seconds
- (void)private_checkMusicEnds {
if (NUM_BGMUSIC > 0) {
if ([ModuleHelper audio_isBackgroundMusicEnabled]) {
//ERROR: BACKGROUND MUSIC NOT PLAYING BUT ALWAYS IS PLAYING???
if ([SimpleAudioEngine sharedEngine].isBackgroundMusicPlaying == NO) {
if ([CacheHelper isFirstTimeSetDefaultBGMusic]) {
_currentPlayingMusic = DEFAULT_BGMUSIC_INDEX;
}
else {
//song start at 1
_currentPlayingMusic = [Helper getRandomIntBothInclusiveLow:1 high:NUM_BGMUSIC];
}
NSString *fn = [NSString stringWithFormat:FORMAT_STRING_SONG_FILE_NAME, _currentPlayingMusic];
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:fn loop:NO];
}
}
}
}
When the song is finished playing, [SimpleAudioEngine sharedEngine].isBackgroundMusicPlaying is still YES.
I have also tried [[CDAudioManager sharedManager].backgroundMusic isPlaying] but I get the same.

iOS: How to get my audio app to register in the multitask controls?

When a user double clicks the home button and swipe right, some audio controls shows.
How do I get use them?
I've searched but havn't found anything that helps me.
Try this
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
[self playTapped];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self previousTapped];
break;
case UIEventSubtypeRemoteControlNextTrack:
[self nextTapped];
break;
default:
break;
}
PlayTapped is the method for playing the music.
nextTapped is the method for the next song to be played.
previousTapped is for playing the previous track.
All the best

Handle iPhone interruption in a music application

I've tried the code to avoid the sleep mode interruption. It's working, but now I have a problem with interruptions when a call or sms is coming in to the iPhone. The song in my music application is stopped at that time. I want to automatically resume the song where it was stopped once the call is over.
- (void) audioPlayerBeginInterruption: (AVAudioPlayer *) player {
if (playing) {
playing = NO;
interruptedOnPlayback = YES;
[self updateViewForPlayerState];
}
}
- (void) audioPlayerEndInterruption: (AVAudioPlayer *) player {
if (interruptedOnPlayback) {
[player prepareToPlay];
[player play];
playing = YES;
interruptedOnPlayback = NO;
}
}
But it doesn't work. Please help me here, thanks in advance.
Try Apple's guide to Audio Interruptions.

AVAudioPlayer resetting currently playing sound and playing it from beginning

I'm having an issue using AVAudioPlayer where I want to reset a player if it's currently playing and have it play again.
I try the following with no luck:
The sound plays once but then the second time i select the button it stops the sound, the third time starts the sound up again.
//Stop the player and restart it
if (player.playing) {
NSLog(#"Reset sound: %#", selectedSound);
[player stop];
[player play];
} else {
NSLog(#"playSound: %#", selectedSound);
[player play];
}
I've also tried using player.currentTime = 0 which indicates that would reset the player, that didn't work, I also tried resetting currentTime = 0 and then calling play that didn't work.
//Stop the player and restart it
if (player.playing) {
NSLog(#"Reset sound: %#", selectedSound);
player.currentTime = 0;
[player play];
} else {
NSLog(#"playSound: %#", selectedSound);
[player play];
}
For your situation I would try the following
//Pause the player and restart it
if (player.playing) {
NSLog(#"Reset sound: %#", selectedSound);
[player pause];
}
player.currentTime = 0;
[player play];
If you are going to immediately play the sound again, I would suggest pause instead of stop. Calling stop "Stops playback and undoes the setup needed for playback."
As indicated, if you want the sound to play again, you should avoid disabling the AVAudioPlayer instance with a stop but rather, just issue a reset of the position where it is playing. So, don't use stop() or pause() but as Plumenator mentioned, just issue .currentTime=0. So in your case, you would simply do:
if (player.isPlaying) {
player.currentTime = 0
} else {
player.play()
}
Swift example:
player.currentTime = 0.0;