Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

iphone - iOS 5.0 crash when reading data from an AVAssetReaderOutput

I have this snippet of code used to read data from an AVAssetReaderOutput, the method works fine in iOS 4.0, however in 5.0 it crashes towards the end with bad access, not sure why, anyone have any input?

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

Thanks

Daniel

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I actually fixed this by checking that blockBuffer was null and continuing if it was, the problem was that ref was not null but the blockBuffer was so this code fixed my issue

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...