Airplay is a cool feature in IOS. It is connected to devices that support airplay through Wi-Fi, and then the image function can be used to display content and play sound on other devices. Many large games support airplay. For example, when connected to a TV, the iPhone is completely a game handle, and the TV shows the game screen. Because most iOS users do not use Apple TV, there are few applications with multiple screens. However, this does not deny the powerful functions of airplay. This article introduces how to use airserver to implement airplay on Mac or windows, and how to implement multi-screen display through programming.
1. Use airserver software to implement airplay
Airserver is an extremely powerful software that can fully implement the functions of Apple TV through software. You can go to Google and have a free trial version. Is its icon:
After the airserver is installed, open it and you will see the logo on the Mac toolbar:
OK. Next, connect the iPhone to the Mac. You can use the hotspot on the iPhone, connect it to the Mac, or create a hotspot on the Mac to connect the iPhone to wifi.
After the connection, double-click the iphonehome key to go to the volume adjustment page, as shown below:
Click the airplay button on the right to bring up the settings page:
Select the second one, that is, Mac:
Open the image. In this case, the airplay image is implemented on Mac, for example:
Next, you can play the video and music to test the effect. If you have a card, you can restart the WiFi so that the transmission speed will be faster.
2. implement multi-screen programming.
After airplay can be used, we need to implement multiple screens, that is, the content displayed on the computer is different from that displayed on the iOS device.
2.1 Basic Principles
Get new screen information ---> Create a new window ---> set the screen corresponding to the new window as a new screen ---> set the UI display of the New Screen
We know that in general, we only use one window (uiwindow) during the development process, and it is generally created in the appdelegate file. In general, we don't even bother with the window. In the same way, in addition to getting some screen information through screen (uiscreen), we will not perform any processing, and there is only one screen for comparing. Now it is different to implement multiple screens. We need to create a new window, get a new screen, and associate the window with the screen, so that the content displayed in the window can be displayed in the new screen.
2.2 Implementation
Step 1: Check whether multiple screens exist. If so, set them directly. Set a uiwindow and uiscreen instance in viewcontroller first:
@property (nonatomic,strong) UIWindow *externalWindow;@property (nonatomic,strong) UIScreen *externalScreen;
Then, check:
- (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; }}
The screens method is used to determine whether the number of screens is greater than 1. If it means that another screen is connected to the iOS device, the screen is obtained.
For window initialization, the key is to set its frame, which is usually set to the screen size and full screen. Then, set the screen of the window to an external screen. Then you can set the window-related view and viewcontroller. The above code is just a small example. Finally, set the hidden of window to No.
In this case, it is mainly for iOS airplay before the app is started. What if airplay is enabled after the app is started?
Of course there is a way ------ notification
Step 2: Check the connection status of the screen by notification.
Uiscreen has two notifications to check the screen connection:
Uiscreendidconnectnotification
Uiscreendiddisconnectnotification
Once the screen is connected or the iOS device is disconnected, the above notification is sent. This is easy to set:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenDidConnect:) name:UIScreenDidConnectNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenDidDisconnect:) name:UIScreenDidDisconnectNotification object:nil];
Then perform related processing.
#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; }}
This is almost done. In iOS development, view editing is mainly used, because the resolution in the new screen is different and specific settings are required.
[This article is an original article. If you need to reprint it, please indicate the source! Blog from songrotek]