2016-09-23 74 views
1

我有一個watchOS應用程序,通過從iPhone父應用程序請求當前的音量:AVAudioSession總是返回相同的outputVolume

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([:]) 
     } 
    } 

這始終返回與第一次請求相同的outputVolume。

我調查了幾件事情,如是否有某種緩存的背景請求,但它總是一個新的調用,返回新值。

是否有任何解決方法以不同的方式獲取系統音量或解決方案如何使其與AVAudioSession一起工作?

回答

1

這不是WatchKit或watchOS本身的問題,因爲我在常規的iOS應用程序中經歷過這個問題。

我不得不啓動以觀察成交量的變化我的應用程序的音頻會話:

try? AVAudioSession.sharedInstance().setActive(true) 
+0

這有從其他應用程序的任何音頻靜音其缺點是不是我打算做 – arnoapp

+1

@ AzzUrr1設置音頻會話類別不會干擾任何其他正在播放的音頻。 – JAL

+1

這工作!謝謝!還需要添加音頻背景模式 – arnoapp

相關問題