in Tricks

AVPlayer AirPlay notification system

In an earlier post, I spoke about enabling AirPlay support to an AVPlayer object.

Here’s a complement to that post, it enables you to monitor the state of AirPlay (starting iOS7) and display a stand-in image where the AVPlayer object is placed. (AVPlayer will show a black screen when AirPlay is active).

// Put this in your init somewhere
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
 
[center addObserver:self selector:@selector(wirelessRouteActive:)
name:MPVolumeViewWirelessRouteActiveDidChangeNotification object:nil];
 
[center addObserver:self selector:@selector(wirelessAvailable:)
name:MPVolumeViewWirelessRoutesAvailableDidChangeNotification object:nil];
 
// Selectors for both notification events
- (void)wirelessRouteActive:(NSNotification*)aNotification
{
    MPVolumeView* volumeView = (MPVolumeView*)aNotification.object;
 
    NSLog(@"%s: %@",__FUNCTION__,aNotification);
    if(volumeView.isWirelessRouteActive) {
        // add airplay indication view
        NSLog(@"airplaying");
    } else {
        // remove airplay indication view
        NSLog(@"not airplaying");
    }
}
 
-(void)wirelessAvailable:(NSNotification*)aNotification
{
    NSLog(@"%s: %@",__FUNCTION__,aNotification);
}