的iOS 5.0的崩溃从AVAssetReaderOutput读取数据时 [英] iOS 5.0 crash when reading data from an AVAssetReaderOutput

查看:863
本文介绍了的iOS 5.0的崩溃从AVAssetReaderOutput读取数据时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个片段code的用于读取来自数据 AVAssetReaderOutput ,该方法的iOS 4.0工作得很好,但是在5.0就接近尾声与崩溃糟糕的访问,不知道为什么,任何人有任何输入?

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

感谢

丹尼尔

推荐答案

我居然通过检查blockBuffer为空并继续如果是固定这一点,问题是,裁判是不为空,但blockBuffer是那么这个$ C $ ç固定我的问题。

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

这篇关于的iOS 5.0的崩溃从AVAssetReaderOutput读取数据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆