AirPlay

1、 AirPlay是iOS中一個(gè)很酷的功能,通過(guò)Wi-Fi連接支持AirPlay的設(shè)備,然后使用鏡像功能就能在其他設(shè)備顯示內(nèi)容,播放聲音。
之前沒(méi)有做過(guò)這方面的開發(fā),投簡(jiǎn)歷的時(shí)候,看到有家公司需要做AirPlay的開發(fā),就自己查了查,并在Mac上下載了個(gè)AirServer,就開始搞了。
這里談得主要是我們開發(fā)中得具體實(shí)現(xiàn)。
2、編程
實(shí)現(xiàn)多屏幕。
在可以使用AirPlay之后,我們要實(shí)現(xiàn)多屏幕,也就是在電腦上顯示的和iOS設(shè)備上顯示的內(nèi)容不一樣。
2.1 基本原理
獲取新的屏幕信息--->創(chuàng)建一個(gè)新的Window--->將新的Window對(duì)應(yīng)的Screen屏幕設(shè)置為新的屏幕--->設(shè)置新的屏幕的UI顯示
我們知道,一般情況下,我們?cè)陂_發(fā)過(guò)程中只使用一個(gè)window(UIWindow),而且一般在AppDelegate文件中創(chuàng)建,一般情況下我們甚至完全不用理會(huì)window。一樣的,screen(UIScreen)我們除了通過(guò)它來(lái)獲取一些屏幕信息之外也不會(huì)做任何處理,比較屏幕也只有一塊。那么現(xiàn)在要實(shí)現(xiàn)多屏幕就不一樣了。我們要?jiǎng)?chuàng)建新的window,獲取新的screen,并且將window和screen聯(lián)系在一起,這樣要在這個(gè)window中顯示的內(nèi)容就可以顯示在這個(gè)新的屏幕當(dāng)中。

2.2 實(shí)現(xiàn)

STEP 1:檢查是否有多的屏幕存在,如果有,那么直接設(shè)置。一般先在viewController中設(shè)置一個(gè)UIWindow和UIScreen的實(shí)例:

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

然后,檢查:
通過(guò)screens Method來(lái)判斷是否屏幕數(shù)大于1,如果是意味著有別的屏幕連接到iOS設(shè)備,這時(shí)獲取這個(gè)屏幕。
對(duì)于window初始化然后關(guān)鍵是要設(shè)置其Frame,一般就是設(shè)置成屏幕大小,全屏。然后將window的screen設(shè)置為外部屏幕。然后就可以進(jìn)行window相關(guān)view,viewController的設(shè)置。最后設(shè)置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;  
    }  
}  

對(duì)于上面這種情況,主要是針對(duì)iOS在啟動(dòng)應(yīng)用之前就已經(jīng)AirPlay了,那么,如果是啟動(dòng)應(yīng)用后才要打開AirPlay呢?
當(dāng)然有辦法------notification

STEP 2:Notification檢查屏幕的連接情況。
UIScreen有兩個(gè)notification可以檢查屏幕的連接情況:
UIScreenDidConnectNotification UIScreenDidDisconnectNotification

一旦屏幕連接上或斷開iOS設(shè)備,就會(huì)發(fā)出上面的notification。這樣就簡(jiǎn)單了,設(shè)置一下:

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

然后進(jìn)行相關(guān)的處理:

#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;  
    }  
}  

這樣差不多就搞定了?;旧显趇OS開發(fā)中主要是view的編輯,因?yàn)樵谛碌钠聊恢蟹直媛什灰粯?,要進(jìn)行特定的設(shè)置。

具體原文

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容