如何检测iOS设备是否处于静音模式? [英] How can I detect whether an iOS device is in silent mode or not?

查看:3572
本文介绍了如何检测iOS设备是否处于静音模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找几个小时,似乎找不到任何API来解释如何检测iPhone铃声是否处于静音模式。这就是我在说的:

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:

有没有可以使用Swift代码查看iOS设备的静音模式是开启还是关闭?我专门寻找iOS8以上,因为iOS5已被弃用。谢谢。

Is there anyway I can find out whether an iOS device's silent mode is on or off using Swift code? I'm specifically looking for iOS8 and up since the iOS5 ones have been deprecated. Thanks.

推荐答案

我不认为有,您可能不需要直接调用API来检测是否设备静音或不静音您需要知道的是:

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()

在{AVAudioSessionCategory },您可以指定:

In the {AVAudioSessionCategory}, you can specify:


AVAudioSessionCategorySoloAmbient:您的音频由屏幕
锁定和静音开关静音

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

AVAudioSessionCategoryPlayBack:您的音频继续静音
开关设置为静音或屏幕锁定

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

这篇关于如何检测iOS设备是否处于静音模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆