2011-10-18 28 views
3

我有這段代碼用於從AVAssetReaderOutput讀取數據,該方法在iOS 4.0中正常工作,但是在5.0中它以不良訪問崩潰,不知道爲什麼,任何人有任何輸入?當從一個AVAssetReaderOutput讀取數據時,iOS 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]; 
} 

感謝

丹尼爾

+0

你是如何解決它的?因爲接受的答案我無法解決。 –

+0

問題是ref沒有返回null,但blockBuffer爲null,所以檢查blockBuffer是否爲null,如果它只是繼續回到循環的頂部,我把答案放在下面並使其成爲acceffered回答 – Daniel

+0

非常感謝。你只是節省了幾個小時的搜索!:D –

回答

9

我居然通過檢查blockBuffer爲空,並繼續如果是固定這一點,問題是,裁判是不爲空,但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]; 
} 
+0

超級...我只是複製並粘貼此代碼...它的工作令人驚歎....謝謝很多... –