Mostrar video en la pantalla externa con AirPlay y AVPlayer

Estoy usando Apple Airplay y AVPlayer para mostrar un video en una pantalla externa, mientras que muestra algunos controles en el dispositivo usando Apple TV y AirPlay.

Estoy usando esta class para mostrar AVPlayer en la pantalla externa:

 #import "AirPlayViewController.h" @interface AirPlayViewController () @end @implementation AirPlayViewController - (id)initWithFrame:(CGRect)iFrame{ self.view.frame = iFrame; self.backGroundImageView = [[UIImageView alloc]initWithFrame:iFrame]; [self.view addSubview: self.backGroundImageView]; return self; } - (void)viewDidLoad{ [super viewDidLoad]; } -(void)viewWillDisappear:(BOOL)animated{ [audioVideoPlayer pause]; } -(void)playMediaWithURL:(NSURL*)aURL{ audioVideoPlayer = [AVPlayer playerWithURL:aURL]; audioVideoPlayer.usesExternalPlaybackWhileExternalScreenIsActive=YES; audioVideoPlayer.allowsExternalPlayback= YES; //manually resizing and adding the layer, so that the app does not get confused with airplayfunctionality AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer: audioVideoPlayer]; playerLayer.frame = self.view.bounds; [self.view.layer addSublayer:playerLayer]; if (audioVideoPlayer) { [audioVideoPlayer play]; } } -(void)stopMediaPlayback{ [audioVideoPlayer pause]; [[AVPlayerLayer playerLayerWithPlayer: audioVideoPlayer] removeFromSuperlayer]; audioVideoPlayer = nil; } @end 

El problema que tengo está ahí mismo en playMediaWithURL: La capa de AVPlayer no se agrega automáticamente a la pantalla externa, pero tengo que agregarla a mano, que se siente sucia y conduce a todo tipo de trabajo de limpieza cuando quiero quitar AVPlayer el AVPlayer de la vista.

¿Alguien más ha tenido problemas con esto y ha encontrado alguna forma clara de implementarlo?

Se llama a UIViewController desde otro UIViewController que contiene la segunda window junto con todo el material delegado de AirPlay para mostrarlo, usando esta function:

 - (void)setupMirroringForScreen:(UIScreen *)anExternalScreen{ //find the correct resolution for the external screen UIScreen *tScreen= anExternalScreen; CGSize max = {0, 0}; UIScreenMode *maxScreenMode = nil; for (UIScreenMode *current in tScreen.availableModes) { if (maxScreenMode == nil || current.size.height > max.height || current.size.width > max.width) { max = current.size; maxScreenMode = current; } } tScreen.currentMode = maxScreenMode; // Setup window in external screen self.externalWindow = [[UIWindow alloc] initWithFrame:tScreen.bounds]; self.externalWindow.hidden = NO; self.externalWindow.layer.contentsGravity = kCAGravityResizeAspect; self.externalWindow.screen = tScreen; self.airPlayVC = [[AirPlayViewController alloc]initWithFrame:tScreen.bounds]; [self.airPlayVC.backGroundImageView setImage:[UIImage imageNamed:mainMenuExternalBackgroundImage]]; //try to play a video on the external Screen/ Audio file as fallback NSURL *tURL= [[NSBundle mainBundle]URLForResource:mainMenuVideo withExtension:nil]; if (!tURL) { tURL = [[NSBundle mainBundle] URLForResource:mainMenuAmbienceMusic withExtension:nil]; } [self.airPlayVC playMediaWithURL:tURL]; [self.externalWindow addSubview:self.airPlayVC.view]; }