从AVAssetReaderOutput读取数据时,iOS 5.0崩溃

时间:2011-10-18 19:11:32

标签: iphone ios audio avfoundation ios5

我有这段代码用于从AVAssetReaderOutput读取数据,该方法在iOS 4.0中运行正常,但是在5.0中它以糟糕的访问方式崩溃到底,不知道为什么,任何人都有任何输入?

AVAssetReaderOutput *output=[myOutputs objectAtIndex:0];
 int totalBuff=0;
while(TRUE)
{
     CMSampleBufferRef ref=[output copyNextSampleBuffer];
    if(ref==NULL)
        break;
    //copy data to file
    //read next one
    AudioBufferList audioBufferList;
    NSMutableData *data=[[NSMutableData alloc] init];
    CMBlockBufferRef blockBuffer;
    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(ref, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);

for( int y=0; y<audioBufferList.mNumberBuffers; y++ )
{
    AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
    Float32 *frame = audioBuffer.mData;


    NSLog(@"Gonna write %d", audioBuffer.mDataByteSize);
    //crashes here
    [data appendBytes:frame length:audioBuffer.mDataByteSize];



}

totalBuff++;
CFRelease(blockBuffer);
CFRelease(ref);


   [fileHandle writeData:data];
    [data release];
}

由于

丹尼尔

1 个答案:

答案 0 :(得分:9)

我实际上是通过检查blockBuffer是否为null来解决这个问题,并且如果是,则问题是ref不是null但是blockBuffer是这样的代码修复了我的问题

-(void)doExportSong:(NSURL*)url toFileUrl:(NSString*)fileURL 
{
    AVURLAsset *asset=[[[AVURLAsset alloc] initWithURL:url options:nil] autorelease];
    AVAssetReader *reader=[[[AVAssetReader alloc] initWithAsset:asset error:nil] autorelease];
    [reader setTimeRange:CMTimeRangeMake(kCMTimeZero, kCMTimePositiveInfinity)];
    NSMutableArray *myOutputs =[[NSMutableArray alloc] init];
    for(id track in [asset tracks])
    {
        AVAssetReaderTrackOutput *ot=[AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:track outputSettings:nil];

        [myOutputs addObject:ot]; 
        [reader addOutput:ot];
    }
    [reader startReading];
    NSFileHandle *fileHandle ;
    NSFileManager *fm=[NSFileManager defaultManager];
    if(![fm fileExistsAtPath:fileURL])
    {
        [fm createFileAtPath:fileURL contents:[[[NSData alloc] init] autorelease] attributes:nil];
    }
    fileHandle=[NSFileHandle fileHandleForUpdatingAtPath:fileURL];    
    [fileHandle seekToEndOfFile];

    AVAssetReaderOutput *output=[myOutputs objectAtIndex:0];

    int totalBuff=0;
    BOOL one=TRUE;
    while(TRUE)
    {
        CMSampleBufferRef ref=[output copyNextSampleBuffer];
        // NSLog(@"%@",ref);
        if(ref==NULL)
            break;
        //copy data to file
        //read next one
        AudioBufferList audioBufferList;
        NSMutableData *data=[[NSMutableData alloc] init];
        CMBlockBufferRef blockBuffer;
        CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(ref, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
        // NSLog(@"%@",blockBuffer);

        if(blockBuffer==NULL)
        {

                [data release];
                continue;

        }
        if(&audioBufferList==NULL)
        {
            [data release];
            continue;
        }

        for( int y=0; y<audioBufferList.mNumberBuffers; y++ )
        {
            AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
            Float32 *frame = (Float32*)audioBuffer.mData;


            [data appendBytes:frame length:audioBuffer.mDataByteSize];



        }

        totalBuff++;

        CFRelease(blockBuffer);
        CFRelease(ref);
        ref=NULL;
        blockBuffer=NULL;
        [fileHandle writeData:data];
        [data release];
    }

    [fileHandle closeFile];
    [myOutputs release];  
}