Search code examples
iosairplay

Is there any public api to detect AirPlay available or not


I have implement the MPVolumeView to show Airplay option but I don't know how to hide MPVolumeView if Airplay options/sources are no longer available.

Is there any public API which can detecting AirPlay option/source are available or not. So that application can hide/show the airplay option.

NOTE: I am using custom player not the default MPMoviePlayerController

Thanks!


Solution

  • I see two approaches that would work:

    1. Set MPVolumeView's showsVolumeSlider to NO and the AirPlay route button picker "...is visible only when there is an AirPlay output device available."

    Source: https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AirPlayGuide/EnrichYourAppforAirPlay/EnrichYourAppforAirPlay.html

    1. Add observer for MPVolumeViewWirelessRoutesAvailableDidChangeNotification and hide or remove your subview.

      - (void)viewWillAppear:(BOOL)animated {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleWirelessRoutesDidChange:)
                                                     name:MPVolumeViewWirelessRoutesAvailableDidChangeNotification object:nil];
      }
      
      - (void)viewWillDisappear:(BOOL)animated {
          [[NSNotificationCenter defaultCenter] removeObserver:self];
      }
      
      - (void)handleWirelessRoutesDidChange:(NSNotification *)notification {
          NSLog(@"Wireless routes did change: %@", notification);
          // Hide or remove your MPVolumeView
      }