AVAudioPlayer se bloquea después de actualizar a iOS 5.0

I was successfully using the AVAudioPlayer class, but after upgrading to iOS 5.0 my iPad app crashes when trying to listen to what I've previously recorded. Under iOS 4.3 all is well.

Aquí está el código:

   NSError * error = NULL;
   player =[[AVAudioPlayer alloc] initWithContentsOfURL: 
                             [NSURL fileURLWithPath:destinationString] 
                             error:&error]; 

   if(player == NULL)
    NSLog( @"error in creating AVAudioPlayer - %@ %@", [error domain], [error localizedDescription] );

         [player prepareToPlay];
         player.delegate = self;
         [player play]; 

- (NSString*) documentsPath
{
    NSArray *searchPaths =
    NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* _documentsPath = [searchPaths objectAtIndex: 0];  
    return _documentsPath;
}

-(void) initRecord
{
    audioSession = [[AVAudioSession sharedInstance] retain];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
    [audioSession setActive:YES error:nil];

    destinationString = [[self documentsPath]
                         stringByAppendingPathComponent:@"myRecording.caf"];
    NSLog(@"%@", destinationString);
    NSURL *destinationURL = [NSURL fileURLWithPath: destinationString];

    NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                              [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                              [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                              [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                              nil];
    NSError *error;
    recorder = [[AVAudioRecorder alloc] initWithURL:destinationURL settings:settings error:&error];
    recorder.delegate = self;
}

The app crashes at this line:

 player =[[AVAudioPlayer alloc] initWithContentsOfURL: 
                             [NSURLfileURLWithPath:destinationString] 
                             error:NULL]; 

I've already tried NSZombieEnabled that gives me:

0x5368b0 <myRecording = 005BD7D0 | Tag = -1>
*** -[NSPathStore2 length]: message sent to deallocated instance 0x5290e0

preguntado el 08 de noviembre de 11 a las 15:11

1 Respuestas

change those initial lines to:

-(void) playRecording
{
    AVAudioPlayer * player = NULL;

    if(destinationString && ([destinationString length] > 15)
    {
        NSError * error = NULL;
        player =[[AVAudioPlayer alloc] initWithContentsOfURL: 
                             [NSURL fileURLWithPath:destinationString] 
                             error:&error]; 

        if(player == NULL)
        {
            NSLog( @"error in creating AVAudioPlayer - %@ %@", [error domain], [error localizedDescription] );
        } else {
            [player prepareToPlay];
            player.delegate = self;
            [player play]; 
            // F.Y.I. -- this player NEEDS to be released when it is done playing
            // 
            // right now you appear to be leaking memory from both AVAudioPlayer and AVAudioRecorder
        }
    } else {
        NSLog( @"destination string doesn't exist or is invalid");
    }
}

I also gave you some error checking code too.

respondido 08 nov., 11:23

I'll test it and let you know the results. Thanks for your help :-) - Neowinston

Hi Michael. Now I got these two errors: error in creating AVAudioPlayer - (null) (null) & -[NSPathStore2 length]: message sent to deallocated instance 0x59b9340. I don't know why but after upgrading to iOS 5 there is this error in creating the AVAudioPlayer. - Neowinston

Could you update your original question with the new code and let me (or anyone else) see what's going on in context? - Michael Dautermann

Sure, I just did it and have also shared the initRecord method. - Neowinston

And I've updated my code as well. I have a strong feeling your autoreleased "destinationString" is null. - Michael Dautermann

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.