美文网首页
语音后台播报的使用坑点

语音后台播报的使用坑点

作者: lotawei | 来源:发表于2018-01-05 14:08 被阅读60次

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

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

    相关文章

      网友评论

          本文标题:语音后台播报的使用坑点

          本文链接:https://www.haomeiwen.com/subject/kkzjnxtx.html