2

I'd like to display the Airplay icon in an iPhone app as an UIBarButtonItem only when airplay devices (other than the user's iPhone) are available.

var volumeView = MPVolumeView!

I've set up the UIBarButtonItem in the viewDidLoad() like that:

volumeView = MPVolumeView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: 0.0))
volumeView.showsVolumeSlider = false
let airplayBarButtonItem = UIBarButtonItem(customView: volumeView)
if let routeButton = volumeView.subviews.last as? UIButton,
  let routeButtonTemplateImage = routeButton.currentImage?.withRenderingMode(.alwaysTemplate) {
  volumeView.setRouteButtonImage(routeButtonTemplateImage, for: .normal)
  volumeView.tintColor = myColor
}

volumeView.showsRouteButton = volumeView.areWirelessRoutesAvailable

In the viewWillAppear():

volumeView.showsRouteButton = volumeView.areWirelessRoutesAvailable

Now the app needs to update the current screen when Airplay devices become available, is there a way to do that?

EDIT: This notification seems to be deprecated, is there any replacement? https://developer.apple.com/documentation/mediaplayer/mpvolumeviewwirelessrouteactivedidchangenotification?language=objc#discussion

nontomatic
  • 2,003
  • 2
  • 24
  • 38
  • I've now come across MPVolumeViewWirelessRoutesAvailableDidChangeNotification, I have no way to test it right now, can someone confirm this will be sent when Airplay devices are recognised and lost? – nontomatic Jul 07 '19 at 18:02
  • The audio session notifies you any time routing changes. – matt Jul 07 '19 at 18:19
  • Thank you Matt, are you referring to a notification distributed by AVAudioSession? I thought I could just add an observer and update .showsRouteButton as done in the ViewWillAppear(), but what need I to observe? – nontomatic Jul 07 '19 at 18:28

0 Answers0