Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPVolumeView AirPlay Button Not Visible

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.

like image 518
Josh Kahane Avatar asked Nov 07 '15 17:11

Josh Kahane


1 Answers

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;
        }
    }
}
like image 61
Chris Horsfield Avatar answered Nov 03 '22 21:11

Chris Horsfield