Reputation: 2527
I am currently loading an audio file inside of a button. I am assuming there is a more efficient way to load this file instead of every time the button is pressed. Where should this be loaded? Globally, inside viewdidload. I'm loading it like the code below.
NSString *path = [[NSBundle mainBundle] pathForResource:@"click" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
Upvotes: 0
Views: 224
Reputation: 52337
viewDidLoad is a good idea -- will let you free up the memory in viewDidUnload too in case you are short on memory. You could also implement getters and setters in the code below by synthesizing theAudio and using self.theAudio
@interface myClass {
AVAudioPlayer *theAudio;
}
@implementation myClass {
- (void) viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:@"click" ofType:@"mp3"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
}
- (void) viewDidUnload {
[theAudio release];
}
}
Upvotes: 1