iOS如何实现语音播报及后台播放
2016-12-08 11:34
453 查看
最近项目刚刚交付,偶然间用到了语音播报和语音搜索的功能。语音搜索我用的是讯飞的demo,感觉效果还不错,感兴趣的话可以去官网上面下载demo,里面讲的特别的详细,不过稍显麻烦一些。语音播报讯飞也有demo,不过做开发当然要寻求最简洁的处理方式,ios7.0之后新添加了一些新的功能,里面就有系统自带的语音播报库AVFoundation。关于语音播报的文章其实挺多的。文本转语音技术, 也叫TTS, 是Text To Speech的缩写. iOS如果想做有声书等功能的时候, 会用到这门技术.
一,使用iOS自带TTS需要注意的几点:
iOS7之后才有该功能
需要 AVFoundation 库
AVSpeechSynthesizer: 语音合成器, 可以假想成一个可以说话的人, 是最主要的接口
AVSpeechSynthesisVoice: 可以假想成人的声音
AVSpeechUtterance: 可以假想成要说的一段话
二,代码示例, 播放语音
三,AVSpeechSynthesizer介绍
这个类就像一个会说话的人, 可以”说话”, 可以”暂停”说话, 可以”继续”说话, 可以判断他当前是否正在说话.有以下的方法或者属性:
说话: speakUtterance
控制: continueSpeaking(继续说), pauseSpeakingAtBoundary(暂停说话), paused(暂停状态的属性), speaking(说话的状态), stopSpeakingAtBoundary(停止说话)
委托: delegate
四,AVSpeechBoundary介绍
这是一个枚举. 在暂停, 或者停止说话的时候, 停下的方式用这个枚举标示. 包括两种:
AVSpeechBoundaryImmediate: 立即停
AVSpeechBoundaryWord : 说完一个整词再停
五,AVSpeechSynthesizerDelegate介绍
合成器的委托, 对于一些事件, 提供了响应的接口.
didCancelSpeechUtterance: 已经取消说话
didContinueSpeechUtterance: 已经继续说话
didFinishSpeechUtterance: 已经说完
didPauseSpeechUtterance: 已经暂停
didStartSpeechUtterance:已经开始
willSpeakRangeOfSpeechString:将要说某段话
六,AVSpeechSynthesisVoice介绍
AVSpeechSynthesisVoice定义了一系列的声音, 主要是不同的语言和地区.
voiceWithLanguage: 根据制定的语言, 获得一个声音.
speechVoices: 获得当前设备支持的声音
currentLanguageCode: 获得当前声音的语言字符串, 比如”ZH-cn”
language: 获得当前的语言
七,AVSpeechUtterance介绍
这个类就是一段要说的话. 主要的属性和方法有:
pitchMultiplier: 音高
postUtteranceDelay: 读完一段后的停顿时间
preUtteranceDelay: 读一段话之前的停顿
rate: 读地速度, 系统提供了三个速度: AVSpeechUtteranceMinimumSpeechRate, AVSpeechUtteranceMaximumSpeechRate, AVSpeechUtteranceDefaultSpeechRate
speechString: 要读的字符串
voice: 使用的声音, 是AVSpeechSynthesisVoice对象
上面这些是关于语音播报的基本用法和一些属性、方法,但是如何结合程序推送,在程序后台运行的时候实现语音播报的效果呢?当然还有很多需要注意的地方。
1.启用推送唤醒
和上面的后台获取类似,更改Info.plist,在UIBackgroundModes下加入remote-notification即可开启,当然同样的更简单直接的办法是使用Capabilities,勾选下面的三个modes。
2.更改推送的payload
在iOS7中,如果想要使用推送来唤醒应用运行代码的话,需要在payload中加入content-available,并设置为1。
{"aps":{"content-available":1,"alert":"今天是个好天气"}}
"content-available":1 推送唤醒
"alert":"" 推送内容
"badge":1 app右上角数字
“sound”:”default” 默认声音
aps
{
content-available: 1
alert: {...}
}
3.实现推送唤醒代码并通知系统
最后在appDelegate中实现-application:didReceiveRemoteNotification:fetchCompletionHandle:。这部分内容和上面的后台获取部分完全一样,在此不再重复。
//接收到推送消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"remote: %@", userInfo);
//回调
completionHandler(UIBackgroundFetchResultNewData);
//语音播报
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:userInfo[@"aps"][@"alert"]];
AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
[synth speakUtterance:utterance];
}
完成以上步骤就可在后台进行语音播报了。
参考文章链接:
一、http://www.jianshu.com/p/174fd2673897
二、https://onevcat.com/2013/08/ios7-background-multitask/
三、http://hayageek.com/ios-silent-push-notifications/
四、http://blog.csdn.net/u012477117/article/details/52039506
五、http://www.cnblogs.com/luerniu/p/5901350.html
六、https://www.oschina.net/question/2556708_2194798
一,使用iOS自带TTS需要注意的几点:
iOS7之后才有该功能
需要 AVFoundation 库
AVSpeechSynthesizer: 语音合成器, 可以假想成一个可以说话的人, 是最主要的接口
AVSpeechSynthesisVoice: 可以假想成人的声音
AVSpeechUtterance: 可以假想成要说的一段话
二,代码示例, 播放语音
//语音播报 AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"床前明月光,疑是地上霜。"]; utterance.pitchMultiplier=0.8; //中式发音 AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]; //英式发音 // AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"]; utterance.voice = voice; NSLog(@"%@",[AVSpeechSynthesisVoice speechVoices]); AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc]init]; [synth speakUtterance:utterance];
三,AVSpeechSynthesizer介绍
这个类就像一个会说话的人, 可以”说话”, 可以”暂停”说话, 可以”继续”说话, 可以判断他当前是否正在说话.有以下的方法或者属性:
说话: speakUtterance
控制: continueSpeaking(继续说), pauseSpeakingAtBoundary(暂停说话), paused(暂停状态的属性), speaking(说话的状态), stopSpeakingAtBoundary(停止说话)
委托: delegate
四,AVSpeechBoundary介绍
这是一个枚举. 在暂停, 或者停止说话的时候, 停下的方式用这个枚举标示. 包括两种:
AVSpeechBoundaryImmediate: 立即停
AVSpeechBoundaryWord : 说完一个整词再停
五,AVSpeechSynthesizerDelegate介绍
合成器的委托, 对于一些事件, 提供了响应的接口.
didCancelSpeechUtterance: 已经取消说话
didContinueSpeechUtterance: 已经继续说话
didFinishSpeechUtterance: 已经说完
didPauseSpeechUtterance: 已经暂停
didStartSpeechUtterance:已经开始
willSpeakRangeOfSpeechString:将要说某段话
六,AVSpeechSynthesisVoice介绍
AVSpeechSynthesisVoice定义了一系列的声音, 主要是不同的语言和地区.
voiceWithLanguage: 根据制定的语言, 获得一个声音.
speechVoices: 获得当前设备支持的声音
currentLanguageCode: 获得当前声音的语言字符串, 比如”ZH-cn”
language: 获得当前的语言
七,AVSpeechUtterance介绍
这个类就是一段要说的话. 主要的属性和方法有:
pitchMultiplier: 音高
postUtteranceDelay: 读完一段后的停顿时间
preUtteranceDelay: 读一段话之前的停顿
rate: 读地速度, 系统提供了三个速度: AVSpeechUtteranceMinimumSpeechRate, AVSpeechUtteranceMaximumSpeechRate, AVSpeechUtteranceDefaultSpeechRate
speechString: 要读的字符串
voice: 使用的声音, 是AVSpeechSynthesisVoice对象
上面这些是关于语音播报的基本用法和一些属性、方法,但是如何结合程序推送,在程序后台运行的时候实现语音播报的效果呢?当然还有很多需要注意的地方。
1.启用推送唤醒
和上面的后台获取类似,更改Info.plist,在UIBackgroundModes下加入remote-notification即可开启,当然同样的更简单直接的办法是使用Capabilities,勾选下面的三个modes。
2.更改推送的payload
在iOS7中,如果想要使用推送来唤醒应用运行代码的话,需要在payload中加入content-available,并设置为1。
{"aps":{"content-available":1,"alert":"今天是个好天气"}}
"content-available":1 推送唤醒
"alert":"" 推送内容
"badge":1 app右上角数字
“sound”:”default” 默认声音
aps
{
content-available: 1
alert: {...}
}
3.实现推送唤醒代码并通知系统
最后在appDelegate中实现-application:didReceiveRemoteNotification:fetchCompletionHandle:。这部分内容和上面的后台获取部分完全一样,在此不再重复。
//接收到推送消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"remote: %@", userInfo);
//回调
completionHandler(UIBackgroundFetchResultNewData);
//语音播报
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:userInfo[@"aps"][@"alert"]];
AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
[synth speakUtterance:utterance];
}
完成以上步骤就可在后台进行语音播报了。
参考文章链接:
一、http://www.jianshu.com/p/174fd2673897
二、https://onevcat.com/2013/08/ios7-background-multitask/
三、http://hayageek.com/ios-silent-push-notifications/
四、http://blog.csdn.net/u012477117/article/details/52039506
五、http://www.cnblogs.com/luerniu/p/5901350.html
六、https://www.oschina.net/question/2556708_2194798
相关文章推荐
- iOS如何实现语音播报及后台播放
- iOS语音播报及后台播放
- iOS实现在后台播放音乐
- iOS实现在后台播放音乐
- iOS中如何实现 压缩语音(Spexx)
- ios开发视频播放后台下载功能实现 :1,ios播放视频 ,包含基于AVPlayer播放器,2,实现下载,iOS后台下载(多任务同时下载,单任务下载,下载进度,下载百分比,文件大小,下载状态)(真机调试功能正常)
- iOS AVPlayer后台播放的实现
- iOS自带TTS技术的实现即语音播报
- iOS实现在后台播放音乐
- iOS 语音类App播放自己的录音完毕后,如何重新继续播放音乐
- iOS项目开发—TTS技术的实现即语音播报(实现方法一)
- ios播放多个PCM直播音频流,实现多人语音聊天
- ios 播放音频 实现长时间后台运行(流氓)
- 请问如何动态实现播放语音文件[WEB学习示例]
- IOS 控制音量并实现后台播放音乐
- iOS-----实现在后台播放音乐
- ios项目开发—TTS技术的实现即语音播报(实现方法二)
- iOS 如何实现 语音朗读
- iOS开发简单的实现后台任务(诸如后台播放音乐,定时器等)