美文网首页音视频iOS 技巧优化iOS Developer
学习笔记-使用Airplay镜像实现多屏显示

学习笔记-使用Airplay镜像实现多屏显示

作者: 请叫我小陈陈 | 来源:发表于2017-01-13 13:00 被阅读2540次

简介

AirPlay是通过Wi-Fi连接支持AirPlay的设备,然后使用镜像功能就能在其他设备显示内容,播放声音。今天我要分享的是使用Airplay之后,我们要在电脑上显示和iOS设备上显示的内容不一样,简言之iOS设备类似于一个遥控器。
配合demo看感觉要好一些哇:https://github.com/Xcc1994/AirplayTestDemo

原理

获取新的屏幕信息--->创建一个新的Window--->将新的Window对应的Screen屏幕设置为新的屏幕--->设置新的屏幕的UI显示

实现

 //连接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监听
    func beginReceivingScreenNotification() {
        //首先检查是否已经连接了
        connectScreen()
        //启动监听
        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)
    }

每次我们创建一个工程都可以在AppDelegate文件中看见一个window,一般情况下我们可以不用理会它。screen我们拿来获取一些屏幕信息之外也不会做其它处理。但是现在不一样了,我们要实现多屏显示,从需求也可以知道会多一个window。
我们启动程序的时候先去判断现在screen的数量,未连接AirPlay进行投屏的时候,我们的screen只有一个main screen。一旦连接AirPlay进行投屏时遍历现在的screen,除去main screen之外,我们还可以获取在电脑上显示的screen。
(启动监听的两个通知后面再介绍……)

extension AirplayService:AirplayScreenDelegate {
    //已经连接
    @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。
现在我们来说说之前启动监听时发送的通知:

 //启动监听
 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君的过程中遇到奥特曼打小怪兽断开了连接之后,世界和平之后又连接上的情况,我们就需要通过系统的两个通知来解决问题了。

   @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软件实现AirPlay,一定要打开镜像

FullSizeRender.jpg

2、(模拟器)

屏幕快照 2017-01-13 12.56.17.png
测试妹子催我改bug……可怜的程序员。。空了再写……
(有错误的地方欢迎提出来)
参考资料:http://blog.csdn.net/songrotek/article/details/8949442

相关文章

  • 学习笔记-使用Airplay镜像实现多屏显示

    简介 AirPlay是通过Wi-Fi连接支持AirPlay的设备,然后使用镜像功能就能在其他设备显示内容,播放声音...

  • iOS下AirPlay投屏功能实现

    这篇文章注重于如何实现AirPlay投屏功能.具体AirPlay的实现逻辑这里不再赘述,网上帖子很多 首先.想要呼...

  • python线性回归基本实现

    通过这个笔记记录以下学习的过程。在使用python实现机器学习,可以使用anaconda (清华镜像下载)去实现。...

  • AirPlay

    1、 AirPlay是iOS中一个很酷的功能,通过Wi-Fi连接支持AirPlay的设备,然后使用镜像功能就能在其...

  • iOS AirPlay 投屏调研

    Airplay Mirroring iOS AirPlay 投屏调研 https://www.jianshu.co...

  • iOS AirPlay投屏功能实现

    相信很部分人有点懵,网上帖子很多关于AirPlay,看完后还是有点不明白,怎么支持调试使用AirPlay来成功投屏...

  • 如何使用Pr进行分屏剪辑

    使用Pr多显示屏功能,在进行笔记本等小屏或多任务剪辑中可以大大提高剪辑体验感。仅需一条转接线和显示屏即可。 设备:...

  • 笔记2018-07-17 关于奥迪A8L

    奥迪A8L前排显示屏包括:仪表盘、中控、中下屏;后排显示屏包括控制屏和两个显示屏,都是独立可拆卸。真正实现了多屏互...

  • @4/23-小系统-如何录屏

    解决问题: 手机上的操作如何录屏?不能在电脑端打开的直播如何录屏? Airplay图文版教程 苹果录屏大师如何使用...

  • 手机投屏TCL电视全屏设置

    手机投屏TCL电视全屏设置 当手机使用视频APP里面的【TV】投屏电视,电视是全屏显示的,而当我们手机镜像投屏的时...

网友评论

  • 8e644ed68eb9:非常完整,但是我有个问题需要请教,检测之前必须先通过airplay手动搜索并连接appletv,是这样的吗?我现在想把搜索和连接appletv的操作在app中实现,而不是手动操作,应该怎么做?谢谢
    voQuan:同求
  • 3518656a0f26:小姐姐你好,我是一名大学生,最近做项目的过程中需要使用Airplay进行投屏(手机屏幕和投影屏幕显示需要不同)。你的这个Demo正好对应了我的问题!奈何我以前没有接触过IOS的编程,所以还想请问,这个DEMO运行在Iphone上需要怎么修改呢?
    请叫我小陈陈:@daweiseu 我在休假中 没设备看代码 这是一年前的代码 我设置过什么我都忘了 原理我都写了
    3518656a0f26:@请叫我小陈陈 小姐姐回复了好激动!我的xcode里没有显示iphone的模拟器,不知道是否需要在项目的哪些设置里修改(把它变成iphone的项目?)暂时还没有试把它直接下载到手机,等我回去试试。
    哎我们指导老师也没做过ios的,只能让我们自己摸索了,现学现卖。小姐姐可不可以留个邮箱,有什么问题我请教您?
    请叫我小陈陈:@daweiseu 讲道理不用修改 原理是一样的 可能运行在iPhone上UI有点问题而已 你木有接触过 打算怎么做项目啊
  • 美的不像话:想在app以外录屏 可以用这个吗
    luguoliang:还在吗?咱俩可以聊聊我最近也有类似的需求qq:1158429526
  • 伦敦乡下的小作家:楼主,我有个问题,为什么我工程中添加系统的airplay按钮,在iPhone上显示正常,在iPad上就显示不出来??
    请叫我小陈陈:你可以用UI工具调试一下
    伦敦乡下的小作家:@请叫我小陈陈 对了,之前显示了几次,现在一次都不显示了
    请叫我小陈陈:@伦敦乡下的小作家 约束写对了吗
  • Lius_:@请叫我小陈陈 怎么在app中直接打开airplay镜像功能
  • Sumency:我最近在做airplay这一块,有个疑问
    请叫我小陈陈:@卓兰清风 这个简单 我demo应该实现了iOS设备上的界面控制投屏界面的显示 你可以看看
    Sumency:@请叫我小陈陈 我的app中有一个播放幻灯片的功能,我需要把幻灯片播放的画面投影到apple tv上,而手机中的画面是几个按钮,来控制幻灯片单页或者退出,妹子拜托啦:smile:
  • Sumency:妹子我想加你好友
  • 景彧:为什么看不懂你例子说了什么,是在Mac上显示你的IPhone的镜像?PS:我只会ObjC,swift看的不是很懂。求妹子解释一下。拜谢!
    请叫我小陈陈:@Ericlo 我还是个小萌新
    景彧:@请叫我小陈陈 你对mirror有深入点的了解吗?
    请叫我小陈陈:主要是说 通过AirPlay实现投屏。推荐你看一篇oc 的吧http://blog.csdn.net/songrotek/article/details/8949442
  • 旅行的光:这个能实现停止投屏功能吗?就是按AirPlay的picker后选择iPhone之后,电视上的投屏就退出。谢谢
    请叫我小陈陈:@旅行的光 有相关代理
  • zhangferry:兄弟,我刚运行代码,好像并不能行,没有投到airserver上
    请叫我小陈陈:@勇闯天涯茉莉花茶 你试试真机哇。我们项目上线了,都没有问题
    zhangferry:@请叫我小陈陈 没有报错,模拟器测试没问题。就是测试的时候通过airplay mirroring可以调用屏幕连接的通知,一切正常。但是点MPVolumeView的airplay图标没有调通知,这个是因为什么,你知道吗
    请叫我小陈陈:现在什么情况?报错?
  • 一半春秋月:厉害了我的陈妹
    请叫我小陈陈:都是你们教的好啊
  • Afer:陈神 V587
    Afer:@Afer :frowning:
    请叫我小陈陈:师兄 都是你教的好啊。

本文标题:学习笔记-使用Airplay镜像实现多屏显示

本文链接:https://www.haomeiwen.com/subject/geqjbttx.html