I have started using MPVolumeView
in my app, however the Air Play button doesn't appear at all. Even though I have more than one Air Play device available to send my AVPlayer
audio to on the network. These can be seen if checking Air Play from the Control Center for example.
Here is the code I have:
myVolumeView = [MPVolumeView new];
[myVolumeView setShowsVolumeSlider:YES];
[myVolumeView setShowsRouteButton:YES];
myVolumeView.frame = _volumePanel.bounds;
[myVolumeView sizeToFit];
_myVolumeView.autoresizingMask = UIAutoresizingFlexibleWidth;
[_volumePanel addSubview:myVolumeView];
Pretty simple stuff. I have an AVPlayer
that runs some music and that's it. Interestingly, if I select another Air Play device from the Control Center it forces the Air Play button to appear in app on my MPVolumeView
, but it is kind of glitchy.
If in Xcode I click the Debug Hierarchy Mode button above the console I can see the Air Play button in my UI, however it's Alpha is 0.
it's a complete and utter hack but it works
MPVolumeView* airplayButton = [[MPVolumeView alloc] init];
for (UIButton *button in airplayButton.subviews)
{
if ([button isKindOfClass:[UIButton class]])
{
[button addObserver:self forKeyPath:@"alpha" options:NSKeyValueObservingOptionNew context:nil];
}
}
Then in the observer
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([object isKindOfClass:[UIButton class]] && [[change valueForKey:NSKeyValueChangeNewKey] intValue] != 1)
{
UIButton *airplay = (UIButton *)object;
[airplay setAlpha:1.0];
}
}
don't forget to turn the observer off in the dealloc
- (void)dealloc
{
for (UIButton *button in _airplayButton.subviews)
{
if ([button isKindOfClass:[UIButton class]])
{
[button removeObserver:self forKeyPath:@"alpha"];
break;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With