AVAudioPlayer Crash
I have one problem with my iOS
Application. I want to play multiple audio files (.WAV
) using IBAction
and AVAudioPlayer
. Unfortunatelly the sound plays but if I play the sound many times, my application crashes. Maybe because memory is not properly released. Can you confirm my hypothesis and/or tell me how to fix this problem?
Here's my code.
ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
@interface ViewController开发者_如何学JAVA : UIViewController
{
NSString *Path;
}
- (IBAction)Sound1;
- (IBAction)Sound2;
- (IBAction)Sound3;
- (IBAction)Sound4;
@end
ViewController.m
#import <AVFoundation/AVAudioPlayer.h>
#import "ViewController.h"
@implementation ViewController
- (IBAction)Sound1
{
Path = [[NSBundle mainBundle] pathForResource:@"Sound1" ofType:@"wav"];
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media play];
}
- (IBAction)Sound2
{
Path = [[NSBundle mainBundle] pathForResource:@"Sound2" ofType:@"wav"];
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media play];
}
- (IBAction)Sound3
{
Path = [[NSBundle mainBundle] pathForResource:@"Sound3" ofType:@"wav"];
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media play];
}
- (IBAction)Sound4
{
Path = [[NSBundle mainBundle] pathForResource:@"Sound4" ofType:@"wav"];
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[player release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)dealloc
{
[super dealloc];
}
@end
The problem is that you're allocating lots of AVAudioPlayer instances, but not releasing any of them.
What you need to do is set up your class as a AVAudioPlayerDelegate
, remove the media instance variable from your class (instead simply create a new instance variable within your various IBAction methods) and implement the audioPlayerDidFinishPlaying:successfully:
method as follows:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
[player release];
}
Finally, update your code to set your class as the delegate for each of the AVAudioPlayers you set up as follows:
...
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media setDelegate:self];
[Media play];
...
By doing this, you'll ensure that you're releasing all of the AVAudioPlayer instances you create.
Media is never released... this is causing the memory leak. use SetterGetter methods, so that whenever new object will be allocated the previous object will be release.
精彩评论