macOS 开发 - NSSpeechSynthesizer 的使用(声音合成)

转载链接地址:https://blog.csdn.net/lovechris00 https://blog.csdn.net/lovechris00/article/details/88736928

一、简介

SpeechSynthesizer 即声音合成,可以通过你输入的文字,系统播放声音。

相关API

  • AppKit 框架中的 NSSpeechSynthesizer、NSSpeechRecognizer
  • ApplicationServices 框架中的 SpeechSynthesis

二、使用 NSSpeechSynthesizer

      1、播放声音

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

      2、获取声音

通过 终端指令$ 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",

      3、获取声音属性

NSSpeechSynthesizerVoiceName voice = NSSpeechSynthesizer.defaultVoice;
NSDictionary *attributesForVoice = [NSSpeechSynthesizer attributesForVoice:voice];
NSLog(@"%@",attributesForVoice);

三、参考

https://developer.apple.com/documentation/applicationservices/speech_synthesis_manager?language=objc

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值