【问题标题】:Record audio while playing a sound播放声音时录制音频
【发布时间】:2013-04-10 16:00:56
【问题描述】:

是否可以使用 AVAudioRecorder 录制音频并同时使用 AVAudioPlayer 播放另一个音频文件?

【问题讨论】:

  • 您想使用来自AVAudioPlayer 的回放作为录制的输入源吗?

标签: iphone ios avaudioplayer avaudiorecorder


【解决方案1】:

在 iOS 6 上是可能的。我自己在 iOS 5.x 中这样做时遇到了问题。没有关于 5.x 之前版本的信息。

为此,必须将 AudioSession-category 设置为 AVAudioSessionCategoryPlayAndRecord

NSError *error = nil;
NSLog(@"Setting category for AudioSession to AVAudioSessionCategoryPlayAndRecord");
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
if (!success) {
    NSLog(@"Error setting category for AudioSession:\n\terror: %@", error.localizedDescription);
}

AVAudioRecorder 的设置如下:

NSString *fileToRecordPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"record.m4a"];
NSURL *fileToRecordURL = [NSURL fileURLWithPath:fileToRecordPath];
NSDictionary *settings = @{AVFormatIDKey: [NSNumber numberWithInt:kAudioFormatMPEG4AAC],
                           AVSampleRateKey: [NSNumber numberWithFloat:44100.0],
                           AVNumberOfChannelsKey: [NSNumber numberWithInt:1]};
NSError *error = nil;

AVAudioRecorder *audioRecorder = [[AVAudioRecorder alloc] initWithURL:self.fileURL settings:settings error:&error];
 if (!audioRecorder) {
    NSLog(@"Error initializing AVAudioRecorder:\n\terror: %@", error.localizedDescription);
}
audioRecorder.delegate = self;
[audioRecorder prepareToRecord];

在 AVAudioPlayer 中播放另一个音频文件(相同格式,m4a)时,在 iOS 6 中一切正常。在 iOS 5 中,行为很奇怪。播放音频或音频播放暂停时,无法开始录音。但是,在开始录制时,可以同时播放(另一个文件)。

经过一番搜索,我找不到正确设置它的方法。虽然我不敢相信,但这是 iOS 中的一个错误,但缺少设置。

所以这适用于 iOS 6,但非常感谢对 iOS 5 问题的帮助;o)

【讨论】:

  • 我没有。这是 AVAudioSessionCategory 和 AVAudioRecorder 的设置。只需按照文档使用 AVAudioPlayer 即可。在我看来,iOS 5 中的异常行为如下所述。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-07
相关资源
最近更新 更多