简体   繁体   中英

Record audio straight to NSData

I need to record audio and get it directly to NSData, I tried to do the same with AVAudioRecorder but unfortunately it saves the recording to a file, and the process takes to much time for saving audio in file and then reading it to NSData, please help with some coding where i could save the audio directly to NSData object, thanks in advance.

My recording code: `

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

`

I'm looking to do the same thing right now. I stumbled across Audio Queue Services -- this is an API that apparently allows recording audio straight into objects.

Quote from Docs:
When you record using Audio Queue Services, the destination can be just about anything—an on-disk file, a network connection, an object in memory , and so on.

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

The API won't allow you to do it. How long does it take you to read the file? The only way I see to do it is to regularly pole the file and add whatever has been added to it to an NSMutableData as the recording goes on (adding size_of_file - size_of_mutable_data every time).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM