gpt4 book ai didi

swift - AVSpeech合成器语言

转载 作者:行者123 更新时间:2023-11-30 12:39:28 27 4
gpt4 key购买 nike

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

这是一些代码

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

最佳答案

也许这对某人有帮助,我找到了解决方案,但在说话之前它看起来像是解决方法,我更改了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)
}

}

关于swift - AVSpeech合成器语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42409968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com