[英]iphone: AVAudioRecorder recording always return FALSE
我正在使用 AVAudioRecorder 錄制聲音,但我遇到了問題。
調用方法[recorder record]后,recorder.recording的值總是返回FALSE,所以無法停止錄制。
我在 iOS 5 上使用 XCode 4.2,我的 iPad 在 iOS 5 上也是 ipad 2。
這是我的代碼:
self.currentFileName = [GlobalRecording newRecordingFileName];
NSURL *url = [NSURL fileURLWithPath:[[GlobalRecording recordingDirectory] stringByAppendingPathComponent:currentFileName]];
NSError *err = nil;
AVAudioRecorder *aRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordingSettings error:&err];
if( err ){
NSLog(@"could not create a recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
self.startRecordingTime = [NSDate date];
recordingLength = 0.0f;
// setup recorder and start recording
[aRecorder setDelegate:self];
aRecorder.meteringEnabled = YES;
[aRecorder record];
self.recorder = aRecorder;
BOOL test = recorder.recording;
[aRecorder release];
在 [audioRecord prepareToRecord] 之后嘗試使用 AVAudioSession
NSError *err = nil;
audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
// route to speaker
UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(ASRoute), &ASRoute);
if (err)
{
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
}
[audioSession setActive:YES error:&err];
音頻會話是您的應用和 iOS 之間的中介,用於配置應用的音頻行為。 啟動后,您的應用程序會自動獲取單例音頻會話。
所以在開始錄制之前激活你的會話
對於 Swift 3.0
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch let error as NSError {
print("could not set session category")
print(error.localizedDescription)
}
do {
try session.setActive(true)
} catch let error as NSError
{
print("could not make session active")
print(error.localizedDescription)
}
self.recorder.record() // recorder is my AVAudioRecorder
我有類似的症狀,因為我正在使用一個正在重置AVAudioSession
全局設置的庫進行播放。 每次我想錄制時,我都需要重置設置。
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
try session.setActive(true)
} catch {
return error
}
我正在使用的庫將其重置為AVAudioSessionCategoryPlay
。 如果有錯誤訪問而不是false
讓您知道導致record()
失敗的原因,那當然會很好。
我在模擬器上遇到了同樣的問題,經過調查發現是因為我的電腦沒有設置任何音頻輸入源。 一旦我連接了輸入源(例如,AirPod), record()
最終返回true
並開始工作。 可悲的是, record()
從來沒有給出任何錯誤來指示這種類型的失敗。
當您調用記錄方法時,您是否也看到了延遲? 我在 iPhone4 硬件上使用 iOS5 時遇到了一個問題,其中錄制方法有 3-4 秒的延遲,並且偶爾會無法開始錄制。 我正在按照相同問題的建議調用 prepareToRecord。
這在 iOS4/iPhone4 或 iOS5/iPhone4S 上不是問題……所以這似乎是在舊硬件上運行新操作系統的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.