0
prepareToRecord()
返回false,沒有明顯的原因。其他類似問題的解決方案都不適合我。以下是我所嘗試的:iOS:AVAudioRecorder.prepareToRecord()失敗,沒有錯誤
- 在創建
AVAudioRecorder
之前立即開始記錄會話(成功)。 - 輸出目錄存在並且是可寫的。
- 這些
recordSettings
在我的代碼的早期版本(現在丟失)中爲我工作,但由於某種原因通常錄製已停止。 - 我一直在使用
OpenEars
做語音識別,但是當我發現這個問題時,我從構建中刪除了它。
版本信息:
- 的iOS 9.1
- 的XCode 7.1
- 雨燕2.1
這裏是我的代碼。模擬器和iPhone上的輸出如下。
func setupRecorder(url : String) -> Promise<Void>? {
let (doneRecording, recorderAccept, _) = Promise<Void>.pendingPromise()
let session: AVAudioSession = AVAudioSession.sharedInstance()
if (session.respondsToSelector("requestRecordPermission:")) {
AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in
if granted {
print("record permission granted")
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
try session.setActive(true)
self._setupRecorder(url, recorderAccept: recorderAccept)
} catch {
print(error)
}
} else{
print("record permission not granted")
}
})
}
return doneRecording
}
func _setupRecorder(url: String, recorderAccept:() ->()) {
print(url)
let recordSettings = [AVSampleRateKey : NSNumber(float: Float(44100.0)),
AVFormatIDKey : Int(kAudioFormatMPEG4AAC),
AVNumberOfChannelsKey : NSNumber(int: 1),
AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.High.rawValue))];
do {
let soundRecorder = try AVAudioRecorder(URL: NSURL(fileURLWithPath: url), settings: recordSettings)
soundRecorder.delegate = self
if !soundRecorder.prepareToRecord() {
print("prerecord failed")
return
}
if !soundRecorder.record() {
print("record failed")
return
}
self.soundRecorder = soundRecorder
self.recorderAccept = recorderAccept
} catch let error as NSError {
print("AVAudioRecorder error: \(error.localizedDescription)")
}
}
iPhone輸出:
record permission granted
file:///var/mobile/Containers/Data/Application/1DC339E6-555F-47AA-8D4F-4A8E1E847919/Library/Caches/iM3xWVKCGr.aac
prerecord failed
模擬器輸出:
record permission granted
file:///Users/adrian/Library/Developer/CoreSimulator/Devices/5D035D65-60FA-413E-9CD7-C2C4277EC25F/data/Containers/Data/Application/6DF6374E-3BCF-4BD0-B826-FE1622F134B4/Library/Caches/1ey96W904T.aac
prerecord failed
緩存目錄權限:
drwxr-xr-x 53 adrian admin 1802 Oct 29 14:12 /Users/adrian/Library/Developer/CoreSimulator/Devices/5D035D65-60FA-413E-9CD7-C2C4277EC25F/data/Containers/Data/Application/6DF6374E-3BCF-4BD0-B826-FE1622F134B4/Library/Caches/
我很想聽聽其他人有一個建議。
先前在模擬器和設備上使用上述設置錄製了錄製內容。當我有機會時,我會從頭開始構建一個新的錄製應用程序。 – user2683747
想通了。謝謝,馬特。 – user2683747