语音后台播报的使用坑点

需要配置的东西就不说了
后台语音能放出来的关键点代码

语音识别这块是必须的 即 avspeech那块
do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .duckOthers)
} catch {
    print(error.localizedDescription)
}
音频播放的需要实现的
 func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        NotificationCenter.default.addObserver(self, selector: #selector(onAudioSessionEvent), name: NSNotification.Name.AVAudioSessionInterruption, object: AVAudioSession.sharedInstance())

    }
    func onAudioSessionEvent(noti: NSNotification) {
        guard noti.name == NSNotification.Name.AVAudioSessionInterruption && noti.userInfo != nil else {
            return
        }
        
        if let typenumber = (noti.userInfo?[AVAudioSessionInterruptionTypeKey] as AnyObject).uintValue {
            switch typenumber {
            case AVAudioSessionInterruptionType.began.rawValue:
                AudioPlayManager.sharePlayer.audioPlayer.pause()
            case AVAudioSessionInterruptionType.ended.rawValue:
                AudioPlayManager.sharePlayer.audioPlayer.resume()
            default:
                break
            }
        }
    }

你可能感兴趣的:(语音后台播报的使用坑点)