How to release Audio correctly to avoid memory issues.
Hello everyone,
Still learning all this stuff, so please forgive my noobie question.
I'm working on an app that contains 25 screens, each leads on to the next. Each screen loads an image and a short 7 second sound. All works fine, however, I am having some memory issues, and when testing on a device, the audio goes a bit crazy on about the 9th screen.
I'm thinking I must be releasing something incorrectly. Any advice would be greatly appreciated.
Here is my code:
Code:
#import "Screen8.h"
#import "Screen9.h"
@implementation Screen8
-(IBAction)correct1{
//Correct Alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Correct"
message:@"Your chose the right one!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
//move to next page
Screen9 *screen = [[Screen9 alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release];
}
-(IBAction)wrong1{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Try again"
message:@"Try again please"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[audioPlayer release];
[audioPlayer dealloc];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
//play audio file:
[audioPlayer stop];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/h.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil)
NSLog(@"%@",[NSString stringWithFormat:@"%@",error]);
else
[audioPlayer play];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear: animated];
[audioPlayer stop];
}
@end
Still having major memory issues?? How to release correctly
The audio is my app is still overlapping... it's strange, sometimes it works fine, other times it doesn't... I've tried releasing the audio (set it as a delegate) now I think I'm over-releasing it???
Can anyone help, here is my code there are about 25 screens than transition from one to the next, memory issues seem to occur about the 6th or 7th screen.... Here is my .m
Code:
#import "Screen12.h"
#import "Screen13.h"
@implementation Screen12
@synthesize audioPlayer;
-(IBAction)correct1{
//Correct Alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Correct"
message:@"Wow! l."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
//move to next page
Screen13 *screen = [[Screen13 alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
if (audioPlayer) [audioPlayer release];
[screen release];
}}
-(IBAction)wrong1{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Try again"
message:@"Click the correct image"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[audioPlayer release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
//play audio file:
//[audioPlayer stop];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/l.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.delegate = self;
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil)
NSLog(@"%@",[NSString stringWithFormat:@"%@",error]);
else
[audioPlayer play];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear: animated];
[audioPlayer stop];
}
@end