Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

timbojill

macrumors member
Original poster
The problem with how the app is now is the first time you press any of the buttons there is a 5 second delay. After that no problem
Code:
.h File for View

#import <UIKit/UIKit.h>
#import<AVFoundation/AVAudioPlayer.h>

@interface buttonSoundViewController : UIViewController <AVAudioPlayerDelegate> {
    }
-(IBAction) button1:(id)sender;
-(IBAction) button2:(id)sender;
-(IBAction) button3:(id)sender;
-(IBAction) button4:(id)sender;
-(IBAction) button5:(id)sender;
-(IBAction) button6:(id)sender;
-(IBAction) button7:(id)sender;

@end

Code:
.M File

-(IBAction) button1:(id)sender{
   
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"wav"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
   
    theAudio.delegate=self;
    [theAudio play];
       
    }

-(IBAction) button2:(id)sender{
   
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"wav"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
   
    theAudio.delegate=self;
    [theAudio play];
   
}

-(IBAction) button3:(id)sender{
   
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sound3" ofType:@"wav"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
   
    theAudio.delegate=self;
    [theAudio play];
   
}

-(IBAction) button4:(id)sender{
   
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sound4" ofType:@"wav"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
   
    theAudio.delegate=self;
    [theAudio play];
   
}

-(IBAction) button5:(id)sender{
   
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sound5" ofType:@"wav"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
   
    theAudio.delegate=self;
    [theAudio play];
   
}

-(IBAction) button6:(id)sender{
   
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sound6" ofType:@"wav"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
   
    theAudio.delegate=self;
    [theAudio play];
   
}

-(IBAction) button7:(id)sender{
   
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sound7" ofType:@"wav"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
   
    theAudio.delegate=self;
    [theAudio play];
   
}
 
I remember reading somewhere that when you play an audio sound for the first time, there is a delay. After that it all runs fine. I think the author loaded one of his sounds in the viewDidLoad method with the volume at 0 and then when he played another sound there was no delay. I'll try and see if I can find exactly where I read it and post it for you.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.