转载链接地址:https://blog.csdn.net/lovechris00 https://blog.csdn.net/lovechris00/article/details/88736928
SpeechSynthesizer 即声音合成,可以通过你输入的文字,系统播放声音。
相关API
- AppKit 框架中的 NSSpeechSynthesizer、NSSpeechRecognizer
- ApplicationServices 框架中的 SpeechSynthesis
teamScore self.synthetizer = [[NSSpeechSynthesizer alloc] initWithVoice:@"com.apple.speech.synthesis.voice.alice.premium"];
[self.synthetizer startSpeakingString:@"123"];
其中 com.apple.speech.synthesis.voice.alice.premium
是 NSSpeechSynthesizerVoiceName (NSString 类型)。
你可以可以设置为 NSSpeechSynthesizer.defaultVoice
通过 终端指令$ mdfind -name com.apple.speech.synthesis.voice 可以发现音频相关文件在 /System/Library/PrivateFrameworks/SpeechObjects.framework/Versions/A/Resources/Audio 目录下。
但可用的声音需要通过 [NSSpeechSynthesizer availableVoices] 获取。
如果不是可用的声音,则会设置失败,使用设置前的上一个声音;如果设置失败的声音之前没有声音,则不会播放。
获取到的可用声音如:
"com.apple.speech.synthesis.voice.Alex",
"com.apple.speech.synthesis.voice.alice",
"com.apple.speech.synthesis.voice.alva",
"com.apple.speech.synthesis.voice.amelie",
"com.apple.speech.synthesis.voice.anna",
"com.apple.speech.synthesis.voice.carmit",
"com.apple.speech.synthesis.voice.damayanti",
"com.apple.speech.synthesis.voice.daniel",
"com.apple.speech.synthesis.voice.diego",
"com.apple.speech.synthesis.voice.ellen",
"com.apple.speech.synthesis.voice.fiona",
"com.apple.speech.synthesis.voice.Fred",
"com.apple.speech.synthesis.voice.ioana",
"com.apple.speech.synthesis.voice.joana",
"com.apple.speech.synthesis.voice.jorge",
NSSpeechSynthesizerVoiceName voice = NSSpeechSynthesizer.defaultVoice;
NSDictionary *attributesForVoice = [NSSpeechSynthesizer attributesForVoice:voice];
NSLog(@"%@",attributesForVoice);
https://developer.apple.com/documentation/applicationservices/speech_synthesis_manager?language=objc