0

How can I play a sound effect only when the user is touching? Just repeat it and stop when the user ends the touch.

Thanks.

Guru
  • 21,652
  • 10
  • 63
  • 102
user465166
  • 39
  • 6

2 Answers2

1

If you use SimpleAudioEngine, you can use playBackgroundMusic: (which defaults to loop) when you call ccTouchesBegan:withEvent:, then call stopBackgroundMusic: when you call ccTouchesEnd:withEvent:.

To use SimpleAudioEngine:

#import "SimpleAudioEngine.h"

To play the music in ccTouchesBegan:withEvent:

SimpleAudioEngine *sae = [SimpleAudioEngine sharedEngine];
[sae preloadBackgroundMusic:@"MusicLoop.mp3"];
sae.backgroundMusicVolume = 0.5f;    // Sets the volume to 50%; this is optional
[sae playBackgroundMusic:@"MusicLoop.mp3"];

To stop the music in ccTouchesEnd:withEvent:

[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
Roger
  • 1,020
  • 1
  • 8
  • 13
1

In your scene use

self.isTouchEnabled = YES;

And now you can respond to methods like ccTouchBegin or ccTouchEnd (forgot their names). So yeah, when touch begins, you can schedule a method which will play the sound effecr over and over. And when the touch ends, unschedule it.

Saturn
  • 17,888
  • 49
  • 145
  • 271