完成首页监听外部vr设备连接状态

This commit is contained in:
bluesea 2024-03-12 11:05:03 +08:00
parent e9c75ce3d6
commit ff75b1ec71
3 changed files with 56 additions and 26 deletions

View File

@ -60,6 +60,10 @@ extension UIColor {
return UIColor.init(red: _r/255, green: _g/255, blue: _b/255, alpha: alpha) return UIColor.init(red: _r/255, green: _g/255, blue: _b/255, alpha: alpha)
} }
class func colorWithRGBINT(_r:Int , _g:Int , _b:Int) -> UIColor{
return UIColor.init(red: CGFloat(_r)/255, green: CGFloat(_g)/255, blue: CGFloat(_b)/255, alpha: 1.0)
}
} }

View File

@ -61,40 +61,45 @@ class CCHomeController: BaseController, LLCycleScrollViewDelegate {
self.navigationController?.tabBarController!.tabBar.isTranslucent = true; self.navigationController?.tabBarController!.tabBar.isTranslucent = true;
self.setNavgationBarColor(color: UIColor.clear) self.setNavgationBarColor(color: UIColor.clear)
self.checkAirPlayStatus()
} }
func isExternalDeviceConnected() -> Bool { // func isExternalDeviceConnected() -> Bool {
let screens = UIScreen.screens // let screens = UIScreen.screens
//
// 1 // // 1
if screens.count > 1 { // if screens.count > 1 {
return true // return true
} // }
//
// // //
for screen in screens { // for screen in screens {
if !screen.isEqual(UIScreen.main) { // if !screen.isEqual(UIScreen.main) {
return true // return true
} // }
} // }
//
// // //
return false // return false
} // }
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
self.view.backgroundColor = UIColor.black self.view.backgroundColor = UIColor.black
self.navLine?.isHidden = true self.navLine?.isHidden = true
let isExternalDeviceConnected = isExternalDeviceConnected() // let isExternalDeviceConnected = isExternalDeviceConnected()
if isExternalDeviceConnected { // if isExternalDeviceConnected {
let deviceName = UIDevice.current.name // let deviceName = UIDevice.current.name
print("已连接外部设备,设备名称:\(deviceName)") // print("\(deviceName)")
} else { // } else {
print("未连接外部设备") // print("")
} // }
// AirPlay
NotificationCenter.default.addObserver(self, selector: #selector(airPlayStatusDidChange(_:)), name: AVAudioSession.routeChangeNotification, object: nil)
//393*236 //393*236
mTopImgView = UIImageView(frame: CGRect(x: 0, y: 0, width: SCREEN_Width, height: SCREEN_Height * 236/393)) mTopImgView = UIImageView(frame: CGRect(x: 0, y: 0, width: SCREEN_Width, height: SCREEN_Height * 236/393))
@ -148,12 +153,33 @@ class CCHomeController: BaseController, LLCycleScrollViewDelegate {
mTopCenterBtn!.setTitle("未连接VR设备", for: UIControl.State.normal) mTopCenterBtn!.setTitle("未连接VR设备", for: UIControl.State.normal)
mTopCenterBtn!.setTitleColor(UIColor.white, for: UIControl.State.normal) mTopCenterBtn!.setTitleColor(UIColor.white, for: UIControl.State.normal)
mTopCenterBtn!.titleLabel?.font = UIFont.systemFont(ofSize: 12) mTopCenterBtn!.titleLabel?.font = UIFont.systemFont(ofSize: 12)
mTopCenterBtn!.isEnabled = false
centerView() centerView()
bottomButton() bottomButton()
} }
//MARK: -
@objc private func airPlayStatusDidChange(_ notification: Notification) {
checkAirPlayStatus()
}
private func checkAirPlayStatus() {
print("设备连接变化")
let currentRoute = AVAudioSession.sharedInstance().currentRoute
let isAirPlayActive = currentRoute.outputs.contains { output in
return output.portType == AVAudioSession.Port.HDMI ||
output.portType == AVAudioSession.Port.airPlay
}
mTopCenterBtn!.setTitle((isAirPlayActive ? "已连接外部设备" : "未连接VR设备"), for: UIControl.State.normal)
mTopCenterBtn!.backgroundColor = isAirPlayActive ? UIColor.colorWithRGBINT(_r: 73, _g: 34, _b: 208) : UIColor.hexStringToColor(hexString: "#060507")
isAirPlayActive ? mTopCenterBtn!.setImage(UIImage.init(named: "linked_button"), for: .normal) : mTopCenterBtn!.setImage(nil,for: .normal)
isAirPlayActive ? mTopCenterBtn!.updateBtnEdgeInsets(style: .Left, space: 5):mTopCenterBtn!.updateBtnEdgeInsets(style: .Left, space: 0)
}
func centerView(){ func centerView(){
//345*200 //345*200