MPVolumeView在启动时不显示路线按钮
iOS 9.1 – iPhone 6S
即使有可用的无线路由,当应用程序启动时,MPVolumeView的路由按钮(airplay)也不会显示。
我已经尝试在创建MPVolumeView之后查询我的MPVolumeView以检查无线路由并且我得到0.我只能通过禁用并启用WiFi来触发通知来获得1(并显示路由按钮)。
我的应用程序中的MPVolumeView用于控制从UIWebView播放的video量。 此外,每当我激活无线路由进行流式传输时,MPVolumeView滑块都会消失 – 有没有办法在使用UIWebView播放媒体时阻止此行为?
下面是我创建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]; }
正如@kvr所说,首先在硬件设备上进行测试
当有多条路线可用时,会出现Airplay路线按钮。
技巧我发现永久显示Airplay按钮是隐藏MPVolumeView路线按钮,删除用户MPVolumeView用户交互并使用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) }