MPVolumeView does not display the route button at startup

iOS 9.1 - iPhone 6S

The MPVolumeView (airplay) route button does not appear when the application starts, even if there are wireless routes available.

I tried to request my MPVolumeView after it was created for checking wireless routes, and I get 0. I can only get 1 (and show the route button) by disabling and allowing WiFi to trigger a notification.

MPVolumeView in my application is designed to control the volume of videos played in UIWebView. Also, whenever I activate the wireless route for streaming, the MPVolumeView slider disappears - is there a way to prevent this behavior when using UIWebView to play media?

Below is my code for creating MPVolumeView:

-(void) createAndDisplayMPVolumeView{ // Create a simple holding UIView and give it a frame volumeHolder = [[UIView alloc] initWithFrame: volumeSlider.frame]; volumeHolder.autoresizingMask = UIViewAutoresizingFlexibleHeight; volumeSlider.hidden = YES; // set the UIView backgroundColor to clear. [volumeHolder setBackgroundColor: [UIColor clearColor]]; // add the holding view as a subView of the main view [nowPlayingMainView addSubview: volumeHolder]; // Create an instance of MPVolumeView and give it a frame myVolumeView = [[CustomVolumeView alloc] initWithFrame: volumeHolder.bounds]; myVolumeView.tintColor = [UIColor darkTextColor]; myVolumeView.showsRouteButton = YES; myVolumeView.showsVolumeSlider = YES; volumeRect = myVolumeView.frame; [myVolumeView setRouteButtonImage:[UIImage imageNamed:@"airplayButton"] forState:UIControlStateNormal]; [myVolumeView setRouteButtonImage:[UIImage imageNamed:@"airplayButtonHighlighted"] forState:UIControlStateHighlighted]; [myVolumeView setRouteButtonImage:[UIImage imageNamed:@"airplayButtonSelected"] forState:UIControlStateSelected]; [volumeHolder addSubview: myVolumeView]; } 
+2
ios xcode airplay mpvolumeview
source share
1 answer

As @kvr said, the first test on a hardware device

The Flight Route button appears when more than one route is available.

The trick I discovered to constantly press the Airplay button is to hide the MPVolumeView route button, remove the user interaction of the MPVolumeView user, and configure the action of the route button using the UIButton Wrapper.

 var airplayRouteButton: UIButton? private func airPlayButton() -> UIButton { let wrapperView = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44)) wrapperView.setImage(YOUR_AIRPLAY_IMAGE, for: UIControlState.normal) wrapperView.backgroundColor = .clear wrapperView.addTarget(self, action: #selector(PlayerView.replaceRouteButton), for: UIControlEvents.touchUpInside) let volumeView = MPVolumeView(frame: wrapperView.bounds) volumeView.showsVolumeSlider = false volumeView.showsRouteButton = false volumeView.isUserInteractionEnabled = false self.airplayRouteButton = volumeView.subviews.filter { $0 is UIButton }.first as? UIButton wrapperView.addSubview(volumeView) return wrapperView } @objc private func replaceRouteButton() { airplayRouteButton?.sendActions(for: .touchUpInside) } 
+1
source share

All Articles