gpt4 book ai didi

ios - AVAudioSession.sharedInstance().outputVolume 未始终返回正确的音量

转载 作者:搜寻专家 更新时间:2023-10-30 23:04:09 25 4
gpt4 key购买 nike

使用以下代码,我得到了输出音量,但它确实不一致 - 有时它给出相同的值,有时它是一个落后的音量变化,尽管系统音量实际上是正确变化的。

有什么办法让它每次都输出正确的值?

func viewDidLoad() {
...

NotificationCenter.default.addObserver(self, selector: #selector(volumeDidChange), name: NSNotification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
...

}


func volumeDidChange() {
print("VOLUME CHANGING", AVAudioSession.sharedInstance().outputVolume)

// output while changing the volume with hardware buttons
VOLUME CHANGING 0.0625
VOLUME CHANGING 0.0625
VOLUME CHANGING 0.125
VOLUME CHANGING 0.1875
VOLUME CHANGING 0.25
VOLUME CHANGING 0.375
VOLUME CHANGING 0.375
VOLUME CHANGING 0.4375
VOLUME CHANGING 0.5
VOLUME CHANGING 0.5625
VOLUME CHANGING 0.625
VOLUME CHANGING 0.6875
VOLUME CHANGING 0.75
VOLUME CHANGING 0.8125
VOLUME CHANGING 0.875
VOLUME CHANGING 0.75
VOLUME CHANGING 0.6875
VOLUME CHANGING 0.625
VOLUME CHANGING 0.625
VOLUME CHANGING 0.5625
VOLUME CHANGING 0.5
VOLUME CHANGING 0.4375
VOLUME CHANGING 0.375
VOLUME CHANGING 0.3125
VOLUME CHANGING 0.375
VOLUME CHANGING 0.4375
VOLUME CHANGING 0.4375

最佳答案

尝试

import AVFoundation
import MediaPlayer

//MARK: Did Load
override func viewDidLoad() {
super.viewDidLoad()

/// Volume View
let volumeView = MPVolumeView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
volumeView.isHidden = false
volumeView.alpha = 0.01
view.addSubview(volumeView)

/// Notification Observer
NotificationCenter.default.addObserver(self, selector: #selector(self.volumeDidChange(notification:)), name: NSNotification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
}

@objc func volumeDidChange(notification: NSNotification) {
//print("VOLUME CHANGING", AVAudioSession.sharedInstance().outputVolume)

let volume = notification.userInfo!["AVSystemController_AudioVolumeNotificationParameter"] as! Float
print("Device Volume:\(volume)")
}

您当前的输出 - 重复相同的值

enter image description here

要求的输出

enter image description here

关于ios - AVAudioSession.sharedInstance().outputVolume 未始终返回正确的音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51034452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com