需求
生活中比较流行的语音播报有:微信支付到账提醒,电子书阅读听书等等广泛的运用。苹果于iOS7.0推出了Speech框架,支持语音播报功能。因项目中需要在不打开app的情况下,播放一天的工作日程等需求,查过很多资料,遇到很多坑。
文字播报
import UIKit
import AVFoundation
import Speech
import MediaPlayer
class SpeechUtteranceManager: NSObject {
/// 单例管理语音播报 比较适用于多种类型语音播报管理
public static let shared = SpeechUtteranceManager()
var synthesizer = AVSpeechSynthesizer()
var speechUtterance: AVSpeechUtterance?
var voiceType = AVSpeechSynthesisVoice(language: Locale.current.languageCode)
private override init() {
super.init()
synthesizer.delegate = self
}
/// 自定义语音播报方法
/// 此处只举例播报一个String的情况
func speechWeather(with weather: String) {
if let _ = speechUtterance {
synthesizer.stopSpeaking(at: .immediate)
}
speechUtterance = AVSpeechUtterance(string: weather)
speechUtterance?.voice = voiceType
speechUtterance?.rate = 0.5
synthesizer.speak(speechUtterance!)
UIApplication.shared.beginReceivingRemoteControlEvents()
setLockView()
}
func stop() {
if synthesizer.isSpeaking {
synthesizer.pauseSpeaking(at: .word)
}
}
func Speaking() {
if synthesizer.isPaused {
synthesizer.continueSpeaking()
}
}
//后台控制:显示标题、控制语速 可以添加显示图片等设置
func setLockView(){
let mpic = MPNowPlayingInfoCenter.default()
mpic.nowPlayingInfo = [
MPMediaItemPropertyTitle:"语音播报",
MPNowPlayingInfoPropertyPlaybackRate:1.0
]
}
func stopSpeaking() {
if let _ = speechUtterance {
synthesizer.stopSpeaking(at: .immediate)
}
}
}
extension SpeechUtteranceManager: AVSpeechSynthesizerDelegate {
//开始播放
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) {
}
//播放完成
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
do {
try AVAudioSession.sharedInstance().setActive(false, with: .notifyOthersOnDeactivation)
} catch {
print(error.localizedDescription)
}
speechUtterance = nil
}
//播放中止
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didPause utterance: AVSpeechUtterance) {
}
//恢复播放
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didContinue utterance: AVSpeechUtterance) {
}
//取消播放
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didCancel utterance: AVSpeechUtterance) {
}
}
播放方法:
SpeechUtteranceManager.shared.speechWeather(with:"播放文本")
文本不可以太长,可以连续添加。
后台播放
需要添加权限:Targets->Capabilities->background modes下Audiu,AirPlay打勾
后台控制
在UIViewController 界面中
//监听为第一响应者 这句话很重要,研究了好久,没有这句代码,锁屏检测不到
override var canBecomeFirstResponder: Bool{
return true
}
//后台控制按钮 没有补全 自行补全一下
override func remoteControlReceived(with event: UIEvent?) {
guard let event = event else {
print("no event\n")
return
}
if event.type == UIEventType.remoteControl {
switch event.subtype {
case .remoteControlTogglePlayPause:
print("暂停/播放")
case .remoteControlPreviousTrack:
print("上一首")
case .remoteControlNextTrack:
print("下一首")
case .remoteControlPlay:
print("播放")
SpeechUtteranceManager.shared.Speaking()
case .remoteControlPause:
print("暂停")
SpeechUtteranceManager.shared.stop()
default:
break
}
}
}
延伸:根据排队添加文本,后台播放、后台控制,可以自己做一个小说阅读听书功能。
网友评论