Сбой AVAudioPlayer после обновления до iOS 5.0 - PullRequest
0 голосов
/ 08 ноября 2011

Я успешно использовал класс AVAudioPlayer, но после обновления до iOS 5.0 мое приложение iPad вылетает при попытке прослушать то, что я ранее записал.Под iOS 4.3 все хорошо.

Вот код:

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

Приложение вылетает в этой строке:

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

Я уже попробовал NSZombieEnabled, чтодает мне:

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

1 Ответ

4 голосов
/ 08 ноября 2011

измените эти начальные строки на:

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

Я также дал вам код проверки ошибок.

...