たくさんの短い音(mp3ファイル)を再生する必要があるアプリケーションをやっています。AvAudioPlayerを使用していて、サウンドは問題なく再生されていますが、アプリがクラッシュするまでリークが発生しています。
プレーヤー用に別のクラスがあります
AVSnd.h
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface AVSoundPlayer : NSObject <AVAudioPlayerDelegate> {
AVAudioPlayer *msoundPlayer;
}
@property (nonatomic, retain) AVAudioPlayer *msoundPlayer;
-(id)initWithMp3File: (NSString *)inString;
-(void) playNum:(int)num;
@end
AVSND.m
@implementation AVSoundPlayer
@synthesize msoundPlayer;
-(id)initWithMp3File: (NSString *)fileName{
if (self = [super init]){
NSBundle *mainBundle = [NSBundle mainBundle];
NSError *error;
NSURL *sURL = [NSURL fileURLWithPath:[mainBundle
pathForResource:fileName ofType:@"mp3"]];
self.msoundPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:sURL error:&error];
if (!self.msoundPlayer) {
NSLog(@"Sound player problem: %@", [error localizedDescription]);
}
}
return self;
}
-(void) playNum:(int)num{
self.msoundPlayer.numberOfLoops = num;
[self.msoundPlayer prepareToPlay];
AVAudioPlayer *tmpPlayer = self.msoundPlayer;
[tmpPlayer play];
}
- (void)dealloc {
[self.msoundPlayer release];
[super dealloc];
}
@終わり
次に、サウンドが必要なビューでこのオブジェクトのインスタンスを作成します。
.hファイルに次の行を追加します。
@class AVSnd;
AVSnd *mPlayer;
@property (nonatomic, retain) AVSnd *mPlayer;
そして私が使用する.mファイルで:
@synthezise mPlayer;
[self.mPlayer initWithMp3File:@"soundFileName"];
[self.mPlayer playNum:1];
[self.mPlayer release];
しかし、サウンドを再生するたびにメモリリークが発生するのはなぜですか?別の方法でプレーヤーを実装する必要がありますか?
助けてくれてありがとう!