Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将音频直接录制到NSData_Iphone_Ios_Ipad_Avfoundation - Fatal编程技术网

Iphone 将音频直接录制到NSData

Iphone 将音频直接录制到NSData,iphone,ios,ipad,avfoundation,Iphone,Ios,Ipad,Avfoundation,我需要录制音频并将其直接发送到NSData,我曾尝试使用AVAudioRecorder进行同样的操作,但不幸的是,它将录制的音频保存到了一个文件中,并且将音频保存到文件中然后将其读取到NSData的过程需要花费大量时间,请帮助进行一些编码,我可以将音频直接保存到NSData对象中,提前谢谢 我的录音代码: ` `API不允许您这样做。你花了多长时间阅读这个文件?我看到的唯一方法是定期对文件进行极化,并在录制过程中将添加到文件中的任何内容添加到NSMU可变数据中(每次添加文件大小-可变数据大小)。

我需要录制音频并将其直接发送到NSData,我曾尝试使用AVAudioRecorder进行同样的操作,但不幸的是,它将录制的音频保存到了一个文件中,并且将音频保存到文件中然后将其读取到NSData的过程需要花费大量时间,请帮助进行一些编码,我可以将音频直接保存到NSData对象中,提前谢谢

我的录音代码: `


`API不允许您这样做。你花了多长时间阅读这个文件?我看到的唯一方法是定期对文件进行极化,并在录制过程中将添加到文件中的任何内容添加到NSMU可变数据中(每次添加文件大小-可变数据大小)。

我现在也希望这样做。我偶然发现了音频队列服务——这是一个显然允许将音频直接录制到对象中的API

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

参见文档:

也许还有其他api,也许实现的api允许我这么做!!您还可以查看音频队列和声音流(核心音频的一部分),当新数据可用时,您将收到回调。但这有点复杂…这是一个合理的答案,我不反对这是如此复杂@在一天的工作中,一切都很好。嗯,可能需要3天的工作时间。我还需要快速将录制转换为NSData,以便作为二进制字节的属性,我可以将录制保存为核心数据。我找到的唯一解决方案是使用音频队列。对你来说,这也是唯一的解决办法。因为AVAudioRecorder无法做到这一点,所以你应该做一些CoreAudio并通过回调接收数据块,然后将它们传递给你的数据库。是的,根据你的建议,我采取了不同的方法。我将录音保存到文件中,然后将这些文件的url作为字符串保存到核心数据中。现在一切都很好。谢谢你,弗洛里克。
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];