2011-04-18 42 views
0

我正在研究一個簡單的聲卡應用程序,只要按下按鈕,它就會播放相應的聲音。然而,我似乎並沒有正確地發佈我的變量(或者現在,因爲我將代碼恢復到播放聲音的地方,但存在內存泄漏),我知道它不符合應用商店標準。我需要弄清楚如何解決這個問題,所以任何幫助表示讚賞!簡單的Soundboard應用程序中的iOS AudioPlayer內存泄漏

我ViewController.h是:

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
#import <AVFoundation/AVAudioPlayer.h> 


@interface FirstViewController : UIViewController <AVAudioPlayerDelegate> 
{ 

} 

-(IBAction)pushTropical; 
-(IBAction)pushVakidis; 
-(IBAction)pushTwiggy; 
-(IBAction)pushScootsy; 
-(IBAction)pushJackiePass; 
-(IBAction)pushJackieIntro; 
-(IBAction)pushBB; 
-(IBAction)yourProblem; 
-(IBAction)pushSuckMy; 
-(IBAction)pushBallGirl; 
-(IBAction)pushGoofballs; 
-(IBAction)pushHavingFun; 
-(IBAction)pushwhoBambi; 
-(IBAction)pushExcited; 
-(IBAction)pushdirtyHippie; 
-(IBAction)pushBambiMother; 
-(IBAction)pushEverybodyPanic; 
-(IBAction)pushSmallChild; 
-(IBAction)pushBurnHouse; 
-(IBAction)pushTitanic; 
-(IBAction)pusherection; 

@end 

我ViewController.m是:

#import "FirstViewController.h" 
#import "AVFoundation/AVAudioPlayer.h" 



@implementation FirstViewController 

-(IBAction)pushTropical { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"Tropical" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 





} 

-(IBAction)pushVakidis { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"Vakidis" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 
-(IBAction)pushBB { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"BB" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushTwiggy { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"Twiggy" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushScootsy { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"Scootsy" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushJackiePass { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"JackiePass" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushJackieIntro { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"JackieIntro" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushyourProblem { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"yourProblemShort" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushBallGirl { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"ballgirl" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushGoofballs { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"goofballs" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushSuckMy { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"yourfamily" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushHavingFun { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"dukesBase" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushwhoBambi { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"whoBambi" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushExcited { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"dukesIsExcited" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushdirtyHippie { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"dirtyhippie" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushBambiMother { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"BambiMother" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushEverybodyPanic { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"EverybodyPanic" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushSmallChild { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"SmallChild" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushBurnHouse { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"BurnHouse" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pushTitanic { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"Titanic" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

-(IBAction)pusherection { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"erection" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 


} 

/* 
// The designated initializer. Override to perform setup that is required before the view is loaded. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 
} 
*/ 

/* 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
*/ 

/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (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. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 

} 

@end 

我已經試過各種方法推(pushTropical,pushBurnHouse等內釋放 'theAudio' 變量)像這樣...

-(IBAction)pushExcited { 
    NSString *path = [[NSBundle mainBundle] 
         pathForResource:@"dukesIsExcited" ofType:@"m4a"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 

    [theAudio release]; 





} 

...但是,然後聲音拒絕播放。所以我不知所措,因爲每當我嘗試在這些功能中的任何地方釋放「音頻」時,它都不會識別它釋放它。幫幫我!

回答

3

在將類設置爲委託後,您應該通過audioPlayerDidFinishPlaying:successfully:AVAudioPlayerDelegate方法釋放AVAudioPlayer。

即:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { 
    [player release]; 
} 

一旦你做到了這一點,就可以成功了。