升级到iOS 5.0后AVAudioPlayer崩溃

时间:2011-11-08 15:40:16

标签: iphone objective-c ipad avaudioplayer exc-bad-access

我成功使用了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 个答案:

答案 0 :(得分:4)

将这些初始行更改为:

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

我也给了你一些错误检查代码。