0

在我的应用程序中,我使用 AVSpeechSynthesizer,文本演讲是俄语,问题是当我将系统语言更改为英语时,文本发音带有英语口音,听起来像是俄语的转录。我该如何处理这个问题?

这是一些代码

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

1 回答 1

0

也许它会对某人有所帮助,我找到了解决方案,但在说话之前它看起来像是解决方法我更改了 AppLanguage,这里是代码

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

同步 AVSpeechSynthesisVoice.currentLanguageCode() 后变为“ru”,忽略系统语言

这是土耳其语的完整代码示例

导入 UIKit

导入 AVFoundation

类视图控制器: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)
}

}

于 2017-02-23T08:32:05.490 回答