欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS语音播报及后台播放

程序员文章站 2023-10-06 17:31:34
最近项目刚刚交付,偶然间用到了语音播报和语音搜索的功能。语音搜索我用的是讯飞的demo,感觉效果还不错,感兴趣的话可以去官网上面下载demo,里面讲的特别的详细,不过稍显麻烦一些。语音播报讯飞也有d...

最近项目刚刚交付,偶然间用到了语音播报和语音搜索的功能。语音搜索我用的是讯飞的demo,感觉效果还不错,感兴趣的话可以去官网上面下载demo,里面讲的特别的详细,不过稍显麻烦一些。语音播报讯飞也有demo,不过做开发当然要寻求最简洁的处理方式,ios7.0之后新添加了一些新的功能,里面就有自带的语音播报库avfoundation。关于语音播报的文章其实挺多的。文本转语音技术,也叫tts, 是text to speech的缩写. ios如果想做有声书等功能的时候, 会用到这门技术.

一,使用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];

}

完成以上步骤就可在后台进行语音播报了。