AVAudioSession始终返回相同的outputVolume
我有一个watchOS应用程序,可通过以下方式从iPhone父应用程序请求当前卷:AVAudioSession始终返回相同的outputVolume,ios,swift,avfoundation,watchkit,avaudiosession,Ios,Swift,Avfoundation,Watchkit,Avaudiosession,我有一个watchOS应用程序,可通过以下方式从iPhone父应用程序请求当前卷: session?.sendMessage(["getVolume":1], replyHandler: { replyDict in if let currentVolume = replyDict["currentVolume"] as? Float{ NSLog("Current volu
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([:])
}
}
这将始终返回手机在第一次请求时的相同输出音量
我调查了一些事情,比如后台请求是否有某种缓存,但总是有一个新调用返回新值
是否有其他解决办法来获取系统卷,或者可能有解决方案如何使其与
AVAudioSession
一起工作?这不是WatchKit或watchOS本身的问题,因为我在常规的iOS应用程序中遇到过这一点
我必须激活应用程序的音频会话才能观察音量的变化:
try? AVAudioSession.sharedInstance().setActive(true)
这有一个缺点,就是来自另一个应用程序的任何音频都是静音的,这不是我想要的do@AzzUrr1将音频会话类别设置为不干扰当前播放的任何其他音频。这有效!谢谢还需要添加音频背景模式