MPVolumeView在启动时不显示路线按钮

时间:2016-03-10 19:46:55

标签: ios xcode airplay mpvolumeview

iOS 9.1 - iPhone 6S

即使有无线路线可用,也会在应用启动时显示MPVolumeView的路线按钮(airplay)。

我已经尝试在创建MPVolumeView后查询我的MPVolumeView以检查无线路由,然后我得到0.我只能通过禁用和启用WiFi来触发a来获得1(并显示路由按钮)通知。

我的应用程序中的MPVolumeView用于控制从UIWebView播放的视频量。此外,每当我激活无线路由进行流式传输时,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];
}

1 个答案:

答案 0 :(得分:5)

正如@kvr所说,首先在硬件设备上进行测试

当有多条路线可用时,会出现Airplay路线按钮。

Trick我发现永久显示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)
}