我想在我的应用程序 发挥每按一下按钮点击声音,当我创建的,其h和.M如下内存泄漏维持性
.h文件中的实用程序类
@interface SoundPlayUtil : NSObject<AVAudioPlayerDelegate,AVAudioSessionDelegate>
{
AVAudioPlayer *audioplayer;
}
@property (retain, nonatomic) AVAudioPlayer *audioplayer;
-(id)initWithDefaultClickSoundName;
-(void)playIfSoundisEnabled;
@end
.m文件
@implementation SoundPlayUtil
@synthesize audioplayer;
-(id)initWithDefaultClickSoundName
{
self = [super init];
if (self)
{
NSString* BS_path_blue=[[NSBundle mainBundle]pathForResource:@"click" ofType:@"mp3"];
self.audioplayer =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:BS_path_blue] error:NULL];
[self.audioplayer prepareToPlay];
}
return self;
}
-(void)playIfSoundisEnabled
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:soundStatus]==YES)
{
[self.audioplayer play];
}
}
-(void)dealloc
{
[audioplayer release];
[super dealloc];
}
@end
,并在任何类别按钮点击我做
SoundPlayUtil *obj = [[SoundPlayUtil alloc] initWithDefaultClickSoundName];
[obj playIfSoundisEnabled];
[obj release];
它的工作正常,我成功播放声音。当我分析代码时出现问题。 编译器显示内存泄漏initWithDefaultClickSoundName实用程序类的.m中的方法,因为我将alloc方法发送到self.audioplayer并且未释放它。
发布此对象的最佳地点是什么?
是否使用ARC? –
不,没有使用ARC –