2011-08-28 3 views
0

J'ai un problème et je ne sais pas comment le gérer. Tyring pour construire une application avec son et animation. Cela me donne un avertissement de niveau 1 puis de niveau 2. J'ai essayé de construire et d'analyser et j'ai eu toutes ces fuites potentielles. Si je relâche l'Audio, le son ne sera pas lu. Des indices?AVAudioPlayer fuit?

Voici une partie du code et les Leakes J'ai

- (IBAction)playsound2 
    { 
     NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"mp3"]; 
     AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
     *- **Method returns an Objective-C object with a +1 retain count (owning reference)*** 
     theAudio.delegate = self; 
     [theAudio play]; 

     ***- Object allocated on line 302 and stored into 'theAudio' is no longer referenced after this point and has a retain count of +1 (object leaked)*** 


     cat1.hidden = 0; 
     cat.hidden = 1; 
     [cat1 startAnimating]; 
     cat.center = cat1.center; 
     [self performSelector:@selector(loadAnimations) withObject:nil afterDelay:1.0]; 
     catLabel.hidden = 0; 
    } 

Répondre

6

Here est la solution pour le même problème posé jusqu'à présent.

- (AVAudioPlayer *)audioPlayerWithContentsOfFile:(NSString *)path { 
     NSData *audioData = [NSData dataWithContentsOfFile:path]; 
     AVAudioPlayer *player = [AVAudioPlayer alloc]; 
     if([player initWithData:audioData error:NULL]) { 
      [player autorelease]; 
     } else { 
      [player release]; 
      player = nil; 
     } 
     return player; 
    } 

Et pour une meilleure idée que vous pouvez suivre this.

+0

Merci beaucoup, ça l'a réparé! – ftwhere

0

Vous devez indiquer une variable d'instance pour l'instance de classe AVAudioPlayer.

//Instance variable: 
{ 
    AVAudioPlayer* theAudio; 
} 

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

    cat1.hidden = 0; 
    cat.hidden = 1; 
    [cat1 startAnimating]; 
    cat.center = cat1.center; 
    [self performSelector:@selector(loadAnimations) withObject:nil afterDelay:1.0]; 
    catLabel.hidden = 0; 
}