谁能告诉我如何获取音频记录的数据?我想用不同的数据保存记录文件的数量。我正在做的是访问 url 、目录路径并重命名它们,但结果它们以不同的名称保存,但没有使用不同的数据。所有文件都在播放我录制的最后一个声音。
源代码:-
- (IBAction) startRecording
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
[audioSession setMode:AVAudioSessionModeVoiceChat error:&err];
if(err)
{
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
[audioSession setActive:YES error:&err];
err = nil;
if(err)
{
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
recordSetting = [[NSMutableDictionary alloc] init];
// We can use kAudioFormatAppleIMA4 (4:1 compression) or kAudioFormatLinearPCM for nocompression
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
// We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
// We can use 2(if using additional h/w) or 1 (iPhone only has one microphone)
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[recordSetting setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
[recordSetting setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setObject:[NSNumber numberWithInt: AVAudioQualityMax] forKey: AVEncoderAudioQualityKey];
recorderFilePath = [NSString stringWithFormat:@"%@/My Sound.caf",DOCUMENTS_FOLDER];
url = [NSURL fileURLWithPath:recorderFilePath];
err = nil;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
soundData = [[NSData alloc] initWithContentsOfURL: url];
NSLog(@"soundData:%@",soundData);
NSLog(@"url:%@",url);
err = nil;
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
if(!recorder)
{
NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: @"Warning"
message: [err localizedDescription]
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
return;
}
//prepare to record
[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
BOOL audioHWAvailable = audioSession.inputIsAvailable;
if (! audioHWAvailable)
{
UIAlertView *cantRecordAlert =
[[UIAlertView alloc] initWithTitle: @"Warning"
message: @"Audio input hardware not available"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[cantRecordAlert show];
}
// start recording
[recorder recordForDuration:(NSTimeInterval) 60];
Tag=30;
}
- (IBAction)saveRecording
{
if([recordingNameTextField.text isEqualToString: @""])
{
[alertView setMessage:@"Recording File Name not set.\n\n"];
[alertView show];
}
else
{
recorderFilePath = [NSString stringWithFormat:@"%@/My Sound.caf",DOCUMENTS_FOLDER];
NSLog(@"%@",recorderFilePath);
NSLog(@"%@",url);
renameString =[NSString stringWithFormat:@"%@/%@.caf",DOCUMENTS_FOLDER,recordingNameTextField.text];
NSLog(@"%@",renameString);
url2 = [NSURL fileURLWithPath:renameString];
NSLog(@"%@",url2);
NSFileManager *fm = [NSFileManager defaultManager];
NSError *renameError;
[fm moveItemAtURL:url toURL:url2 error:&renameError];
[recordingFile checkAndCreateDatabase];
[recordingFile addRecord:1 andname:recordingNameTextField.text];
[recordingFile readDatabase];
recordingNameTextField.text = @"";
NSLog(@"Text Field: %@",recordingNameTextField.text);
saveRecording.userInteractionEnabled=NO;
}
请帮忙。谢谢:)