gpt4 book ai didi

iphone - 从 AVAssetReaderOutput 读取数据时 iOS 5.0 崩溃

转载 作者:可可西里 更新时间:2023-11-01 06:23:48 25 4
gpt4 key购买 nike

我有这段代码用于从 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];
}

谢谢

丹尼尔

最佳答案

我实际上通过检查 blockBuffer 是否为 null 并继续检查是否为 null 来解决此问题,问题是 ref 不为 null 但 blockBuffer 为 null 所以这段代码解决了我的问题

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

关于iphone - 从 AVAssetReaderOutput 读取数据时 iOS 5.0 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7812394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com