(Objective C) Playing and Stopping Sound Effect in Cocos2d

This is how you play and stop sound effects in cocos2d

  1. - (void)playSoundFXFor:(int)type
  2. {
  3.     switch (type) {
  4.         case 1:
  5.             [[SimpleAudioEngine sharedEngine] playEffect:@"snap.caf"];
  6.             break;
  7.         case 2:
  8.             [[SimpleAudioEngine sharedEngine] playEffect:@"Erase.caf"];
  9.             break;
  10.         case 3:
  11.             [[SimpleAudioEngine sharedEngine] playEffect:@"boing2.m4a"];
  12.             break;
  13.         case 4:
  14.             [[SimpleAudioEngine sharedEngine] playEffect:@"intro.m4a"];
  15.             break;
  16.         case 5:
  17.             [[SimpleAudioEngine sharedEngine] playEffect:@"tryAgain.m4a"];
  18.             break;
  19.            
  20.         default:
  21.             break;
  22.     }
  23.  
  24.     //Play the tiles narration;
  25.     if (currentFXSoundID > 0) {
  26.         //If we currently playing a sound effect, stop it and play the next audio.
  27.         [[SimpleAudioEngine sharedEngine] stopEffect:currentFXSoundID];
  28.     }
  29.    
  30.     //When you call [[SimpleAudioEngine sharedEngine] playEffect:@"mysound.wav"], it actually return an identifier with type ALuint. Save this value, and you can use it to stop the corresponding effect, using [[SimpleAudioEngine sharedEngine] stopEffect:yourEffectId].
  31.    
  32.     switch (type) {
  33.  
  34.         case 21:
  35.             currentFXSoundID = [[SimpleAudioEngine sharedEngine] playEffect:@"1.m4a"];
  36.             break;
  37.         case 22:
  38.             currentFXSoundID = [[SimpleAudioEngine sharedEngine] playEffect:@"2.m4a"];
  39.             break;
  40.         case 23:
  41.             currentFXSoundID = [[SimpleAudioEngine sharedEngine] playEffect:@"3.m4a"];
  42.             break;
  43.         case 24:
  44.             currentFXSoundID = [[SimpleAudioEngine sharedEngine] playEffect:@"4.m4a"];
  45.             break;
  46.         default:
  47.             break;
  48.     }
  49. }
This entry was posted in Objective-C and tagged , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?

  • Advertisement

  • Categories