簡介
AirPlay是通過Wi-Fi連接支持AirPlay的設備一忱,然后使用鏡像功能就能在其他設備顯示內(nèi)容,播放聲音噩凹。今天我要分享的是使用Airplay之后临扮,我們要在電腦上顯示和iOS設備上顯示的內(nèi)容不一樣,簡言之iOS設備類似于一個遙控器剃允。
配合demo看感覺要好一些哇:https://github.com/Xcc1994/AirplayTestDemo
原理
獲取新的屏幕信息--->創(chuàng)建一個新的Window--->將新的Window對應的Screen屏幕設置為新的屏幕--->設置新的屏幕的UI顯示
實現(xiàn)
//連接Airplay Screen
func connectScreen() {
if UIScreen.screens.count > 1 {
UIScreen.screens.forEach({[weak self] (screen:UIScreen) in
if screen != UIScreen.main {
self?.didConnectScreen(screen: screen)
}
})
}
}
//開始Airplay監(jiān)聽
func beginReceivingScreenNotification() {
//首先檢查是否已經(jīng)連接了
connectScreen()
//啟動監(jiān)聽
NotificationCenter.default.addObserver(self, selector: #selector(AirplayService.didReceiveConnectScreenNotification(noti:)), name: NSNotification.Name.UIScreenDidConnect, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(AirplayService.didReceiveDisConnectScreenNotification(noti:)), name: NSNotification.Name.UIScreenDidDisconnect, object: nil)
}
每次我們創(chuàng)建一個工程都可以在AppDelegate文件中看見一個window沛简,一般情況下我們可以不用理會它。screen我們拿來獲取一些屏幕信息之外也不會做其它處理斥废。但是現(xiàn)在不一樣了椒楣,我們要實現(xiàn)多屏顯示,從需求也可以知道會多一個window牡肉。
我們啟動程序的時候先去判斷現(xiàn)在screen的數(shù)量捧灰,未連接AirPlay進行投屏的時候,我們的screen只有一個main screen统锤。一旦連接AirPlay進行投屏時遍歷現(xiàn)在的screen凤壁,除去main screen之外吩屹,我們還可以獲取在電腦上顯示的screen跪另。
(啟動監(jiān)聽的兩個通知后面再介紹……)
extension AirplayService:AirplayScreenDelegate {
//已經(jīng)連接
@objc func didConnectScreen(screen: UIScreen) {
if currentViewController == nil {
let defalutScreen = DefalutViewController()
currentViewController = defalutScreen
}
if screenWindow == nil {
let window = UIWindow(frame: screen.bounds)
screenWindow = window
screenWindow?.rootViewController = currentViewController
screenWindow?.isHidden = false
}
screenStatus = .Connected
screenWindow?.screen = screen
NotificationCenter.default.post(name: NSNotification.Name(rawValue: AirplayConstants.Notifications.ScreenDidConnected), object: nil)
}
//斷開連接
@objc func didDisconnectScrren(screen: UIScreen) {
screenStatus = .Disconnected
NotificationCenter.default.post(name: NSNotification.Name(rawValue: AirplayConstants.Notifications.ScreenDidDisconnected), object: nil)
}
}
一旦我們獲取到screen之后拧抖,我們就可以將新的Window對應的Screen屏幕設置為新的屏幕。如果投屏的window當前的currentViewController為nil 免绿,我進行了一個小處理寫了一個類似于屏保的默認DefalutViewController唧席。
現(xiàn)在我們來說說之前啟動監(jiān)聽時發(fā)送的通知:
//啟動監(jiān)聽
NotificationCenter.default.addObserver(self, selector: #selector(AirplayService.didReceiveConnectScreenNotification(noti:)), name: NSNotification.Name.UIScreenDidConnect, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(AirplayService.didReceiveDisConnectScreenNotification(noti:)), name: NSNotification.Name.UIScreenDidDisconnect, object: nil)
@objc func didReceiveConnectScreenNotification(noti:NSNotification) {
let screen:UIScreen = noti.object as! UIScreen
self.didConnectScreen(screen: screen)
}
@objc func didReceiveDisConnectScreenNotification(noti:NSNotification) {
let screen:UIScreen = noti.object as! UIScreen
self.didDisconnectScrren(screen: screen)
}
我們連接AirPlay進行投屏,可以有2種手順:
1嘲驾、先連接AirPlay進行投屏淌哟,再啟動App君,這樣的話辽故,我們啟動App君的時候獲取screen的時候就可以獲取到多個screen徒仓,排除開main screen之外就可以進行連接。
2誊垢、先啟動App君掉弛,再連接AirPlay進行投屏∥棺撸或者是運行App君的過程中遇到奧特曼打小怪獸斷開了連接之后殃饿,世界和平之后又連接上的情況,我們就需要通過系統(tǒng)的兩個通知來解決問題了芋肠。
@discardableResult func updateViewController(viewController:AirplayViewController,animation:Bool) -> Bool{
guard screenWindow != nil else {
return false
}
currentViewController?.airplayViewWillClose()
currentViewController = viewController
screenWindow?.rootViewController?.removeFromParentViewController()
screenWindow?.rootViewController?.navigationController?.removeFromParentViewController()
screenWindow?.rootViewController = nil
screenWindow?.rootViewController = currentViewController
currentViewController?.view.frame = (screenWindow?.bounds)!
currentViewController?.view.layoutIfNeeded()
currentViewController?.airplayViewWillShow()
if animation {
let maskView:UIView = UIView(frame: (currentViewController?.view.frame)!)
maskView.backgroundColor = UIColor.black
self.currentViewController?.view.addSubview(maskView)
UIView.animate(withDuration: 0.5, animations: {
maskView.alpha = 0
}, completion: { (finish:Bool) in
maskView.removeFromSuperview()
})
}
NotificationCenter.default.post(name: NSNotification.Name(rawValue: AirplayConstants.Notifications.ViewControllerDidUpdate), object: nil)
return true
}
這個是自己寫的一個更新電腦端Window的ViewController的一個動畫乎芳。很簡單就不說了。我是用Xcode8.1建的項目帖池,用swift3.0的語法寫的奈惑,@discardableResult聲明是告訴編譯器此方法可以不用接收返回值。
測試
1睡汹、(真機)使用AirServer軟件實現(xiàn)AirPlay肴甸,一定要打開鏡像
2、(模擬器)
測試妹子催我改bug……可憐的程序員帮孔。雷滋。空了再寫……
(有錯誤的地方歡迎提出來)
參考資料:http://blog.csdn.net/songrotek/article/details/8949442