我正在从 我的旧帖子中继续这个问题。我使用音频队列、音频缓冲区阅读了很多音频录制和播放的内容,不幸的是,由于时间较短,我妥协并计划使用 AVFondation 框架来录制和播放录音机音频。
我的想法: 创建一个AVRecorder实例并通过麦克风录制,相同的音频计划使用AVAudioPlayer通过 Iphone 扬声器播放(我将使用多个实例)
请把我的大量台词用于录音和播放
-(id)startAudioRecorder:(NSUInteger)viewTag {
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
//Set the general audio session category
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryErr];
//Make the default sound route for the session be to use the speaker
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (doChangeDefaultRoute), &doChangeDefaultRoute);
//Activate the customized audio session
[[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
self.audioRecorder=nil;
//AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
//[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init] ;
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];//kAudioFormatAppleIMA4,kAudioFormatAAC
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
NSString *absolutePath=[self getAbsoluteAudioFilePath:viewTag];
NSError *audioRecorderError=nil;
NSURL *absoluteUrl=[NSURL fileURLWithPath:absolutePath];
self.audioRecorder=[[[AVAudioRecorder alloc] initWithURL:absoluteUrl settings:recordSetting error:&audioRecorderError] autorelease];
NSLog(@"%s audioRecorder created=%@",__func__,self.audioRecorder);
BOOL isDurationAccepted=[self.audioRecorder recordForDuration:1800.0];
BOOL record=[self.audioRecorder record];
[recordSetting release];
recordSetting=nil;
//if(error !=NULL) *error=audioRecorderError;
return nil;
}
-(id)playRecordedAudio:(NSUInteger)viewTag
{
NSURL *absoluteUrl=[NSURL fileURLWithPath:[[self getAbsoluteAudioFilePath:viewTag] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ]];
NSError *audioPlayerError=nil;
recordingView.audioPlayer=[[[AVAudioPlayer alloc] initWithContentsOfURL: absoluteUrl error:&audioPlayerError] autorelease] ;
recordingView.audioPlayer.delegate=self;
[recordingView.audioPlayer prepareToPlay];
recordingView.audioPlayer.numberOfLoops=-1;
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
[recordingView.audioPlayer play];
return nil;
}
上面的代码完全符合我的预期。但是当我正在寻找录制 - >播放时,再次录制(在完成当前播放音频之前)这次当前正在播放来自麦克风(耳机)的音频,而不是通过扬声器播放。
现在的问题是,我无法同时录制和播放。我期待始终通过耳机进行录音(甚至是音频播放),并通过手机扬声器播放多个音频。
请建议我更改上面的代码以摆脱这种情况。您的所有评论都有帮助。