[英]AVAudioRecorder not working on iPhone 5S
因此,我正在使用AVAudioRecorder來與正在錄制視頻的AVCaptureSession一起錄制音頻(我知道這很奇怪,但就我的情況而言,我需要單獨錄制它們)。
除了我的iPhone 5S,其他所有設備都可以正常工作。 它記錄沒有錯誤,但是保存到磁盤的文件已損壞或發生了什么。 當我在Mac上訪問文件系統並嘗試使用VLC或Quicktime播放m4a文件時,出現“無法檢測到文件格式”錯誤。 這是初始化AVAudioRecorder並記錄音頻的方法:
// Prepare the audio session
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
[[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoRecording error:nil];
// Setup audio recording
NSDictionary *recordSettings = @{AVFormatIDKey: @(kAudioFormatMPEG4AAC),
AVEncoderAudioQualityKey: @(AVAudioQualityLow),
AVEncoderBitRateKey: @16,
AVNumberOfChannelsKey: @1,
AVSampleRateKey: @22050.0f};
NSError *audioRecorderError;
NSURL *audioFileURL = [[self.outputFileURL URLByDeletingPathExtension] URLByAppendingPathExtension:@"m4a"];
self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:audioFileURL
settings:recordSettings
error:&audioRecorderError];
self.audioRecorder.delegate = self;
if (audioRecorderError) {
CCLog(@"Error while initializing the audio recorder... Skipping sound recording!");
}
else {
if (![self.audioRecorder prepareToRecord]) {
CCLog(@"Error preparing to record");
}
if (![self.audioRecorder record]) {
CCLog(@"Error recording");
}
}
同樣,這適用於5S以外的所有設備。 有人知道是什么原因造成的嗎?
再多挖一些,我顯然找到了解決方案。 我只是擺脫了AVEncoderBitRateKey,一切正常。 所以現在我的recordSettings字典看起來像這樣:
// Setup audio recording
NSDictionary *recordSettings = @{AVFormatIDKey: @(kAudioFormatMPEG4AAC),
AVEncoderAudioQualityKey: @(AVAudioQualityLow),
AVNumberOfChannelsKey: @1,
AVSampleRateKey: @22050.0f};
仍然不確定為什么只有iPhone 5S會出現這種情況。 同樣,我已經在運行iOS6和iOS7的所有其他設備上進行了測試,並且舊的設置字典可以在5S以外的所有設備上正常運行。 現在,我已經刪除了AVEncoderBitRateKey和value,它也可以在5S上使用。
看起來這實際上是AVNumberOfChannelsKey
和AVEncoderBitRateKey
之間的交互。 當通道數= 1時,我似乎無法記錄高於64000Kbps的速度,但是當它等於2時,我可以使用128000 ...
我遇到了同樣的問題-答案被證明與AVAudioSession有關。 我需要將音頻會話類別設置為PlayAndRecord。 我的應用程序的其他部分將其設置為Ambient。 這篇文章https://stackoverflow.com/a/10287793/1009125給了我答案。 希望這對某人有幫助,我在這個星期天迷路了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.