像Siri一樣監聽說話

打開info Speech權限

Swift 語音 轉 文字 錄音 siri 監聽

創建Button+TextView

檢查用戶權限

SFSpeechRecognizer.requestAuthorization { (authStatus) in
    var isButtonEnabled = false

    switch authStatus {
    case .authorized:
        isButtonEnabled = true

    case .denied:
        isButtonEnabled = false
        print("用戶拒絕接受語音識別")

    case .restricted:
        isButtonEnabled = false
        print("語音識別功能沒有經過認可")

    case .notDetermined:
        isButtonEnabled = false
        print("當前設備不能語音識別")

    @unknown default:
        print("錯誤")
    }
}

AVAudioSessionMode

AudioSessionMode兼容category應用場景
DefaultAll Category默認音頻會話模式
VoiceChatPlayAndRecordVoIP(雙向語音通信)
GameChatPlayAndRecord遊戲錄制(Game Kit)
VideoRecordingRecord、PlayAndRecord錄制視頻
MeasurementPlayback、Record、PlayAndRecord音頻輸入或輸出的測量
MoviePlaybackPlayback視頻播放
VideoChatPlayAndRecord視頻通話
SpokenAudioPlayback、SoloAmbient、PlayAndRecord、MultiRoute有聲讀物
VoicePrompt
func startRecording() {

    if recognitionTask != nil {
        recognitionTask?.cancel()
        recognitionTask = nil
    }

    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSession.Category.record)
        try audioSession.setMode(AVAudioSession.Mode.measurement)  // 音頻輸入或輸出的測量
        try audioSession.setActive(true, options: .notifyOthersOnDeactivation)  // 停止其他音樂
    } catch {
        print("audioSession properties weren't set because of an error.")
    }

    // SFSpeechAudioBufferRecognitionRequest
    // SFSpeechURLRecognitionRequest
    recognitionRequest = SFSpeechAudioBufferRecognitionRequest()

    let inputNode = audioEngine.inputNode 

    guard let recognitionRequest = recognitionRequest else {
        fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object")
    }

    recognitionRequest.shouldReportPartialResults = true
    recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in

        var isFinal = false

        if result != nil {
            print(result?.bestTranscription.formattedString)
            self.textView.text = result?.bestTranscription.formattedString
            isFinal = (result?.isFinal)!
        }

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

            self.recognitionRequest = nil
            self.recognitionTask = nil

            self.microphoneButton.isEnabled = true
        }
    })

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

    audioEngine.prepare()

    do {
        try audioEngine.start()
    } catch {
        print("錯誤,audioEngine無法啟動。")
    }

    textView.text = "說點什麼,我在聽!"
}

Demo

Github

https://github.com/Bgihe/Swift-Speech

Swift更多文章

Swift 彈出視窗 AlertController 的使用方法 💥

Swift 判斷螢幕方向 📱

Swift Core Data 實現 💾🔥

Swift UISegmentedControl 💻分段控制器!

Swift 實現抽屜效果 🧹

Categorized in: