私は、ある言語から別の言語ひっくり返す処理するために一緒にAVSpeechSynthesizerクラスを投げました。これについては、AVSpeechSynthesizer tutorial on NSHipsterがこれを学ぶのに適しています。私は翻訳を手こずっていませんでしたが、あなたはその部分を理解することができます... "hello"を "مرحبا"に翻訳する基本的な翻訳者クラスも作成しました。
@IBAction func translateToArabicAction(_ sender: UIButton) {
// check that there are characters entered in the textField
if (textToTranslateTextField.text?.characters.count)! > 0 {
let translatedText = translator.translate(word: (textToTranslateTextField.text?.lowercased())!)
speechSynthesizer.speak(translatedText, in: Language.arabic.rawValue)
}
}
@IBAction func translateToEnglishAction(_ sender: UIButton) {
// check that there are characters entered in the textField
if (textToTranslateTextField.text?.characters.count)! > 0 {
let translatedText = translator.translate(word: (textToTranslateTextField.text?.lowercased())!)
speechSynthesizer.speak(translatedText, in: Language.english.rawValue)
}
}
音声合成は次のようになります。あなたはおそらくそうのようなボタンにアクションを結び付けたいと思い、
TranslateDemo
はトランスレータを使用するには:あなたはここでプロジェクトを見ることができます:
import AVFoundation
// You can use an enum so you don't have to manually type out character strings. Look them up once and stick them in an enum. From there, you set the language with your enum rather than typing out the string.
enum Language: String {
case english = "en-US"
case arabic = "ar-SA"
}
class Speaker: NSObject {
let synth = AVSpeechSynthesizer()
override init() {
super.init()
synth.delegate = self
}
func speak(_ announcement: String, in language: String) {
print("speak announcement in language \(language) called")
prepareAudioSession()
let utterance = AVSpeechUtterance(string: announcement.lowercased())
utterance.voice = AVSpeechSynthesisVoice(language: language)
synth.speak(utterance)
}
private func prepareAudioSession() {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, with: .mixWithOthers)
} catch {
print(error)
}
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
}
func stop() {
if synth.isSpeaking {
synth.stopSpeaking(at: .immediate)
}
}
}
extension Speaker: AVSpeechSynthesizerDelegate {
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) {
print("Speaker class started")
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
print("Speaker class finished")
}
}
「AVSpeechSynthesizer」に入力する前に文字列を翻訳する必要があります。 – Adrian
お返事ありがとうございます。どのAPIを使用してAVSpeechSynthesizerに入力する前に文字列を変換できますか? Appleはこれをサポートする独自のAPIを持っていますか? – Aneesa
多くの場合、GitHubを掘り下げて見つけることができます。私はこのhttps://github.com/watson-developer-cloud/ios-sdk#language-translatorを見ましたが、私はそれを使っていません。 GoogleとBingは翻訳を行います。これを行うフレームワークは - > https://github.com/gpolak/FGTranslatorです。フレームワークを使用するときは、CocoaPodに多くのバンプがありますので、ここではチュートリアル - > https://www.raywenderlich.com/97014/use-cocoapods-with-swift – Adrian