ios - 如何检测iOS中音量的变化?
问题描述
我已经阅读了 stackOverFlow 和 gitHub 提供的很多解决方案,它们都对我不起作用。我尝试了以下两种方式:
- 使用音频会话
override func viewWillAppear(_ animated: Bool) {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setActive(true, options: [])
} catch {
print("error")
}
// this print shows, got the current volume.
//but what I need is the notification of the change of volume
print("view will appear ,this volume is \(audioSession.outputVolume)")
audioSession.addObserver(self,
forKeyPath: "outputVolume",
options: [.new],
context: nil)
}
override class func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
if keyPath == "outputVolume"{
let audioSession = AVAudioSession.sharedInstance()
let volume = audioSession.outputVolume
//this print doesn't shows up
print("observed volume is \(volume)")
}
}
- 使用通知
override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(observed(_:)), name: Notification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
}
@objc func observed(_ notification: Notification){
print("observed")
}
顺便说一句,我正在使用模拟器(iPhone11,iOS13.1),Xcode版本是11.1
解决方案
尝试使用以下内容:
import UIKit
import MediaPlayer
import AVFoundation
class ViewController: UIViewController {
private var audioLevel: Float = 0.0
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
listenVolumeButton()
}
func listenVolumeButton() {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setActive(true, options: [])
audioSession.addObserver(self, forKeyPath: "outputVolume",
options: NSKeyValueObservingOptions.new, context: nil)
audioLevel = audioSession.outputVolume
} catch {
print("Error")
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "outputVolume" {
let audioSession = AVAudioSession.sharedInstance()
if audioSession.outputVolume > audioLevel {
print("Hello")
audioLevel = audioSession.outputVolume
}
if audioSession.outputVolume < audioLevel {
print("Goodbye")
audioLevel = audioSession.outputVolume
}
if audioSession.outputVolume > 0.999 {
(MPVolumeView().subviews.filter { NSStringFromClass($0.classForCoder) == "MPVolumeSlider" }.first as? UISlider)?.setValue(0.9375, animated: false)
audioLevel = 0.9375
}
if audioSession.outputVolume < 0.001 {
(MPVolumeView().subviews.filter { NSStringFromClass($0.classForCoder) == "MPVolumeSlider"}.first as? UISlider)?.setValue(0.0625, animated: false)
audioLevel = 0.0625
}
}
}
}
希望对你有帮助!
推荐阅读
- php - 使用 PHP CURL 在 Coinbase 中使用 PHP 进行身份验证时遇到问题
- c# - 如何使用 HelixToolkit.Wpf.SharpDX 设置 3d 模型?
- google-cloud-platform - 如何获取 Google Compute Engine 实例的动态子区域以克服 ZONE_RESOURCE_POOL_EXHAUSTED 错误
- firebase - 使用 Stripe 的订阅 Firebase 扩展向客户收费时运行函数
- c - 使用尾随 '\0' 初始化数组
- r - 具有多个嵌套因子级别的 EMMEAN 中的特定自定义对比
- c - C程序将矩阵乘以标量
- python - 将字符串附加到 Pandas 数据框中没有结果
- rust - 在短值中使用多个字母
- kotlin - kotlin REPL 中的粘贴模式/多行代码段?