簡體   English   中英

播放聲音時錄制音頻

[英]Record audio while playing a sound

是否可以使用AVAudioRecorder錄制音頻並同時使用AVAudioPlayer播放另一個音頻文件?

iOS 6.我可以在iOS 5.x中自行完成這個問題。 沒有關於5.x之前的版本的信息。

為此,必須將AudioSession類別設置為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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM