4

使用MPVolumeView我想为应用程序的音频创建一个 AirPlay 输出按钮。

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

没有错误/问题,但没有出现,有什么想法吗?
谢谢!

4

3 回答 3

6

可能是您将 VolumeView 放在白色背景上。Airplay 路由按钮在使用之前是白色的(即:当它不通过 AirPlay 路由时),因此如果您将控件放在白色背景上,您将看不到它,但它会响应点击。将背景更改为红色,如上所示,它会显示出来。

于 2014-03-06T21:31:40.687 回答
3

而不是 init,向它发送 initWithFrame:(CGRect) 消息。似乎视图就在那里,它只有一个 (0,0,0,0) 的框架

这是代码:

- (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;
}

它在设备上测试时有效:

在此处输入图像描述

于 2013-05-01T18:00:03.047 回答
2

当有多个路线可用时,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:05:59.190 回答