iOS/Swift - AVFoundation(-)文本转语音

在iOS中我们想把文本转换成语音使用的AVFoundation框架中的AVSpeechSynthesizer。
比如我们要把“你好”读出来代码如下:

AVSpeechSynthesizer * synthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance * utterance = [[AVSpeechUtterance alloc] initWithString:@"你好"];
[synthesizer speakUtterance:utterance];

运行程序会自动读出“你好”。
这里的AVSpeechSynthesizer这个是合成器

AVSpeechSynthesizer

AVSpeechSynthesizer有以下的属性:
speaking:是否在说话,只读属性
paused:是否已经停止说话,只读属性;
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;:停止说话并且清除队列;
- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;:暂停说话;
- (BOOL)continueSpeaking;:继续说话;
NSArray<AVAudioSessionChannelDescription *> *outputChannels:输出频道,iOS10加入,默认是没有的也就是不设置就在系统的声道;
delegate - AVSpeechSynthesizerDelegate:重要的属性:
其中包括以下代理:

//  开始
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance;
// 完成
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance;
// 暂停
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance;
// 继续
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance;
// 取消
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance;
// 将要读到的位置
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance;

AVSpeechUtterance

AVSpeechUtterance这里相当于一个人的声带。
我们看下AVSpeechUtterance里面的属性。
voice:表示语言的种类,我们可以用AVSpeechSynthesisVoice的speechVoices来回去“目前”支持的的语言列表。
rate:播放语音的速率,相当于讲话的速度哈。其中有

AVF_EXPORT const float AVSpeechUtteranceMinimumSpeechRate NS_AVAILABLE_IOS(7_0);  // 最小的说话速率(0.0)
AVF_EXPORT const float AVSpeechUtteranceMaximumSpeechRate NS_AVAILABLE_IOS(7_0);  // 最大的说话速率(1.0)
AVF_EXPORT const float AVSpeechUtteranceDefaultSpeechRate NS_AVAILABLE_IOS(7_0);  // 默认的说话速率(0.5)

pitchMultiplier:这个属性是表示声音的音调,他一般在[0.5 - 2],默认值是1
volume:是声音的大小,也就是音量默认是1
postUtteranceDelaypreUtteranceDelay:这个属性表示播放多个语音是,之间的间歇。post表示播放下一句的间歇时间,pre播放当前语句前的间歇时间。

下面是我打印的支持的声音列表:

for (AVSpeechSynthesisVoice *voice in [AVSpeechSynthesisVoice speechVoices]) {
        NSLog(@"%@", voice.language);
    }
2017-01-15 16:28:59.969489 NSSpeechSythesizer[1204:766847] ar-SA  // 沙特阿拉伯 🇸🇦
2017-01-15 16:28:59.969527 NSSpeechSythesizer[1204:766847] cs-CZ  // 捷克共和国 🇨🇿
2017-01-15 16:28:59.969558 NSSpeechSythesizer[1204:766847] da-DK  // 丹麦 🇩🇰
2017-01-15 16:28:59.969586 NSSpeechSythesizer[1204:766847] de-DE  // 德国 🇩🇪
2017-01-15 16:28:59.969613 NSSpeechSythesizer[1204:766847] el-GR  // 希腊 🇬🇷
2017-01-15 16:28:59.969786 NSSpeechSythesizer[1204:766847] en-AU  // 澳大利亚 🇦🇺
2017-01-15 16:28:59.969971 NSSpeechSythesizer[1204:766847] en-GB  // 联合王国  
2017-01-15 16:28:59.970051 NSSpeechSythesizer[1204:766847] en-IE  // 爱尔兰 🇮🇪
2017-01-15 16:28:59.970077 NSSpeechSythesizer[1204:766847] en-US  // 美国 🇺🇸
2017-01-15 16:28:59.970351 NSSpeechSythesizer[1204:766847] en-ZA  // 南非 🇿🇦
2017-01-15 16:28:59.970446 NSSpeechSythesizer[1204:766847] es-ES  // 西班牙 🇪🇸
2017-01-15 16:28:59.970517 NSSpeechSythesizer[1204:766847] es-MX  // 墨西哥 🇲🇽
2017-01-15 16:28:59.970547 NSSpeechSythesizer[1204:766847] fi-FI  // 芬兰 🇫🇮
2017-01-15 16:28:59.970575 NSSpeechSythesizer[1204:766847] fr-CA  // 加拿大 🇨🇦
2017-01-15 16:28:59.970630 NSSpeechSythesizer[1204:766847] fr-FR  // 法国 🇫🇷
2017-01-15 16:28:59.970660 NSSpeechSythesizer[1204:766847] he-IL  // 以色列 🇮🇱
2017-01-15 16:28:59.970687 NSSpeechSythesizer[1204:766847] hi-IN  // 印度 🇮🇳
2017-01-15 16:28:59.970714 NSSpeechSythesizer[1204:766847] hu-HU  // 匈牙利 🇭🇺
2017-01-15 16:28:59.970741 NSSpeechSythesizer[1204:766847] id-ID  // 印尼 🇮🇩
2017-01-15 16:28:59.970767 NSSpeechSythesizer[1204:766847] it-IT  // 意大利🇮🇹
2017-01-15 16:28:59.970793 NSSpeechSythesizer[1204:766847] ja-JP  // 日本 🇯🇵
2017-01-15 16:28:59.970819 NSSpeechSythesizer[1204:766847] ko-KR  // 韩国 🇰🇷
2017-01-15 16:28:59.970945 NSSpeechSythesizer[1204:766847] nl-BE  // 比利时 🇧🇪
2017-01-15 16:28:59.971428 NSSpeechSythesizer[1204:766847] nl-NL  // 荷兰 🇳🇱 
2017-01-15 16:28:59.971531 NSSpeechSythesizer[1204:766847] no-NO  // 挪威 🇳🇴
2017-01-15 16:28:59.971559 NSSpeechSythesizer[1204:766847] pl-PL  // 波兰 🇵🇱
2017-01-15 16:28:59.971613 NSSpeechSythesizer[1204:766847] pt-BR  // 巴西 🇧🇷
2017-01-15 16:28:59.971643 NSSpeechSythesizer[1204:766847] pt-PT  // 葡萄牙 🇵🇹
2017-01-15 16:28:59.971669 NSSpeechSythesizer[1204:766847] ro-RO  // 罗马尼亚 🇷🇴
2017-01-15 16:28:59.971696 NSSpeechSythesizer[1204:766847] ru-RU  // 俄罗斯联邦 🇷🇺
2017-01-15 16:28:59.971722 NSSpeechSythesizer[1204:766847] sk-SK  // 斯洛伐克 🇸🇰
2017-01-15 16:28:59.971749 NSSpeechSythesizer[1204:766847] sv-SE  // 瑞典 🇸🇪
2017-01-15 16:28:59.971795 NSSpeechSythesizer[1204:766847] th-TH  // 泰国 🇹🇭
2017-01-15 16:28:59.971891 NSSpeechSythesizer[1204:766847] tr-TR  // 土耳其 🇹🇷
2017-01-15 16:28:59.971959 NSSpeechSythesizer[1204:766847] zh-CN  // 中国 🇨🇳
2017-01-15 16:28:59.972096 NSSpeechSythesizer[1204:766847] zh-HK  // 中国香港 🇨🇳🇭🇰
2017-01-15 16:28:59.973301 NSSpeechSythesizer[1204:766847] zh-TW  // 中国台湾 🇨🇳

iOS 8添加了希伯来语-以色列,在iOS 9或10中没有添加新语言。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 16,665评论 4 61
  • 最近项目刚刚交付,偶然间用到了语音播报和语音搜索的功能。语音搜索我用的是讯飞的demo,感觉效果还不错,感兴趣的话...
    奔跑的小蚂蚁_8b28阅读 8,688评论 13 6
  • 有道是:天下合久必分,分久必合。三国后期,司马氏乱曹,谋逆主上。三分归之一晋,为司马昭所篡。下传至第三代司马勋之上...
    天地明珠阅读 270评论 0 0
  • “有幸”生活圈里面遇见几个爱折腾的人。 对于我们这种被21世纪“快餐文化”、“碎片化”影响的一代人,让我看到超过三...
    小弟桑pc阅读 3,278评论 0 0
  • 和柚溪的正式交集是在一个夕阳斜照的一个傍晚,和往常一样,我坐在她身后看着夕阳,看着她的背影,突然,她好像在沙滩上发...
    冰瞳_7444阅读 351评论 0 0

友情链接更多精彩内容