簡(jiǎn)介
AirPlay是通過Wi-Fi連接支持AirPlay的設(shè)備,然后使用鏡像功能就能在其他設(shè)備顯示內(nèi)容,播放聲音。今天我要分享的是使用Airplay之后,我們要在電腦上顯示和iOS設(shè)備上顯示的內(nèi)容不一樣,簡(jiǎn)言之iOS設(shè)備類似于一個(gè)遙控器。
配合demo看感覺要好一些哇:https://github.com/Xcc1994/AirplayTestDemo
原理
獲取新的屏幕信息--->創(chuàng)建一個(gè)新的Window--->將新的Window對(duì)應(yīng)的Screen屏幕設(shè)置為新的屏幕--->設(shè)置新的屏幕的UI顯示
實(shí)現(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()
//啟動(dòng)監(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)建一個(gè)工程都可以在AppDelegate文件中看見一個(gè)window,一般情況下我們可以不用理會(huì)它。screen我們拿來獲取一些屏幕信息之外也不會(huì)做其它處理。但是現(xiàn)在不一樣了,我們要實(shí)現(xiàn)多屏顯示,從需求也可以知道會(huì)多一個(gè)window。
我們啟動(dòng)程序的時(shí)候先去判斷現(xiàn)在screen的數(shù)量,未連接AirPlay進(jìn)行投屏的時(shí)候,我們的screen只有一個(gè)main screen。一旦連接AirPlay進(jìn)行投屏?xí)r遍歷現(xiàn)在的screen,除去main screen之外,我們還可以獲取在電腦上顯示的screen。
(啟動(dòng)監(jiān)聽的兩個(gè)通知后面再介紹……)
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對(duì)應(yīng)的Screen屏幕設(shè)置為新的屏幕。如果投屏的window當(dāng)前的currentViewController為nil ,我進(jìn)行了一個(gè)小處理寫了一個(gè)類似于屏保的默認(rèn)DefalutViewController。
現(xiàn)在我們來說說之前啟動(dòng)監(jiān)聽時(shí)發(fā)送的通知:
//啟動(dòng)監(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進(jìn)行投屏,可以有2種手順:
1、先連接AirPlay進(jìn)行投屏,再啟動(dòng)App君,這樣的話,我們啟動(dòng)App君的時(shí)候獲取screen的時(shí)候就可以獲取到多個(gè)screen,排除開main screen之外就可以進(jìn)行連接。
2、先啟動(dòng)App君,再連接AirPlay進(jìn)行投屏?;蛘呤沁\(yùn)行App君的過程中遇到奧特曼打小怪獸斷開了連接之后,世界和平之后又連接上的情況,我們就需要通過系統(tǒng)的兩個(gè)通知來解決問題了。
@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
}
這個(gè)是自己寫的一個(gè)更新電腦端Window的ViewController的一個(gè)動(dòng)畫。很簡(jiǎn)單就不說了。我是用Xcode8.1建的項(xiàng)目,用swift3.0的語法寫的,@discardableResult聲明是告訴編譯器此方法可以不用接收返回值。
測(cè)試
1、(真機(jī))使用AirServer軟件實(shí)現(xiàn)AirPlay,一定要打開鏡像

2、(模擬器)

測(cè)試妹子催我改bug……可憐的程序員。。空了再寫……
(有錯(cuò)誤的地方歡迎提出來)
參考資料:http://blog.csdn.net/songrotek/article/details/8949442