Here are the sound files if you don't have any of your own.


Bach Musical Offering

Bell.wav

Buzzer.wav

HighClick.wav

LowClick.wav

 


Here are the code snippets to put into your app.


AppDelegate.h

This piece goes after "#import <UIKit/UIKit.h>":

#import <AVFoundation/AVFoundation.h>

#import "ViewController.h"

This piece goes before "@end":

+(AppDelegate *)AD;

-(void)playButtonSound;

-(void)toggleSound;

 


AppDelegate.m

This piece goes after "@implementation AppDelegate":

AppDelegate *singleton;

 

#pragma mark - Sound

 

#define NUM_BUT_SOUNDS   4

int soundNum;

BOOL noSound;

AVAudioPlayer *buttonSounds[NUM_BUT_SOUNDS];

AVAudioPlayer *theme1Sound;

AVAudioPlayer *incorrectSound;

AVAudioPlayer *correctSound[3];

 

-(void)createSound:(AVAudioPlayer *)player filename:(NSString *)fn soundType:(NSString *)st {

    NSURL *fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:fn ofType:st]];

    NSData *audioData = [NSData dataWithContentsOfURL:fileURL];

    if ([player initWithData:audioData error:nil]) {

        player.volume = 1.0;

        [player prepareToPlay];

    } else

        player = nil;

}

 

-(void)loadButtonSound:(int)i {

    buttonSounds[i] = [AVAudioPlayer alloc];

    [self createSound:buttonSounds[i] filename:@"HighClick"soundType:@"wav"];

    if (buttonSounds[i])

        buttonSounds[i].volume = 0.4;

}

 

-(void)playButtonSound {

    if (noSound)

        return;

    if (buttonSounds[soundNum] == nil) {// if sound is unavailable, try loading again

        [self loadButtonSound:soundNum];

        soundNum = (soundNum + 1) % NUM_BUT_SOUNDS;

    }

    if (buttonSounds[soundNum]) {// only load one

        if ([buttonSounds[soundNum] isPlaying]) {

            [buttonSounds[soundNum] stop];

            buttonSounds[soundNum].currentTime = 0;

        }

        [buttonSounds[soundNum] play];

        soundNum = (soundNum + 1) % NUM_BUT_SOUNDS;

    }

}

 

-(void)playThemeSound {

    if (noSound)

        return;

    if (theme1Sound == nil) {

        theme1Sound = [AVAudioPlayer alloc];

        [self createSound:theme1Sound filename:@"Bach Musical Offering" soundType:@"mp3"];

        theme1Sound.numberOfLoops = -1;// uncomment this line if you want the music to infinitely repeat

    }

    if (theme1Sound)

        [theme1Sound play];

}

 

-(void)killThemeSound {

    if (theme1Sound) {

        [theme1Sound stop];

        theme1Sound = nil;

    }

}

 

-(void)toggleSound {

    noSound = !noSound;

    if (noSound) {

        [self killThemeSound];

    } else {

        [self playThemeSound];// uncomment this line if you want the music to infinitely repeat

    }

}

 

+(AppDelegate *)AD {

    return singleton;

}

 

This piece goes after "// Override point for customization after application launch.":

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];// this line allows music and sounds from other programs continue, specifically iTunes

    singleton = self;

    noSound = NO;

    [self playThemeSound];

    for (int i = 0; i < NUM_BUT_SOUNDS; i++)

        [self loadButtonSound:i];

 


Remember to change "#import <UIKit/UIKit.h>" to "#import "AppDelegate.h"" in all your ViewController subclass files.


ViewController.h

This piece goes before "@end":

-(IBAction)playClick:(id)sender;

 


ViewController.m

This piece goes before "@end":

-(IBAction)playClick:(id)sender {

    [[AppDelegate AD] playButtonSound];

}

 


Be sure to subclass your main menu ViewController.

MainMenuViewController.h

This piece goes before "@end":

-(IBAction)toggleSound:(id)sender;

 


MainMenuViewController.m

This piece goes before "@end":

-(IBAction)toggleSound:(id)sender {

    [[AppDelegate AD] toggleSound];

    [[AppDelegate AD] playButtonSound];

}

 


You will still need to make all your IB connections in the storyboard! 

You also need to add the AVFoundation.framework

Additional information