我正在使用音频单元录制我们的声音。在我的回调函数中,我在AudioBufferList中获取数据。我必须将此缓冲区存储在一个文件中。
问题是;当我尝试将缓冲区存储在.caf文件中时,应用程序崩溃了。以下是代码。
AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate = 44100.00;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kAudioFormatFlagsCanonical;
audioFormat.mBytesPerPacket = 2;
audioFormat.mFramesPerPacket = 1;
audioFormat.mBytesPerFrame = 2;
audioFormat.mChannelsPerFrame = 1;
audioFormat.mBitsPerChannel = 16;
audioFormat.mReserved = 0;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
if(![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory])
[[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"audio.caf"];
if(![[NSFileManager defaultManager] fileExistsAtPath:path])
NSLog(@"file not exist.");
NSLog(@"Path : %@", path);
THIS->url = [NSURL fileURLWithPath:[path retain]];
err = ExtAudioFileCreateWithURL((CFURLRef)THIS->url, kAudioFileCAFType, &audioFormat, NULL, kAudioFileFlags_EraseFile, &THIS->fOutputAudioFile);
if(err != noErr){
printf("create with url: error %d\n", (int)err);
}
THIS->state = 2;
err = ExtAudioFileSetProperty(THIS->fOutputAudioFile, kExtAudioFileProperty_ClientDataFormat, (UInt32)sizeof(audioFormat), &audioFormat);
if(err != noErr){
printf("file set property: error %d\n", (int)err);
}
err = ExtAudioFileWriteAsync(THIS->fOutputAudioFile, 0, NULL);
if(err != noErr){
printf("write async: error %d\n", (int)err);
return err;
}
[pool release];
err = ExtAudioFileWriteAsync(THIS->fOutputAudioFile, ioData->mNumberBuffers, ioData);
if(err != noErr){
printf("file write async: error %d\n", (int)err);
return err;
}
ERROR:
Program received signal: “EXC_BAD_ACCESS”.
#0 0x30d04bb2 in memmove ()
#1 0x33ba1d1e in AudioRingBuffer::Store ()
#2 0x33c2269e in ExtAudioFile::WriteFramesAsync ()
#3 0x33c278c8 in ExtAudioFileWriteAsync ()
#4 0x00002e74 in PerformThru (inRefCon=0x14d9f0, ioActionFlags=0x2ffe77d4, inTimeStamp=0xa896fc, inBusNumber=0, inNumberFrames=256, ioData=0x14f680) at /Alex/project/ProStudio/TestAudioUnit/Classes/TestAudioUnitAppDelegate.mm:399
#5 0x33b70896 in AUInputElement::PullInput ()
#6 0x33b794a0 in AUInputFormatConverter2::InputProc ()
#7 0x33b5560e in AudioConverterChain::CallInputProc ()
#8 0x33b5555e in AudioConverterChain::FillBufferFromInputProc ()
#9 0x33b5537c in BufferedAudioConverter::GetInputBytes ()
#10 0x33b79390 in CBRConverter::RenderOutput ()
#11 0x33b5505a in BufferedAudioConverter::FillBuffer ()
#12 0x33b55362 in BufferedAudioConverter::GetInputBytes ()
#13 0x33b79390 in CBRConverter::RenderOutput ()
#14 0x33b5505a in BufferedAudioConverter::FillBuffer ()
#15 0x33b551ae in AudioConverterChain::RenderOutput ()
#16 0x33b5505a in BufferedAudioConverter::FillBuffer ()
#17 0x33b54e2a in AudioConverterFillComplexBuffer ()
#18 0x33b78f58 in AUConverterBase::RenderBus ()
#19 0x33c08eea in AURemoteIO::RenderBus ()
#20 0x33b5666e in AUBase::DoRender ()
#21 0x33c09698 in AURemoteIO::PerformIO ()
#22 0x33c09962 in AURIOCallbackReceiver_PerformIO ()
#23 0x33c02448 in _XPerformIO ()
#24 0x33b71bea in mshMIGPerform ()
#25 0x33bd7de0 in MSHMIGDispatchMessage ()
#26 0x33c0ebae in AURemoteIO::IOThread::Entry ()
#27 0x33b4a1d8 in CAPThread::Entry ()
#28 0x30d7d88c in _pthread_start ()
#29 0x30d72a90 in thread_start ()
请帮助某人解决此问题。
答案 0 :(得分:0)
我得到了这个问题的解决方案;
问题是;我正在为音频单元和ExtAudioFileCreateWithURL设置AudioStreamBasicDescription。它有一些不同的设置。我改变了一样。现在工作正常。
以前的ASBD是;
audioFormat.mSampleRate = 44100.00;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kAudioFormatFlagsCanonical | (kAudioUnitSampleFractionBits << kLinearPCMFormatFlagsSampleFractionShift) | kAudioFormatFlagIsNonInterleaved;
audioFormat.mChannelsPerFrame = 2;
audioFormat.mFramesPerPacket = 1;
audioFormat.mBitsPerChannel = 8 * sizeof(AudioUnitSampleType);
audioFormat.mBytesPerPacket = sizeof(AudioUnitSampleType);
audioFormat.mBytesPerFrame = sizeof(AudioUnitSampleType);;
audioFormat.mReserved = 0;
现在我正在使用ASBD;
AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate = 44100.00;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioFormat.mFramesPerPacket = 1;
audioFormat.mChannelsPerFrame = 1;
audioFormat.mBitsPerChannel = 16;
audioFormat.mBytesPerPacket = 2;
audioFormat.mBytesPerFrame = 2;
audioFormat.mReserved = 0;