【问题标题】:AVSpeechSynthesizer languageAVS语音合成器语言
【发布时间】:2017-06-17 14:40:09
【问题描述】:

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

这里有一些代码

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

【问题讨论】:

    标签: swift voice avspeechsynthesizer avspeechutterance


    【解决方案1】:

    也许它会对某人有所帮助,我找到了解决方案,但在我更改 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)
    }
    

    }

    【讨论】:

    • 无论语言键,它都适用于系统语言。
    • 你能在你想使用这段代码的地方展示一部分代码吗?就我而言,我用它来删除英语口音,因为该应用程序是俄语。
    • 你去吧:让 lName = defaults.getLanguage() defaults.store?.setObject([defaults.lang2LetterCode(lName)], forKey: "AppleLanguages") defaults.store?.synchronize()让 langCode = defaults.lang5LetterCode(lName) if let voiceSL = AVSpeechSynthesisVoice(language: langCode) { utterance.voice = voiceSL }
    • 我使用 en-GB 作为 langCode 尝试了您的代码,它工作正常。什么确切不起作用?尽管您要播放的文本不同,但您仍然有 deviceLanguage 重音?
    • 当设备语言为英语并且我将其设置为“tr-TR”时,它不会读取土耳其语。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 2021-12-14
    • 1970-01-01
    相关资源
    最近更新 更多