Search code examples
iosswiftavaudioplayerairplay

Adding AirPlay To AVAudioPlayer (Swift)


I'm making a music app with an AVAudioPlayer. I would like the user to be able to open the AirPlay picker view...

AirPlay Picker View

...by tapping a UIBarButtonItem in a UIToolBar to stream the music playing from my app. How would I do this?


Solution

  • Start with an MPVolumeView without the volume slider:

    MPVolumeView* myVolumeView = [[MPVolumeView alloc] initWithFrame: CGRectMake(...)];
    myVolumeView. showsVolumeSlider = NO;
    

    See Add MPVolumeView to bottom toolbar for how to add an MPVolumeView to a UIBarButtonItem, i.e.:

    UIBarButtonItem* b = [[UIBarButtonItem alloc] initWithCustomView: myVolumeView];
    

    Swift 4

    var myVolumeView = MPVolumeView(frame: CGRect(x: x, y: y, width: width, height: height))
    myVolumeView.showsVolumeSlider = false
    var airplayButton = UIBarButtonItem(customView: myVolumeView)