我試圖創建一個應用程序,記錄用戶的聲音並同時將其轉錄。我使用AVFoundation和Speech框架來完成這項工作。問題在於蘋果將轉錄時間限制爲一分鐘。所以,在這段時間之後,我應該回憶一下語音識別請求。問題是我也想同時錄製聲音。1分鐘後不重新啓動連續語音識別
有誰知道我該如何解決這個問題?
這是我使用的代碼:
private func startRecording() throws {
// Cancel the previous task if it's running.
if let recognitionTask = recognitionTask {
recognitionTask.cancel()
self.recognitionTask = nil
}
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: .allowBluetoothA2DP)
try audioSession.setMode(AVAudioSessionModeMeasurement)
try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
guard let inputNode = audioEngine.inputNode else { fatalError("Audio engine has no input node") }
guard let recognitionRequest = recognitionRequest else { fatalError("Unable to created a SFSpeechAudioBufferRecognitionRequest object") }
// Configure request so that results are returned before audio recording is finished
recognitionRequest.shouldReportPartialResults = true
// A recognition task represents a speech recognition session.
// We keep a reference to the task so that it can be cancelled.
recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest) { result, error in
var isFinal = false
if result != nil {
if let result = result {
self.textView.text = result.bestTranscription.formattedString
}
isFinal = (result?.isFinal)!
if isFinal == true{
self.textView.text.append((result?.bestTranscription.formattedString)!)
}
}
if error != nil || isFinal {
print("Error: \(error)")
print("ifFinal: \(isFinal)")
self.audioEngine.stop()
inputNode.removeTap(onBus: 0)
self.recognitionRequest = nil
self.recognitionTask = nil
try! self.startRecording()
self.recordButton.isEnabled = true
self.recordButton.setTitle("Start Recording", for: [])
}
}
let recordingFormat = inputNode.outputFormat(forBus: 0)
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
DispatchQueue.main.async {
self.recognitionRequest?.append(buffer)
self.writeBuffer(buffer)
}
}
if !audioEngine.isRunning {
audioEngine.prepare()
try audioEngine.start()
}
}
正如你可以從代碼中看到,我提出的要求,我寫在installTap方法的音頻文件。因此,每次我必須重新開始錄音時,我還要刪除巴士上的水龍頭。通過這種方式,我無法繼續錄製音頻文件。
有什麼我可以做的嗎?任何解決方案備擇方案?
你有答案嗎? –