In my soundboard app, after some time the sound will stop working until the app is closed and opened again. Can’t figure out what is wrong! here is my code:
In the .h file :
(imported files here)
@interface MainView : UIView {
}
- (IBAction)pushButton2:(id)sender;
@end
In the .m file:
(imported files here)
@implementation MainView
- (IBAction)pushButton2:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
@end
I’m not sure it would cause the behavior you’re seeing, but you’re definitely leaking the AVAudioPlayer each time the button is pressed. Additionally, I would just load it once (say, in
viewDidLoad), rather than on each button press. Perhaps something like: