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