AVAudioSession 始终 returns 相同的 outputVolume
AVAudioSession always returns the same outputVolume
我有一个 watchOS 应用程序通过以下方式从 iPhone 父应用程序请求当前音量:
session?.sendMessage(["getVolume":1], replyHandler: {
replyDict in
if let currentVolume = replyDict["currentVolume"] as? Float{
NSLog("Current volume received from phone with value: \(currentVolume)")
}
}, errorHandler: {
(error) -> Void in
NSLog("Error: :\(error)")
// iOS app failed to get message. Send it in the background
//self.session?.transferUserInfo(["getVolume":1])
})
iPhone 应用程序是这样处理的:
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
NSLog("Received a message")
if let _ = message["getVolume"] as? Int{
replyHandler(["currentVolume":AVAudioSession.sharedInstance().outputVolume])
}
else{
replyHandler([:])
}
}
这总是 returns 与 phone 在第一个请求中的输出量相同。
我调查了几件事,比如后台请求是否有某种缓存,但 returns 新值总是一个新调用。
是否有任何解决方法可以通过不同的方式获取系统音量,或者可能有解决方案如何让它与 AVAudioSession
一起工作?
这不是 WatchKit 或 watchOS 本身的问题,因为我在常规 iOS 应用程序中遇到过这种情况。
我必须激活应用程序的音频会话才能观察音量变化:
try? AVAudioSession.sharedInstance().setActive(true)
我有一个 watchOS 应用程序通过以下方式从 iPhone 父应用程序请求当前音量:
session?.sendMessage(["getVolume":1], replyHandler: {
replyDict in
if let currentVolume = replyDict["currentVolume"] as? Float{
NSLog("Current volume received from phone with value: \(currentVolume)")
}
}, errorHandler: {
(error) -> Void in
NSLog("Error: :\(error)")
// iOS app failed to get message. Send it in the background
//self.session?.transferUserInfo(["getVolume":1])
})
iPhone 应用程序是这样处理的:
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
NSLog("Received a message")
if let _ = message["getVolume"] as? Int{
replyHandler(["currentVolume":AVAudioSession.sharedInstance().outputVolume])
}
else{
replyHandler([:])
}
}
这总是 returns 与 phone 在第一个请求中的输出量相同。
我调查了几件事,比如后台请求是否有某种缓存,但 returns 新值总是一个新调用。
是否有任何解决方法可以通过不同的方式获取系统音量,或者可能有解决方案如何让它与 AVAudioSession
一起工作?
这不是 WatchKit 或 watchOS 本身的问题,因为我在常规 iOS 应用程序中遇到过这种情况。
我必须激活应用程序的音频会话才能观察音量变化:
try? AVAudioSession.sharedInstance().setActive(true)