gpt4 book ai didi

iphone - 将音频直接录制到 NSData

转载 作者:行者123 更新时间:2023-11-28 17:55:36 24 4
gpt4 key购买 nike

我需要录制音频并将其直接获取到 NSData,我尝试用 AVAudioRecorder 做同样的事情,但不幸的是它将录音保存到文件中,并且将音频保存到文件中然后将其读取到文件中的过程需要很长时间NSData,请帮助进行一些编码,以便我可以将音频直接保存到 NSData 对象,在此先感谢。

我的录音代码:`

printf("\nstart recording ...");
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];
[recordSettings setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSettings setValue:[NSNumber numberWithFloat:11400.0] forKey:AVSampleRateKey];
[recordSettings setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[recordSettings setValue :[NSNumber numberWithInt:8] forKey:AVLinearPCMBitDepthKey];
[recordSettings setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSettings setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
NSArray *arrayTempDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *soundFilePath = [[arrayTempDir objectAtIndex:0] stringByAppendingString: @"/sound.caf"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:soundFilePath error:nil];
NSLog(@"\n%@",soundFilePath);
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
NSError *error = nil;
audioRecorder = [[ AVAudioRecorder alloc] initWithURL:newURL settings:recordSettings error:&error];
NSLog(@"%@",error);
[audioRecorder setDelegate:self];
if ([audioRecorder prepareToRecord] == YES)
{
audioRecorder.meteringEnabled = YES;
[audioRecorder record];
[NSTimer scheduledTimerWithTimeInterval:RECORDING_METERING target:self selector:@selector(stopRecording) userInfo:nil repeats:NO];
}
[recordSettings release];

`

最佳答案

我现在正打算做同样的事情。我偶然发现了音频队列服务——这是一个显然允许将音频直接录制到对象中的 API。

引自文档:
当您使用音频队列服务录制时,目标可以是任何东西——磁盘文件、网络连接、内存中的对象,等等。

查看文档:
http://developer.apple.com/library/ios/#documentation/MusicAudio/Conceptual/AudioQueueProgrammingGuide/AQRecord/RecordingAudio.html#//apple_ref/doc/uid/TP40005343-CH4-SW1

关于iphone - 将音频直接录制到 NSData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7739863/

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