AirPlay

1、 AirPlay是iOS中一个很酷的功能,通过Wi-Fi连接支持AirPlay的设备,然后使用镜像功能就能在其他设备显示内容,播放声音。
之前没有做过这方面的开发,投简历的时候,看到有家公司需要做AirPlay的开发,就自己查了查,并在Mac上下载了个AirServer,就开始搞了。
这里谈得主要是我们开发中得具体实现。
2、编程
实现多屏幕。
在可以使用AirPlay之后,我们要实现多屏幕,也就是在电脑上显示的和iOS设备上显示的内容不一样。
2.1 基本原理
获取新的屏幕信息--->创建一个新的Window--->将新的Window对应的Screen屏幕设置为新的屏幕--->设置新的屏幕的UI显示
我们知道,一般情况下,我们在开发过程中只使用一个window(UIWindow),而且一般在AppDelegate文件中创建,一般情况下我们甚至完全不用理会window。一样的,screen(UIScreen)我们除了通过它来获取一些屏幕信息之外也不会做任何处理,比较屏幕也只有一块。那么现在要实现多屏幕就不一样了。我们要创建新的window,获取新的screen,并且将window和screen联系在一起,这样要在这个window中显示的内容就可以显示在这个新的屏幕当中。

2.2 实现

STEP 1:检查是否有多的屏幕存在,如果有,那么直接设置。一般先在viewController中设置一个UIWindow和UIScreen的实例:

@property (nonatomic,strong) UIWindow *externalWindow;  
@property (nonatomic,strong) UIScreen *externalScreen;

然后,检查:
通过screens Method来判断是否屏幕数大于1,如果是意味着有别的屏幕连接到iOS设备,这时获取这个屏幕。
对于window初始化然后关键是要设置其Frame,一般就是设置成屏幕大小,全屏。然后将window的screen设置为外部屏幕。然后就可以进行window相关view,viewController的设置。最后设置window 的hidden为NO。

- (void)checkForExistingScreenAndInitializeIfPresent  
{  
    if ([UIScreen screens].count > 1) {  
        self.externalScreen = [[UIScreen screens] objectAtIndex:1];  
        NSLog(@"external screen :%@",self.externalScreen);  
           
        CGRect screenBounds = self.externalScreen.bounds;  
        self.externalWindow = [[UIWindow alloc] initWithFrame:screenBounds];  
        self.externalWindow.screen = self.externalScreen;  
          
        // Set the initial UI for the window for example  
        {  
            UILabel *screenLabel = [[UILabel alloc] initWithFrame:screenBounds];  
            screenLabel.text = @"Screen 2";  
            screenLabel.textAlignment = NSTextAlignmentCenter;  
            screenLabel.font = [UIFont systemFontOfSize:100]; 

            UIViewController *externalViewController = [[UIViewController alloc] init];  
            externalViewController.view.frame = screenBounds;  
            [externalViewController.view addSubview:screenLabel];  
            self.externalWindow.rootViewController = externalViewController;  
        }
        self.externalWindow.hidden = NO;  
    }  
}  

对于上面这种情况,主要是针对iOS在启动应用之前就已经AirPlay了,那么,如果是启动应用后才要打开AirPlay呢?
当然有办法------notification

STEP 2:Notification检查屏幕的连接情况。
UIScreen有两个notification可以检查屏幕的连接情况:
UIScreenDidConnectNotification UIScreenDidDisconnectNotification

一旦屏幕连接上或断开iOS设备,就会发出上面的notification。这样就简单了,设置一下:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenDidConnect:) name:UIScreenDidConnectNotification object:nil];  
      
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenDidDisconnect:) name:UIScreenDidDisconnectNotification object:nil];  

然后进行相关的处理:

#pragma mark - Notifications Handler  
  
- (void)screenDidConnect:(NSNotification *)notification  
{  
    NSLog(@"connect");  
    self.externalScreen = notification.object;  
      
    // Handle the configuration below......  
}  
  
- (void)screenDidDisconnect:(NSNotification *)notification  
{  
    NSLog(@"disconnect");  
    if (self.externalWindow) {  
        self.externalWindow.hidden = YES;  
        self.externalScreen = nil;  
        self.externalWindow = nil;  
    }  
}  

这样差不多就搞定了。基本上在iOS开发中主要是view的编辑,因为在新的屏幕中分辨率不一样,要进行特定的设置。

具体原文

你可能感兴趣的:(AirPlay)