From 6bd53acba3ad2eced6f769a6d82c6396c918dca8 Mon Sep 17 00:00:00 2001 From: bluesea <307723040@qq.com> Date: Tue, 19 Mar 2024 16:00:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=BA=86=E4=B8=80=E7=A7=8D?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E6=8A=95=E5=B1=8F=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Project/Controller/CCHomeController.swift | 2 +- .../CCSpatialPhotoDisplayController.swift | 47 ++++++++++++------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/SwiftProject/SwiftProject/Project/Controller/CCHomeController.swift b/SwiftProject/SwiftProject/Project/Controller/CCHomeController.swift index def0ff9..9798db9 100644 --- a/SwiftProject/SwiftProject/Project/Controller/CCHomeController.swift +++ b/SwiftProject/SwiftProject/Project/Controller/CCHomeController.swift @@ -166,7 +166,7 @@ class CCHomeController: BaseController, LLCycleScrollViewDelegate,MFMailComposeV } private func checkAirPlayStatus() { - print("设备连接变化") + print("设备连接变化 homecontroller") let currentRoute = AVAudioSession.sharedInstance().currentRoute let isAirPlayActive = currentRoute.outputs.contains { output in return output.portType == AVAudioSession.Port.HDMI || diff --git a/SwiftProject/SwiftProject/Project/Controller/RecordingVideo/CCSpatialPhotoDisplayController.swift b/SwiftProject/SwiftProject/Project/Controller/RecordingVideo/CCSpatialPhotoDisplayController.swift index 877f15a..62ffa30 100644 --- a/SwiftProject/SwiftProject/Project/Controller/RecordingVideo/CCSpatialPhotoDisplayController.swift +++ b/SwiftProject/SwiftProject/Project/Controller/RecordingVideo/CCSpatialPhotoDisplayController.swift @@ -150,14 +150,18 @@ class CCSpatialPhotoDisplayController: BaseController { deinit { + print("spatioal photo display deinit...") NotificationCenter.default.removeObserver(self) } override func viewDidLoad() { super.viewDidLoad() //添加外接屏幕链接通知 - NotificationCenter.default.addObserver(self, selector: #selector(exScreenWillConnectNotification(notification:)), name: UIScene.willConnectNotification, object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(exScreenDisConnectNotification(notification:)), name: UIScene.didDisconnectNotification, object: nil) +// NotificationCenter.default.addObserver(self, selector: #selector(exScreenWillConnectNotification(notification:)), name: UIScene.willConnectNotification, object: nil) +// NotificationCenter.default.addObserver(self, selector: #selector(exScreenDisConnectNotification(notification:)), name: UIScene.didDisconnectNotification, object: nil) + + // 监听 AirPlay 设备的连接状态 + NotificationCenter.default.addObserver(self, selector: #selector(airPlayStatusDidChange(notification:)), name: AVAudioSession.routeChangeNotification, object: nil) self.view.backgroundColor = UIColor(hexString: "#060507") outputVideoURL = URL.documentsDirectory.appending(path:"output11114.jpg") @@ -207,21 +211,18 @@ class CCSpatialPhotoDisplayController: BaseController { } //MARK: - 外接屏幕 链接 与 断开 - @objc func exScreenWillConnectNotification(notification:Notification) { - - + @objc func airPlayStatusDidChange(notification:Notification) { checkAirPlayStatus() - print("外连接。。。。。") } - @objc func exScreenDisConnectNotification(notification:Notification) { - print("已断开 屏幕。。。。。") - - checkAirPlayStatus() - } +// @objc func exScreenDisConnectNotification(notification:Notification) { +// print("已断开 屏幕。。。。。") +// +// checkAirPlayStatus() +// } private func checkAirPlayStatus() { - print("设备连接变化") + print("设备连接变化 photodisplaycontroller>>>>>") let currentRoute = AVAudioSession.sharedInstance().currentRoute self.isAirPlayActive = currentRoute.outputs.contains { output in return output.portType == AVAudioSession.Port.HDMI || @@ -232,20 +233,34 @@ class CCSpatialPhotoDisplayController: BaseController { func setttinisScreenMirroring(isScreenMirroring:Bool){ //已连接 if(isScreenMirroring){ - if let screen = UIScreen.screens.last { - self.externalWindow.screen = screen + print("变化为已连接。。。。") + // 获取所有打开的会话 + let sessions:Set = UIApplication.shared.openSessions + + // 过滤会话以找到来自其他屏幕的场景 + let otherScreenSessions:Set = sessions.filter { + $0.role.rawValue.contains("External") + } + + if let session = otherScreenSessions.first, + let scene = session.scene { + self.externalWindow.windowScene = scene as! UIWindowScene let nvc = UIViewController() self.externalWindow.rootViewController = nvc - var imageView = UIImageView(frame: CGRectMake(500, 0, KScreenHeight, KScreenHeight)) + var imageView = UIImageView(frame: CGRectMake(0, 0, KScreenHeight*2, KScreenHeight*2)) imageView.contentMode = .scaleAspectFit externalImageView = imageView nvc.view.addSubview(imageView) self.externalWindow.isHidden = false - var dispalylink:CADisplayLink? = screen.displayLink(withTarget: self, selector: #selector(displayUpdate(caDisplayLink:))) + var dispalylink:CADisplayLink? = self.externalWindow.windowScene?.windows.first?.screen.displayLink(withTarget: self, selector: #selector(displayUpdate(caDisplayLink:))) dispalylink?.add(to: RunLoop.main, forMode: RunLoop.Mode.common) } + else { + print("未发现外接屏....") + } } else{ + print("变化为 断开。。。。") if let imv = externalImageView{ imv .removeFromSuperview() }