语音后台播报的使用坑点

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

语音识别这块是必须的 即 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
            }
        }
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,856评论 25 709
  • 张义同志,曾是我教育局同事,现任县委办副主任。上月,我于简书发书联“桃李春风一杯酒,江湖夜雨十年灯”,他见后以为甚...
    高德华阅读 3,701评论 1 7
  • 下载java 网址:jdk下载 安装过程 过程很easy。 打开dmg文件,双击安装文件,跟着流程next就行了。...
    JooYip阅读 3,427评论 0 3