2017-02-23 11 views
0

私のアプリではAVSpeechSynthesizerを使用しています。テキストスピーチはロシア語ですが、問題はシステム言語を英語に変更するときです。英語のアクセント付きテキストの発音はロシア語の文字列のようです。この問題をどうやって管理できますか?ここAVSpeechSynthesizer言語

は、いくつかのコード

utterance.voice = AVSpeechSynthesisVoice(language: "ru-Ru") 
    synthesizer.pauseSpeaking(at: .word) 
    utterance = AVSpeechUtterance(string: "Какой-то текст который нужно произнести") 
    synthesizer.speak(utterance) 

答えて

0

ある多分それが誰かのために参考になる、私は解決策を見つけたが、それは私がAppLanguageを変更話す前に、回避策のように見え、ここにコード

UserDefaults.standard.set(["ru"], forKey: "AppleLanguages") 
    UserDefaults.standard.synchronize() 
    utterance.voice = AVSpeechSynthesisVoice(identifier: "ru-RU") 

後には、同期AVSpeechSynthesisVoice.currentLanguageCode()はシステム言語を無視して "ru"になりました

ここにはトルコ語の完全なコード例

輸入のUIKit

輸入AVFoundation

クラスのViewController:のUIViewController {

let synthesizer : AVSpeechSynthesizer = AVSpeechSynthesizer() 
var utterance : AVSpeechUtterance = AVSpeechUtterance(string: "") 

override func viewDidLoad() { 
    super.viewDidLoad() 
    UserDefaults.standard.set(["tr"], forKey: "AppleLanguages") 
    UserDefaults.standard.synchronize() 
} 

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .interruptSpokenAudioAndMixWithOthers) 
    utterance.voice = AVSpeechSynthesisVoice(identifier: "tr-TR") 
    synthesizer.pauseSpeaking(at: .word) 
    testSpeech() 
} 

private func testSpeech() { 
    utterance = AVSpeechUtterance(string: "Çeviri için deney metni") 
    speak() 
} 

private func speak() { 
    if synthesizer.isSpeaking { 
     synthesizer.pauseSpeaking(at: .immediate) 
     return 
    } 
    synthesizer.speak(utterance) 
} 

}

+0

それは私のために動作しませんでした。助言がありますか? –

+0

あなたが使用する言語キーが正しいと確信していますか? –

+0

言語キーに関係なく、システム言語で動作します。 –