2

iOS 9.1 - iPhone 6S

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

在创建 MPVolumeView 以检查无线路由后,我尝试查询它并得到 0。通过禁用和启用 WiFi 以触发通知,我只能得到 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];
}
4

1 回答 1

5

正如@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)
}
于 2017-10-17T10:08:56.480 回答