如何在插入耳机时录制带有背景音轨的语音

时间:2014-12-10 09:18:36

标签: ios objective-c iphone core-audio

我正在开发像卡拉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];

提前致谢

2 个答案:

答案 0 :(得分:2)

要录制麦克风输入和音乐曲目,应用必须使用其中一个允许原始音频采样捕获(音频队列,或点击AVAudioEngine等)的API播放该曲目,然后将捕获的数据与在将混音保存到文件之前,从所选麦克风录制的输入。

答案 1 :(得分:1)

我得到了答案.... @ hotpaw2感谢提示..

在theamazingaudioengine中,我们可以将实例添加为多个源的接收器,这些将自动混合在一起。

谢谢 惊人的音频引擎

这是解决方案:

http://theamazingaudioengine.com/doc/_receiving-_audio.html#Recording