Swift - How can I convert Saved Audio file conversations to Text?

若如初见. 提交于 2019-12-04 09:30:31

I have worked on same things which are working for me.

I have audio file in my project bundle which. So I have written following code to convert audio to text.

let audioURL = Bundle.main.url(forResource: "Song", withExtension: "mov")

let recognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US"))
let request = SFSpeechURLRecognitionRequest(url: audioURL!)

request.shouldReportPartialResults = true

if (recognizer?.isAvailable)! {

    recognizer?.recognitionTask(with: request) { result, error in
        guard error == nil else { print("Error: \(error!)"); return }
        guard let result = result else { print("No result!"); return }

        print(result.bestTranscription.formattedString)
    }
} else {
    print("Device doesn't support speech recognition")
}

First get audio url from where you have store audio file. Then create instance of SFSpeechRecognizer with locale that you have want. Create instance of SFSpeechURLRecognitionRequest which are used to requesting recognitionTask.

recognitionTask will give you result and error. Where result contains bestTranscription.formattedString. formmatedString is your test result of audio file.

If set request.shouldReportPartialResults = true, this will give your partial result of every line speak in audio.

I hope this will help you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!