How can I detect whether an iOS device is in silent mode or not?

后端 未结 1 1338
执笔经年
执笔经年 2020-12-03 11:30

I\'ve been looking for hours and I can\'t seem to find any API that explains how to detect whether an iPhone ringer is in silent mode or not. This is what I\'m talking about

相关标签:
1条回答
  • 2020-12-03 12:10

    I don't think there is and you probably don't need to directly call an API to detect if the device is muted or not. What you need to know is this:

    When playing a sound, you will do something like:

            try AVAudioSession.sharedInstance().setCategory({AVAudioSessionCategory})
            try AVAudioSession.sharedInstance().setActive(true)
            audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound)
            audioPlayer.prepareToPlay()
            audioPlayer.play()
    

    In the {AVAudioSessionCategory}, you can specify:

    AVAudioSessionCategorySoloAmbient: Your audio is silenced by screen locking and by the Silent switch

    AVAudioSessionCategoryPlayBack: Your Audio continues with the Silent switch set to silent or when the screen locks

    0 讨论(0)
提交回复
热议问题