英文:
AVSpeechSynthesizer Voice not changing
问题
我正在使用AVSpeechSynthesizer
,以下是将文本传递给AVSpeechSynthesizer
的代码。
func speakOutText(_ textToRead: String) {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
try audioSession.setMode(AVAudioSessionModeDefault)
try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
} catch {
print("audioSession properties weren't set because of an error.")
}
let speakUtterance = AVSpeechUtterance(string: textToRead)
speakUtterance.voice = AVSpeechSynthesisVoice(language: "en-IN")
speakUtterance.pitchMultiplier = 1.2
speakUtterance.rate = 0.5
speechSynthesizer.speak(speakUtterance)
}
它以女性声音正确朗读文本。
但是,如果我在**设置 -> Siri & Search -> Siri Voice -> Gender(Male)**中更改为男性声音,然后再调用speakOutText
方法,它仍然以女性声音朗读文本,而不是男性声音。
请问有人能告诉我原因,为什么它不会更改为男性声音吗?
英文:
I am using **AVSpeechSynthesizer**
, the following is the code to pass text to **AVSpeechSynthesizer**
.
func speakOutText(_ textToRead:String ){
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
try audioSession.setMode(AVAudioSessionModeDefault)
try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
} catch {
print("audioSession properties weren't set because of an error.")
}
let speakUtterance = AVSpeechUtterance.init(string: textToRead);
speakUtterance.voice = AVSpeechSynthesisVoice(language: "en-IN")
speakUtterance.pitchMultiplier = 1.2
speakUtterance.rate = 0.5
speechSynthesizer.speak(speakUtterance)
}
It is speaking the text properly in a female voice.
But if I change male voice in settings -> Siri & Search -> Siri Voice -> Gender(Male). Again calling speakOutText
method, it is speaking the text in female voice only, not in a male voice.
Can anyone please let me know the reason, why it is not changing to a male voice?
答案1
得分: 4
-
Siri 和 AVSpeechSynthesizer 的语音不同,不在相同的设置位置 (iOS 13.3):
- Siri ⟹ 设置 → Siri 与搜索 → 语言 和 Siri 语音。
- AVSpeechSynthesizer ⟹ 设置 → 辅助功能 → 口语内容 → 语音。
-
但是,如果我在设置中更改男性声音 -> Siri & Search -> Siri Voice -> Gender(男性)... 它仍然以女性声音朗读文本,而不是男性声音。
-
AVSpeechSynthesisVoiceGender
属性只是一个获取器,不能帮助您更改声音的性别。
要实现此目标,您必须在设置中明确指定要使用的声音(请参见上述说明),或者在需要的情况下在编程中进行指定:要注意您的应用程序中使用的当前语言代码(AVSpeechSynthesisVoice.currentLanguageCode()),如果您不明确更改话语的声音,则它将作为默认值。
以下是编写的代码(iOS 13, Swift 5.1):
let allVoices = AVSpeechSynthesisVoice.speechVoices()
var index = 0
for theVoice in allVoices {
print("Voice[\(index)] = \(theVoice)\n")
index += 1
}
请在此列表中指示要使用的语音话语,以获取适当的语音性别。
结论:更改 AVSpeechSynthesizer 的语音与 Siri 无关,即使可以在设置中完成,它也将适应当前语言代码,除非一切都在编程中明确指定。
英文:
> Can anyone please let me know the reason, why it is not changing to a male voice?
Siri and AVSpeechSynthesizer voices aren't the same ones and aren't located at the same place in the settings (iOS 13.3):
- Siri ⟹ Settings → Siri & Search → Language and Siri Voice.
- AVSpeechSynthesizer ⟹ Settings → Accessibility → Spoken Content → Voices.
> But if I change male voice in settings -> Siri & Search -> Siri Voice -> Gender(Male)... it is speaking the text in female voice only, not in a male voice.
The AVSpeechSynthesisVoiceGender
property is only a getter that can't help you to change the gender voice.
To do so, you must specify the exact voice you want to use either with the settings (see the point above) or programmatically if need be: beware of the current language code (AVSpeechSynthesisVoice.currentLanguageCode()) that is used in your app because it will be taken as default if you don't literally change the utterance voice.
Writing the code hereafter (iOS 13, Swift 5.1):
let allVoices = AVSpeechSynthesisVoice.speechVoices()
var index = 0
for theVoice in allVoices {
print("Voice[\(index)] = \(theVoice)\n")
index += 1
}
... will display all the available voices on your device:
Indicate the voice utterance in this list to get the appropriate voice gender
Conclusion: changing the AVSpeechSynthesizer voice has nothing to do with Siri even it can be done in the settings that will be adapted to the current language code unless everything is specified programmatically.
答案2
得分: 0
private let synthesizer = AVSpeechSynthesizer()
var Rate: Float = AVSpeechUtteranceDefaultSpeechRate
var USMaleVoice = AVSpeechSynthesisVoice(language: "en-US")
func Say(_ phrase: String)
{
let Utterance = AVSpeechUtterance(string: phrase)
Utterance.rate = Rate
Utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
synthesizer.speak(Utterance)
}
//Print all voices ↓
func GetVoices()
{
AVSpeechSynthesisVoice.speechVoices().forEach({ print($0)})
}
英文:
private let synthesizer = AVSpeechSynthesizer()
var Rate: Float = AVSpeechUtteranceDefaultSpeechRate
var USMaleVoice = AVSpeechSynthesisVoice(language: "en-US")
func Say(_ phrase: String)
{
let Utterance = AVSpeechUtterance(string: phrase)
Utterance.rate = Rate
Utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
synthesizer.speak(Utterance)
}
//Print all voices ↓
func GetVoices()
{
AVSpeechSynthesisVoice.speechVoices().forEach({ print($0)})
}
答案3
得分: -1
使用这个
AVSpeechSynthesisVoiceGender.male
如果您有任何疑问,请查看苹果链接
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论