SpeechRecognizer后无法播放视频>>

问题描述 投票:0回答:1

[SpeechRecognizer后无法播放视频。没有得到任何错误,只是停留在AVPlayerViewController上。我也停止了SpeechRecognizer。然后我尝试播放视频之后。该视频可以在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()
}

[SpeechRecognizer后无法播放视频。没有得到任何错误,只是停留在AVPlayerViewController上。我也停止了SpeechRecognizer。然后我尝试播放视频之后。完美的视频...

ios swift avplayer avaudiosession sfspeechrecognizer
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.