Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-14-2011, 03:33 AM   #1 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Default 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
Thanks in advance!

Marshu
marshusensei is offline   Reply With Quote
Old 04-14-2011, 08:12 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

AVAudioPlayer can have a delegate. Release the player when the audio finishes.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 04-20-2011, 01:36 AM   #3 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Question 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
My corresponding .h code looks like this:

Code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>


@interface Screen12 : UIViewController <AVAudioPlayerDelegate> {
    AVAudioPlayer *audioPlayer;
    
}

-(IBAction)correct1;
-(IBAction)wrong1;
@property(nonatomic, retain)AVAudioPlayer *audioPlayer;
@end
Thanks guys. Your help or advice is greatly appreciated.
marshusensei is offline   Reply With Quote
Old 04-20-2011, 05:55 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: Tokyo
Posts: 163
kazbluesky is on a distinguished road
Default

What about putting

if (audioPlayer) [audioPlayer release];

just above your

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.delegate = self;
kazbluesky is offline   Reply With Quote
Old 04-20-2011, 06:31 AM   #5 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Default thanks kazbluesky

Quote:
Originally Posted by kazbluesky View Post
What about putting

if (audioPlayer) [audioPlayer release];

just above your

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.delegate = self;


thanks kazbluesky, tried that before, no good so far... im really confused as to what to do?
marshusensei is offline   Reply With Quote
Old 04-20-2011, 07:01 AM   #6 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Default Hmm

Hmm, this might lead to something,

I just simulated a memory warning and received this message:

AudioQueue: request to trim 0 + 1300 = 1300 frames from buffer containing 576 frames

What could that be??
marshusensei is offline   Reply With Quote
Reply

Bookmarks

Tags
assistance required, audio, dealloc, release

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 364
12 members and 352 guests
dansparrow, dre, ilmman, LezB44, Nobbsy, Objective Zero, oztemel, samdanielblr, shagor012, sledzeppelin, thephotographer, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:45 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0