Im faire une application qui a besoin de jouer beaucoup de sons courts (fichiers mp3). Im en utilisant AvAudioPlayer et les sons jouent bien, MAIS les fuites se construisent jusqu'à ce que mon application se bloque.AvAudioPlayer mémoire fuit?
J'ai une classe séparée pour le joueur
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];
}
@end
Ensuite, je fais une instance de cet objet dans les vues que je veux son.
dans les fichiers .h j'ajouter des lignes suivantes:
@class AVSnd;
AVSnd *mPlayer;
@property (nonatomic, retain) AVSnd *mPlayer;
et dans les fichiers .m i utiliser:
@synthezise mPlayer;
[self.mPlayer initWithMp3File:@"soundFileName"];
[self.mPlayer playNum:1];
[self.mPlayer release];
Mais pourquoi dois-je obtenir des fuites de mémoire-chaque fois que je joue un son? Dois-je implémenter le joueur d'une autre manière?
Merci beaucoup pour toute aide!
oublié d'ajouter la méthode playNum dans AVSnd.m ici est: - (void) playNum: (int) num { self.msoundPlayer.numberOfLoops = num; // utilise uniquement la boucle 1 [self.msoundPlayer prepareToPlay]; AV12Player * tmpPlayer = self.msoundPlayer; [tmpPlayer play]; } –