PDA

View Full Version : Problem with multiple sound buttons.




timbojill
May 21, 2011, 05:35 PM
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

.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



.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];

}



Shawnpk
May 22, 2011, 09:32 AM
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.