Изтича AVAudioPlayer?

Имам проблем и не знам как да се справя с него. Уморително е да създадете приложение със звук и анимация. Дава ми предупреждение за памет от ниво 1 и след това от ниво 2. Опитах да изградя и анализирам и получих всички тези потенциални течове. Ако пусна звука, звукът няма да се възпроизведе. Някакви съвети?

Ето част от кода и течовете, които имам

    - (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;
    }

person ftwhere    schedule 28.08.2011    source източник


Отговори (2)


Ето решението за същия проблем питани досега.

    - (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;
    }

И за по-добра представа можете да следвате това.

person alloc_iNit    schedule 28.08.2011

Трябва да посочите променлива на екземпляр за екземпляра на класа 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;
}
person waterforest    schedule 10.05.2012