Posted By

espinallab on 05/24/11


Tagged

iphone cocos2d SimpleAudioEngine


Versions (?)

Playing and Stopping Sound Effect in Cocos2d


 / Published in: Objective C
 

URL: http://www.espinallab.com

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. }

Report this snippet  

You need to login to post a comment.