gpt4 book ai didi

ios - 语音识别器后视频无法播放

转载 作者:行者123 更新时间:2023-11-30 10:59:08 30 4
gpt4 key购买 nike

在 SpeechRecognizer 之后视频无法播放。没有收到任何错误,只是停留在 AVPlayerViewController 上。我也停止了语音识别器。然后在我尝试播放视频之后。该视频在语音识别器之前完美播放。

也许可能的speechRecognizer并没有被这段代码停止。所以,也许问题出在 stopRecording() 上。

enter image description here

@IBAction func btnRecord(_ sender: Any) {
player.pause()
player.seek(to: CMTime.init(value: 0, timescale: player.currentTime().timescale))

if self.audioEngine.isRunning {
self.audioEngine.stop()
self.recognitionRequest?.endAudio()
}
else {
try! self.startRecording()
}
}

private func startRecording() throws {

// Cancel the previous task if it's running.
if let recognitionTask = recognitionTask {
recognitionTask.cancel()
self.recognitionTask = nil
}

let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(AVAudioSession.Category.record, mode: .default, options: [])
try audioSession.setMode(AVAudioSession.Mode.measurement)
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)

recognitionRequest = SFSpeechAudioBufferRecognitionRequest()

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 let result = result {
self.text = result.bestTranscription.formattedString
self.lblText.text = self.text
isFinal = result.isFinal
}

if error != nil || isFinal {
self.audioEngine.stop()
inputNode.removeTap(onBus: 0)

self.recognitionRequest = nil
self.recognitionTask = nil
}
}

let recordingFormat = inputNode.outputFormat(forBus: 0)
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
self.recognitionRequest?.append(buffer)
}

audioEngine.prepare()

try audioEngine.start()
}

private func stopRecording() {
audioEngine.stop()
recognitionRequest?.endAudio()

if let recognitionTask = recognitionTask {
recognitionTask.cancel()
self.recognitionTask = nil
}
}

@IBAction func btnDonePopup(_ sender: Any) {
self.stopRecording()
self.playVideo()
}

最佳答案

请将audioSession.setCategory更改为默认值:

    if error != nil || isFinal {
self.audioEngine.stop()
inputNode.removeTap(onBus: 0)

self.recognitionRequest = nil
self.recognitionTask = nil

do {
try audioSession.setCategory(.soloAmbient, mode: .measurement, options: [])
} catch { }
}

关于ios - 语音识别器后视频无法播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53607647/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com