我正在开发卡拉OK录音之类的应用程序。当耳机未插入设备时,我能够记录包括背景音轨在内的整个音频流。但是当耳机插入设备时,它仅在录制声音流,不包括背景音轨。我想录制包括背景音轨在内的人声流。
我的代码是:
//For Recording
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSetting error:&error];
audioRecorder.delegate=self;
AVAudioSession* session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord
error:&error];
[session overrideOutputAudioPort:AVAudioSessionPortOverrideNone
error:&error];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride),&audioRouteOverride);
[audioRecorder prepareToRecord];
[audioRecorder record];
// Playing Karaoke video file
NSURL *movieURL = [NSURL fileURLWithPath: [[CommonUISetting applicationDocumentsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",songInfo.fileName]]] ;
movieController=[[MPMoviePlayerController alloc]initWithContentURL:movieURL];
movieController.controlStyle = MPMovieControlStyleNone;
[movieController.view setFrame:CGRectMake(0, 0, viewPlayer.frame.size.width, viewPlayer.frame.size.height)];
[ movieController play];
[movieController.view setBackgroundColor:[UIColor clearColor]];
[viewPlayer addSubview:movieController.view];
提前致谢
最佳答案
要记录麦克风输入和音乐曲目,该应用必须使用允许原始音频样本捕获(音频队列或在AVAudioEngine上的轻击等)的API之一播放该轨道,然后将捕获的数据与记录的输入进行混合在将混音保存到文件之前,请从选定的麦克风中选择。
关于ios - 插入耳机时如何使用背景音轨录制语音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27397221/