我尝试使用AVAudioRecorder
录制音频,正如许多教程中所解释的那样。
我没有收到任何错误,但记录后我的.caf文件仍然是空的。
启动录音机的代码:
-(void)startRecordAudio:(NSString*)name {
Shortcut *shortcut = XAppDelegate.currentShortcut;
NSString *filePathWav = [shortcut getTmpFile:name];
NSString *filePathCaf = [filePathWav stringByReplacingOccurrencesOfString:@".wav" withString:@".caf"];
NSURL *tmpFileUrl = [NSURL fileURLWithPath:filePathCaf];
// AUDIO SESSION
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err;
[audioSession setCategory :AVAudioSessionCategoryRecord error:&err];
if (err) {
[self log:[err localizedDescription]];
return;
}
err = nil;
[audioSession setActive:YES error:&err];
if(err){
[self log:[err localizedDescription]];
return;
}
if (!audioSession.isInputAvailable) {
[self log:@"Le microphone n'est pas disponible. Veuillez réessayer"];
return;
}
//AUDIO RECORDER
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: AVAudioQualityMedium], AVEncoderAudioQualityKey,
[NSNumber numberWithInt: kAudioFormatLinearPCM], AVFormatIDKey,
[NSNumber numberWithFloat:44100.0], AVSampleRateKey,
[NSNumber numberWithInt:16],AVEncoderBitRateKey,
[NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
nil];
NSError *error = nil;
_recorder = [[AVAudioRecorder alloc] initWithURL:tmpFileUrl settings:recordSettings error:&error];
if (error) {
[self log:[error localizedDescription]];
}
[_recorder prepareToRecord];
[_recorder record];
}
并停止录音:
-(void)stopRecordAudio {
[_recorder stop];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
NSURL *inUrl = _recorder.url;
NSURL *outUrl = [NSURL URLWithString:[[NSString stringWithFormat:@"%@", inUrl] stringByReplacingOccurrencesOfString:@".caf" withString:@".wav"]];
[AudioConverter convertFile:inUrl toWav:outUrl];
NSLog(@"Audio data : %@", [NSString stringWithContentsOfURL:inUrl encoding:NSUTF8StringEncoding error:nil]); // Result : (null)
}