-1

I made an app to fool around with that is basically a flappy bird clone. I need to know how to make the sound when I touch the screen. I tried using a button but the sound would only play as a touched the button, not the whole screen. When I touched the button, the bird just fell. I know I probably have to use TouchBegan, but what codes do I use and where do I put them? The more detail the better because im a huge rookie in this stuff. Thanks!

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • Look into UITapGestureRecognizer. One can be added to the whole view, and its action method will be called whenever you touch the screen. – rdelmar Apr 04 '14 at 04:16

2 Answers2

1

If you are using a UIView subclass, you can implement the following method to determine if a tap has occurred. From there, just play your sound. (Note, using ARC)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // now that we know that a touch has occurred, lets play a sound
    NSString *pathToMySound = [[NSBundle mainBundle] pathForResource:@"mySound" ofType:@"aif"];
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID([NSURL fileURLWithPath: pathToMySound], &soundID);
    AudioServicesPlaySystemSound(soundID);
}
MrHaze
  • 3,786
  • 3
  • 26
  • 47
Brian Tracy
  • 6,801
  • 2
  • 33
  • 48
  • So basically if I copy that with the exception of "mySound" as I'll replace that with the sound name as I have it saved, that's it? Just copy that code? – user3496401 Apr 04 '14 at 04:31
  • I typed this and an a red ! Came up saying it doesn't recognize "soundPath". What do I do? – user3496401 Apr 04 '14 at 04:39
  • 1
    @user3496401 I don't know the exact details of the `SystemSound` framework. That's for you to look up. There is probably a great page on the apple developer reference site. – Brian Tracy Apr 04 '14 at 04:41
  • this is the link @BrianTracy is talking about, he's got a point :) https://developer.apple.com/library/ios/documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html – MrHaze Apr 04 '14 at 04:48
0

Try to add the UITapGestureRecogniser to the main view which would probably be self.view, or whatever you have named it.

make sure that user interaction is enabled, and create a selector that will play the sound every time it gets called.

this article might help you, I haven't tried it, but it looks like it going in the right direction. I would add the UIGesture programatically though.

--This is how you crate a UITapGesture

//this stuff goes  in your viewDidLoad for example
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSthCool:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self addGestureRecognizer: singleTap]; //self or wherever you want to add it

.....
.....

- (void) doSthCool: (UITapGestureRecognizer *)recognizer{
     //Code to handle the gesture
     NSLog(@"flap");
}

Also, when trying to use a URL don't us CFURLRef if you don't know what its doing, instead try something like this:

NSURL *path = [NSURL URLWithString:@"whatever the url/path is."];

I do suggest that you google some stuff when you get stuck, it's the best way to learn :)

good luck mate! and have fun coding :)

MrHaze
  • 3,786
  • 3
  • 26
  • 47