zoukankan      html  css  js  c++  java
  • iOS 获取手机后台音频状态以及类别

    需求: 获取iPhone手机其他第三方音频状态,是否在打电话?是否在后台听音乐?是否在用qq语音打电话?是否在用微信语音打电话?

    第一步:导入AVKit

    查找官方文档发现:

    /// The prompt style is a hint to sessions using AVAudioSessionModeVoicePrompt to alter the type of
    
        /// prompts they issue in response to other audio activity on the system, such as Siri and phone
    
        /// calls. This property is key-value observable.
    
        @available(iOS 13.0, *)
    
        open var promptStyle: AVAudioSession.PromptStyle { get }

    第二步:继续看这个属性写的是什么东西:

    /**
             @enum     AVAudioSessionPromptStyle
             @brief
                 The prompt style is a hint to sessions that use AVAudioSessionModeVoicePrompt to modify the type of
                 prompt they play in response to other audio activity on the system, such as Siri or phone calls.
                 Sessions that issue voice prompts are encouraged to pay attention to changes in the prompt style and
                 modify their prompts in response. Apple encourages the use of non-verbal prompts when the Short
                 style is requested.
             @var AVAudioSessionPromptStyleNone
                 Indicates that another session is actively using microphone input and would be negatively impacted
                 by having prompts play at that time. For example if Siri is recognizing speech, having navigation or
                 exercise prompts play, could interfere with its ability to accurately recognize the user’s speech.
                 Client sessions should refrain from playing any prompts while the prompt style is None.
             @var AVAudioSessionPromptStyleShort
                 Indicates one of three states: Siri is active but not recording, voicemail playback is active, or
                 voice call is active. Short, non-verbal versions of prompts should be used.
             @var AVAudioSessionPromptStyleNormal
                 Indicates that normal (long, verbal) versions of prompts may be used.
         */
        public enum PromptStyle : UInt {
    
            
            case none = 1852796517
    
            case short = 1936224884
    
            case normal = 1852992876
        }
    
    //知道你们懒,我帮你们谷歌翻译一下
    @enum AVAudioSessionPromptStyle
             @简要
                 提示样式是使用AVAudioSessionModeVoicePrompt修改会话类型的会话的提示
                 提示他们播放以响应系统上的其他音频活动,例如Siri或电话。
                 鼓励发出语音提示的会话注意提示样式和
                 修改他们的提示以作为回应。当短促时,Apple鼓励使用非语言提示
                 要求样式。
             @var AVAudioSessionPromptStyleNone
                 表示另一个会话正在使用麦克风输入,并且会受到负面影响
                 通过在那时播放提示。例如,如果Siri识别语音,导航或
                 锻炼提示播放,可能会干扰其准确识别用户语音的能力。
                 提示样式为“无”时,客户端会话应避免播放任何提示。
             @var AVAudioSessionPromptStyleShort
                 指示以下三种状态之一:Siri处于活动状态但未录制,语音邮件播放处于活动状态,或
                 语音通话已启动。提示应使用简短的非语言版本的提示。
             @var AVAudioSessionPromptStyleNormal
                 指示可以使用普通(长,口头)提示版本。

    第三步:使用方法

    let types: UInt = AVAudioSession.sharedInstance().promptStyle.rawValue
                    if types == 1852992876 { 
                        //普通听歌等情景
                    } else if types == 1936224884{ 
                        //手机通话,微信通话等情景
                    } else {
                        //1852796517 
                        //  其他特殊情景,我不想要其他提示,导航等
                    }            

    总结: 以上是API是iOS13.0之后可以使用的方法

    那么iOS13.0之前的系统怎么办呢?

    我自己想了个办法,虽然不是最佳,但至少能凑合用一下。如果在听歌,则会有一瞬间,声音没有了,然后又恢复了

        ///判断暂停success  or  failed 
        //1. 先尝试下暂停音频
        //2. 获取第三方音频播放的status
        //3. if 暂停 success  当前有第三方音频例如网易云音乐等在听歌
        //4. if 在打电话 or  QQ  or  WeChat 电话  ,则暂停failed 
        func isStop() {
            self.setOtherAudioCAtegory(category: .soloAmbient)
    
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {[weak self] in
                self?.isOtherAudioPlaying = AVAudioSession.sharedInstance().isOtherAudioPlaying
                if self?.isOtherAudioPlaying == true {
                    //暂停 failed
                    // 在这里我可以做别的事情了
                } else {
                    //暂停success
                     // 在这里我可以做别的事情了
                }
                self?.resumeGoOn()
            }
    
        }
    
    
    
    
    //尝试暂停一下其他音频
        func setOtherAudioCAtegory(category: AVAudioSession.Category) {
       
            try? audioSession.setCategory(category);
            try? audioSession.setActive(true)
            
        }
    
    
    /// go on 第三方音乐app
        func resumeGoOn() -> Void {
            do {
                try audioSession.setActive(false, options: .notifyOthersOnDeactivation)
            } catch {
                
            }
        }
  • 相关阅读:
    AutoresizingMask草草草
    StoryBoard不使用AutoLayout情况下 按比例快速兼容适配iPhone6/6 Plus教程
    Unbalanced calls to begin/end appearance transitions for XXXX
    XCode常用快捷键
    Xcode使用心得01:断点中断问题和调整编译目标
    reloaddata 后不执行cellForRowAtIndexPath
    Xcode开发调试技巧—断点调试
    Xcode6 ADD Copy Files Build Phase 是灰色的,不能点问题
    duplicate symbols for architecture x86_64
    could not build module uikit
  • 原文地址:https://www.cnblogs.com/naiwenmoer/p/13915130.html
Copyright © 2011-2022 走看看