Mute
没有原生的 iOS API 可以检测设备上的静音开关是否启用/禁用。
检查设备是否静音的一般原理是播放一个短音,并检测其播放长度。从这段长度中,我们可以确定是否已静音。
这个库实际上是 SoundSwitch 的 Swift 变体。
默认触发频率为 1 秒。
示例
要运行示例项目,请克隆仓库,并首先从 Example 目录运行 pod install
用法
import UIKit
import Mute
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel! {
didSet {
self.label.text = "-"
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Notify every 2 seconds
Mute.shared.checkInterval = 2.0
// Always notify on interval
Mute.shared.alwaysNotify = true
// Update label when notification received
Mute.shared.notify = { [weak self] m in
self?.label.text = m ? "Muted" : "Not Muted"
}
// Stop after 5 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
Mute.shared.isPaused = true
}
// Re-start after 10 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
Mute.shared.isPaused = false
}
}
// Trigger an ad-hoc check
@IBAction func checkPressed(_ sender: UIButton) {
Mute.shared.check()
}
}
需要
安装
静音支持通过 CocoaPods 提供。要安装它,只需将以下行添加到您的Podfile中
pod 'Mute'
作者
Akram Hussein,[email protected]
许可
Mute遵循MIT许可。有关更多信息,请参阅LICENSE文件。