同步口譯app的開發過程
iPhone
即時語音辨識用內建的就很強
private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "zh-TW"))!
// private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "ja-JP"))!
// private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US"))!
// private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "th-TH"))!
// private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "vi-VN"))!
// private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "ko-KR"))!
private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
private var recognitionTask: SFSpeechRecognitionTask?
private let audioEngine = AVAudioEngine()
speechRecognizer.delegate = self
// Asynchronously make the authorization request.
SFSpeechRecognizer.requestAuthorization
可以參考xcode中的範例 SpokenWord
然後,翻譯的部分,我先嚐試用 google mlkit的翻譯,但翻譯品質很差,我打算改用google 雲端付費的api來試試,每月有50萬字的免費額度
google mlkit translate的作法
要先引用套件
pod 'GoogleMLKit/Translate', '4.0.0'
然後使用的方法是
var mytr: Translator?
let options = TranslatorOptions(sourceLanguage: .chinese, targetLanguage: .japanese)
mytr = Translator.translator(options: options)
let conditions = ModelDownloadConditions(
allowsCellularAccess: false,
allowsBackgroundDownloading: true
)
mytr?.downloadModelIfNeeded(with: conditions) { error in
guard error == nil else { return }
print("model ok")
// Model downloaded successfully. Okay to start translating.
}
self.mytr?.translate(self.tv_talk.text) { translatedText, error in
guard error == nil, let translatedText = translatedText else { return }
self.tv_my.text=translatedText
}
同樣也有範例參考 TranslateExample
沒有留言:
張貼留言