MPVolumeView AirPlay button not showing

Using MPVolumeView , I wanted to create an AirPlay exit button for an audio application.

 MPVolumeView *volumeView = [ [MPVolumeView alloc] initWithFrame:CGRectMake(20, 20, 220, 20)]; [volumeView setShowsVolumeSlider:NO]; [volumeView setShowsRouteButton:YES]; [volumeView sizeToFit]; [self.view addSubview:volumeView]; [volumeView release]; 

There are no errors / problems, but this is not displayed, any ideas?
Thank you

+4
source share
3 answers

instead of init, send the message initWithFrame: (CGRect). It seems that there is a view, it just has a frame (0,0,0,0)

Here is the code:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; ViewController *vc = [[ViewController alloc] init]; [vc.view setBackgroundColor:[UIColor redColor]]; MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(20, 20, 200, 50)]; [volumeView setShowsVolumeSlider:YES]; [volumeView setShowsRouteButton:YES]; [volumeView sizeToFit]; [vc.view addSubview:volumeView]; UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 50)]; testLabel.text = @"TESTING"; [vc.view addSubview:testLabel]; [self.window setRootViewController:vc]; [self.window makeKeyAndVisible]; [vc viewDidLoad]; return YES; } 

It works when testing on a device:

enter image description here

+3
source

Perhaps you are placing your VolumeView on a white background. The Airplay route button is white before it is used (for example, when it will not be routed through AirPlay), so if you set the control to a white background, you will not see it, but it will respond to taps. Change the background to something like red as shown above and it shows.

+6
source

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) } 
0
source

All Articles