iOS语音合成,语音阅读《AVFoundation》->AVSpeechSynthesizer使用方法介绍

一:写在前面

相关源代码已经上传到网上,里面该有的注释也都有了,感兴趣的同学可以直接上Github下载: AVFoundation相关代码下载地址:点击我就可以了。

二:正文

我们有时候在读书软件上可以发现语音朗读功能(读起来好像没什么感情)。其实这个利用iOS系统api就可以实现。下面就通过一个语音朗读文字的demo来讲解该功能的实现步骤。

2.1:AVSpeechSynthesizer介绍

实现该功能核心的部件就是AVSpeechSynthesizer。

NS_CLASS_AVAILABLE_IOS(7_0)
@interface AVSpeechSynthesizer : NSObject

大家看到了么,iOS7.0之后才出现了AVSpeechSynthesizer。AVSpeechSynthesizer具有以下属性:

//代理方法
@property(nonatomic, weak, nullable) id<AVSpeechSynthesizerDelegate> delegate;
//是否正在朗读(只读)
@property(nonatomic, readonly, getter=isSpeaking) BOOL speaking;
//是否已经暂停(只读)
@property(nonatomic, readonly, getter=isPaused) BOOL paused;

支持的方法如下:

//朗读方法,需要一个AVSpeechUtterance类型参数
- (void)speakUtterance:(AVSpeechUtterance *)utterance;//停止朗读,会清理掉当前正在执行朗读操作的队列
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;
//暂停朗读,这里面需要传递一个AVSpeechBoundary类型参数,两种选择,是立即停止还是读完这个单词再停止。
- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;
//继续朗读
- (BOOL)continueSpeaking;

上面提到代理方法,还记得不?下面就是相关的代理方法:

//开始朗读的代理方法
- (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;

2.2:AVSpeechUtterance介绍

除了核心的AVSpeechSynthesizer,还有一个非常关键的成员就是AVSpeechUtterance,要是想把声音读出来并且设置和声音有关的一些属性,你离不开它。AVSpeechUtterance主要包含以下属性:

//需要用什么语言来朗读,系统提供了很多语言选项,如果有中文,一定要选择中文语言,要不然读不出来。
@property(nonatomic, retain, nullable) AVSpeechSynthesisVoice *voice;
//朗读的内容
@property(nonatomic, readonly) NSString *speechString;
//朗读的内容NSAttributedString 格式
@property(nonatomic, readonly) NSAttributedString *attributedSpeechString API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0));/* Setting these values after a speech utterance has been enqueued will have no effect. */
语速0.0f~1.0f
@property(nonatomic) float rate;             // Values are pinned between AVSpeechUtteranceMinimumSpeechRate and AVSpeechUtteranceMaximumSpeechRate.//声音的音调0.5f~2.0f
@property(nonatomic) float pitchMultiplier;  // [0.5 - 2] Default = 1
@property(nonatomic) float volume;           // [0-1] Default = 1
//播放下下一句话的时候有多长时间的延迟
@property(nonatomic) NSTimeInterval preUtteranceDelay;    // Default is 0.0
//开始播放之前需要等待多久
@property(nonatomic) NSTimeInterval postUtteranceDelay;   // Default is 0.0

支持的方法如下:

//初始化类方法,需要传一个字符串进去
+ (instancetype)speechUtteranceWithString:(NSString *)string;//初始化类方法,需要一个NSAttributedString类型字符串
+ (instancetype)speechUtteranceWithAttributedString:(NSAttributedString *)string API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0));//初始化对象方法,需要一个字符串作为参数
- (instancetype)initWithString:(NSString *)string;
//初始化对象方法,需要一个NSAttributedString类型字符串
- (instancetype)initWithAttributedString:(NSAttributedString *)string API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0));

上面提到的语言类型,主要由以下几种:

Arabic (ar-SA)
Chinese (zh-CN, zh-HK, zh-TW)
Czech (cs-CZ)
Danish (da-DK)
Dutch (nl-BE, nl-NL)
English (en-AU, en-GB, en-IE, en-US, en-ZA)
Finnish (fi-FI)
French (fr-CA, fr-FR)
German (de-DE)
Greek (el-GR)
Hebrew (he-IL)
Hindi (hi-IN)
Hungarian (hu-HU)
Indonesian (id-ID)
Italian (it-IT)
Japanese (ja-JP)
Korean (ko-KR)
Norwegian (no-NO)
Polish (pl-PL)
Portuguese (pt-BR, pt-PT)
Romanian (ro-RO)
Russian (ru-RU)
Slovak (sk-SK)
Spanish (es-ES, es-MX)
Swedish (sv-SE)
Thai (th-TH)
Turkish (tr-TR)

三:代码实现:

相关知识点介绍完毕之后,下面就是具体的代码实现:

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVSpeechSynthesizerDelegate>
@property(nonatomic,strong) NSArray * strArray;
@property(nonatomic,strong) NSArray *voiceArray;
@property(nonatomic,strong) AVSpeechSynthesizer *synthesizer;
@property (weak, nonatomic) IBOutlet UILabel *speechLabel;
@property (weak, nonatomic) IBOutlet UIButton *controlButton;
@property (nonatomic,assign)BOOL isPlay;
@end@implementation ViewController- (void)viewDidLoad {
[super viewDidLoad];
self.strArray =@[@"单车欲问边,",@"属国过居延。",@"征蓬出汉塞,",@"归雁入胡天。",@"大漠孤烟直,",@"长河落日圆,",@"萧关逢候骑,",@"都护在燕然。",@"A solitary carriage to the frontiers bound,",@"An envoy with no retinue around,",@"A drifting leaf from proud Cathy,",@"With geese back north on a hordish day.",@"A smoke hangs straight on the desert vast,",@"A sun sits round on the endless stream.",@"A horseman bows by a fortress passed:",@"The general’s at the north extreme!"];
self.voiceArray = @[[AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"],[AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"]];
self.synthesizer = [[AVSpeechSynthesizer alloc]init];
self.isPlay = NO;
[self.controlButton setTitle:@"pause" forState:UIControlStateNormal];
self.synthesizer.delegate = self;
for (int i = 0; i < self.strArray.count;  i ++) {AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:self.strArray[i]];//需要读的语言if (i < 8) {utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];}else{utterance.voice = self.voiceArray[i%2];}//语速0.0f~1.0futterance.rate = 0.5f;//声音的音调0.5f~2.0futterance.pitchMultiplier = 0.8f;//播放下下一句话的时候有多长时间的延迟utterance.postUtteranceDelay = 0.1f;//上一句之前需要多久utterance.preUtteranceDelay = 0.5f;//音量utterance.volume = 1.0f;//开始播放[self.synthesizer speakUtterance:utterance];
}
}
//开始朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{
NSLog(@"didStartSpeechUtterance->%@",utterance.speechString);
}
//结束朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
NSLog(@"didFinishSpeechUtterance->%@",utterance.speechString);
}
//暂停朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance{
NSLog(@"didPauseSpeechUtterance->%@",utterance.speechString);
}
//继续朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance{
NSLog(@"didContinueSpeechUtterance->%@",utterance.speechString);
}
//取消朗读的代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance{
NSLog(@"didCancelSpeechUtterance->%@",utterance.speechString);
}
//将要播放的语音文字
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance{
NSLog(@"willSpeakRangeOfSpeechString->characterRange.location = %zd->characterRange.length = %zd->utterance.speechString= %@",characterRange.location,characterRange.length,utterance.speechString);
self.speechLabel.text = utterance.speechString;
}- (IBAction)buttonClick:(id)sender {
self.isPlay = !self.isPlay;
if (self.isPlay) {[self.controlButton setTitle:@"play" forState:UIControlStateNormal];[self.synthesizer  pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
else{[self.controlButton setTitle:@"pause" forState:UIControlStateNormal];[self.synthesizer continueSpeaking];
}
}- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}@end

写在后面:

相关源代码已经上传到网上,里面该有的注释也都有了,感兴趣的同学可以直接上Github下载: AVFoundation相关代码下载地址:点击我就可以了。

iOS语音合成,语音阅读《AVFoundation》-AVSpeechSynthesizer使用方法介绍相关推荐

  1. unity怎么设置游戏页面_王者荣耀李小龙粤语语音包怎么得?李小龙粤语语音包获取与设置方法介绍[多图] - 游戏攻略...

    王者荣耀游戏中,李小龙有他的专属粤语语音,想要使用粤语语音先要获取语音包后才能设置,以下是王者荣耀李小龙皮肤粤语语音包获取与设置方法介绍. 王者荣耀李小龙皮肤粤语语音包获取与设置方法介绍: 王者荣耀裴 ...

  2. ios电子书语音阅读

    非常感谢大家利用自己宝贵的时间来阅读我的文章 ,  上一篇文章写了Epub的加密一个实现方式,<ios Epub加密及解密阅读的一种实现方式>,今天这篇文章主要说一下电子书的语音阅读,这个 ...

  3. “讯飞语音+”语音识别开放功能使用方法介绍

    1"讯飞语音+"功能介绍 "讯飞语音+"主要提供的语音服务包括语音合成.语音识别.语义理解和语音搜索. 语音合成 讯飞语音+提供的语音合成服务包括在线语音合成与 ...

  4. 噬血代码进不了游戏_噬血代码联机不了怎么办 噬血代码联机失败解决方法介绍-游侠网...

    噬血代码联机不了怎么办?今天我们给大家带来了噬血代码联机失败解决方法介绍,相信大家看完后,就能和小伙伴一起愉快联机了,还不知道解决方法的玩家赶紧来看看吧. 推荐阅读 联机失败解决方法介绍 多人连线不稳 ...

  5. ios apple语音性别_如何使用Apple的CoreML和Vision API构建图像识别iOS应用

    ios apple语音性别 by Mark Mansur 马克·曼苏尔(Mark Mansur) 如何使用Apple的CoreML和Vision API构建图像识别iOS应用 (How to buil ...

  6. ios apple语音性别_iOS 14:Apple终于听了

    ios apple语音性别 Every year, Apple release the latest version of iOS, one of the most popular operating ...

  7. 文字转语音文件的两种方法

    文件转语音文件的两种方法.小伙伴们在日常的工作.学习或是生活当中,是否遇到过以下这种情况.在工作中每天要阅读大量文件资料,在学习中每天要阅读各种课文,在生活中也会观看各种小说.但是长时间的用眼阅读,就 ...

  8. android 语音阅读软件,语音阅读器APP

    语音阅读器APP是一款非常好用的文字转语音阅读软件,不论身在哪里,不论方不方便看文本,该软件都可以帮你转换成语音读给你听,支持文章.小说等等内容的语音,非常方便,需要语音阅读器的可以来西西下载使用! ...

  9. html ios 视频播放不了,html中的video标签在ios微信中无法播放的解决方法之一

    html中的video标签在ios微信中无法播放的解决方法之一 发布时间:2020-07-16 22:41:04 来源:51CTO 阅读:2652 作者:lixiaoyu1223 最近在做一个微信企业 ...

最新文章

  1. React UI 库:React Suite 3.7.8 版本更新
  2. oracle中ci是什么意思,enq: CI - contention(附AWR)
  3. 双机调试在线下载符号文件
  4. python常用内置函数可以操作字符串_Python常用内置函数(二)
  5. ASP.NET Core开发常见“坑”
  6. 揭秘《死者之书》之游戏角色资源创作
  7. 是Dubbo不香了吗?阿里为啥又搞一套Spring Cloud Alibaba?
  8. python显示no matching distribution,Python使用pip安装No matching distribution found for PyYaml==5.3.1...
  9. python的实时音频传送_python – 使用Alexa传输音频的最简单示例
  10. groupmod 修改用户组信息
  11. HART转PROFIBUS DP(V0)+RS485方法
  12. java mp3转g722_(转载)wav文件转成g722, g729编码的文件
  13. 台湾J2ME专家王森北京讲座---掌上开发专业研讨
  14. Linux 中的逻辑卷 LVM 管理完整初学者指南
  15. 很多人觉得做地推很没有面子
  16. Vue中使用echart实现中国地图统计图
  17. 长高不仅靠遗传,让孩子再次长高的秘诀都在这
  18. ubuntu下常用软件下载安装
  19. 华为发布下一代智能产品战略及全新+AI系列新品!
  20. 在多个指定区间内生成随机数,且随机数总和固定算法

热门文章

  1. Java 23种设计模式全归纳 | 已打包请带走
  2. python中的单引号和双引号的区别
  3. windows下的信息收集
  4. MMDetection2系列-(3)-部署
  5. 从NXP官网下载的SDK如何导进MCUXpresso IDE
  6. 高通开发系列 - 内核升级后音频驱动的开发和调试
  7. 【转】自然常数“e”,工程中的自然数“1”
  8. 可拖拽,拉伸的盒子(类似我们的桌面应用框,可以拉伸放大,可以拖拽移动)
  9. poi tl 判断空值_POI中文API文档
  10. Qt简介 及与MFC、GDK+的比较