-3

I want to play a different alert sound if the multiple choice option answer is correct or wrong. I have code for the correct and wrong answer as follows

if (questionNumber == 1) {
        [self correctAnswer];
    }

if (questionNumber == 2) {
        [self wrongAnswer];
    }

I have created the correct IBAction Alert code. can I create a simple play.alert statement under either the correct or wrong answer?

1 Answers1

0

If you want to use default Apple sounds, use the code in this question, and click on the link he has there to try out different sounds.

If you would like to play custom sounds, the easiest way is with AVAudioPlayer:

First of all, you will need to add the AVFoundation framework to your project. It is very easy from there:

NSURL *url = [NSURL fileURLWithPath:path isDirectory:YES];
AVAudioPlayer *sound = audioPlayers[url];
NSError *error = nil;
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[sound play];
Community
  • 1
  • 1
WolfLink
  • 3,308
  • 2
  • 26
  • 44
  • Hi Wolflink, thanks but i'm not sure how to implement your code. I will use AVFoundation. Do I apply an IBAction to the button in the .h? Is it a @property to synthesise? is it a -(void) or BOOL. I feel like I'm missing something fundamental here. sorry! this is the youtube tutorial and its code links that I based my app on. The 4 multiple choice buttons are what i want to make correct/wrong sounds http://www.youtube.com/watch?v=Uw2pXE9JFDU code:http://developersmp.webs.com/apps/documents/ (choose how to make a more advanced quiz). Would be really grateful for more help. – Matt Ward Mar 30 '13 at 17:39
  • Have a correct answer function and incorrect answer function, each with code like the code I have above. Replace "path" in the code with @"your_filename_here_including_extension" (srry I didn't make that clear, I must have just copied and pasted without thinking). Call those functions when you want to play the sound (yes, probably at some level from IBAction functions from your buttons). – WolfLink Apr 12 '13 at 05:33