How to use iOS 5+ AirPlay for a second screen
Asked Answered
B

1

2

I'm toying with AirPlay using an iPhone 4S and I'm having some trouble. I've updated my Apple TV 2 to the latest (4.4.3 I think? I forget, it's in the other room and I'm too lazy to check) and I have iOS 5.0 on my 4S while I use Xcode 4.2 Build 4D199. I wrote a simple UIView app that attempts to draw on a second screen when available. Here's what I have in my viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:[[MPVolumeView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]];
    NSLog(@"Registering for screen connect events...");
    // Do any additional setup after loading the view, typically from a nib.
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(screenDidConnect:)
     name:UIScreenDidConnectNotification
     object:nil];
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(screenDidDisconnect:)
     name:UIScreenDidDisconnectNotification
     object:nil];    NSLog(@"There are %i connected screens.", ScreenCount);
    if (ScreenCount > 1) {
        self.secondWindow = [self myScreenInit:[[UIScreen screens] objectAtIndex:1]];
        [self.secondWindow addSubview:[self createFullscreenViewForText:@"Second window" withSize:CGSizeMake(640, 480)]];
    }
}

This code works in the simulator when I enable TV Out although it always crashes if I enable TV Out while in debug. I have to relaunch the app after enabling TV out in the hardware menu in the simulator. On relaunch I see the second screen UILabel on the second screen. On the iPhone 4S I can only make this work if I enable AirPlay using the AirPlay control in the running tasks bar all the way to the left. You'll note that I have added an MPVolumeView component to the view on launch which I attempt to use to connect to my Apple TV to no avail.

If I use the in app MPVolumeView I can successfully disconnect from the Apple TV but when I use it again and select the apple TV to reconnect I do not get the UIScreenDidConnectNotification. Again the only time I can see the 2nd screen is when I enable mirroring. If I connect with either AirPlay control I only count 1 screen and never get the notification. Also, each time I enable mirroring I see this in the console logs:

Could not find mapped image ColorOnGrayShadow_AirPlay.png

This seems to be broken and I would like to know what others experience has been in this area. I'll continue to toy around and update this post as I learn new things.

Backspace answered 26/11, 2011 at 1:16 Comment(3)
I'm having the exact same issue with the MPVolumeView not giving another screen. I was having an issue getting content onto the second display, but it turns out that was because I wasn't retaining the window object. Still, would be nice to not have to use mirroring to get this working.Esparza
Hey Cliff - any idea how to control one screen from another one? I cant figure out how to do that. thanks!Flunky
If you mean control a second screen via AirPlay then it's a matter of using AirPlay's APIs to access the 2nd screen. one you have a reference to it then you can draw or place anything you want on it.Backspace
P
5

MPVolumeView's route menu can only control an audio stream going to the AppleTV. Here's a working example of what you are trying to do: https://github.com/quellish/AirplayDemo/

The "ColorOnGrayShadow_AirPlay.png" log message is normal for iOS 5. Using an Airplay connected AppleTV as an external display unfortunately requires mirroring, and you can't activate that from inside your app. The user has to follow these instructions:

http://support.apple.com/kb/TS4085

Prisca answered 27/1, 2012 at 0:57 Comment(2)
Thanks quellish. I've learned just the same by trial and error. Indeed it is not clear that the user must manually enable mirroring for everything to work. Hopefully a future update will address the unclarity.Backspace
If you enter a short enhancement request at bugreporter.apple.com on the documentation, they'll be able to prioritize updating the documentation to make it clearer that mirroring is required. The more requests filed on this issue, the better.Prisca

© 2022 - 2024 — McMap. All rights reserved.