![1081255447.jpg 640?wx_fmt=jpeg](https://img-blog.csdnimg.cn/img_convert/a278344f161aefd40c5a73b7b40c4f42.png)
![1081255447.jpg 640?wx_fmt=jpeg](https://img-blog.csdnimg.cn/img_convert/a278344f161aefd40c5a73b7b40c4f42.png)
![1081255447.jpg 640?wx_fmt=jpeg](https://img-blog.csdnimg.cn/img_convert/a278344f161aefd40c5a73b7b40c4f42.png)
作者丨RayJiang97
https://www.jianshu.com/p/39bf2b7f45cc
后台播放
大致流程:
1.使用BDSSpeechSynthesizer的synthesizeSentence方法合成音频
2.从BDSSpeechSynthesizerDelegate代理的synthesizerNewDataArrived方法获取合成好的音频
Tips:这个方法中获取到的Data数据是pcm格式的
3.使用BDSBuiltInPlayer播放pcm格式的Data数据
踩坑的总结:
Q:为什么不能用BDSSpeechSynthesizer直接播放
A:如果使用下面代码直接播放,虽然退到后台也能播放,但是无法设置锁屏播放器
Q:为什么不能用AVAudioPlayer播放synthesizerNewDataArrived回调中的Data数据
A:因为这个Data数据是pcm格式,AVAudioPlayer不支持
Q:为什么一段文本synthesizerNewDataArrived会回调多次
A:猜测:当合成的文本较长时,为了更快地合成,会将句子拆分之后请求服务器,所以会回调多次
重点:synthesizerNewDataArrived会按 顺序回调 多次,至于要怎么处理去看百度Demo就知道了
划重点:顺序回调
如何设置后台播放
网上有很多教程了,这里就简单说下
1.在Targets → Capabilities中打开Background Modes,勾选Audio, AirPlay, and Picture in Picture
2.在AppDelegate的applicationWillResignActive回调中设置
func applicationWillResignActive(_ application: UIApplication) {
// 允许后台播放
UIApplication.shared.beginReceivingRemoteControlEvents()
// 设置锁屏播放器的数据
MPNowPlayingInfoCenter.default().nowPlayingInfo = SpeakerManager.manage.playingInfo
}
3.在AppDelegate中实现锁屏播放器的回调remoteControlReceived
override func remoteControlReceived(with event: UIEvent?) {
guard let e = event else {return}
switch e.subtype {
case .remoteControlPlay:
SpeakerManager.manage.continueSpeaking()
case .remoteControlPause:
SpeakerManager.manage.pauseSpeaking()
default:
break
}
}
推荐↓↓↓
![640?wx_fmt=jpeg](https://img-blog.csdnimg.cn/img_convert/f9e4ba8800a77a9f417086b37235e2f1.png)
长
按
关
注
?【16个技术公众号】都在这里!
涵盖:程序员大咖、源码共读、程序员共读、数据结构与算法、黑客技术和网络安全、大数据科技、编程前端、Java、Python、Web编程开发、Android、iOS开发、Linux、数据库研发、幽默程序员等。
![640?wx_fmt=png](https://img-blog.csdnimg.cn/img_convert/091a71e116ede4d63ecebc1202eb0d33.png)