首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MPVolumeView AirPlay按钮未显示

MPVolumeView AirPlay按钮未显示
EN

Stack Overflow用户
提问于 2013-05-02 01:18:08
回答 3查看 5.6K关注 0票数 4

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

代码语言:javascript
复制
MPVolumeView *volumeView = [ [MPVolumeView alloc] initWithFrame:CGRectMake(20, 20, 220, 20)];
    [volumeView setShowsVolumeSlider:NO];
    [volumeView setShowsRouteButton:YES];
    [volumeView sizeToFit];
    [self.view addSubview:volumeView];
    [volumeView release];

Errors / issues none,但它没有显示,有什么想法吗?

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-05-02 02:00:03

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

代码如下:

代码语言:javascript
复制
- (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;
}

当在设备上测试时,它可以工作:

票数 3
EN

Stack Overflow用户

发布于 2014-03-07 05:31:41

可能是你把你的VolumeView放在白色的背景上。Airplay路由按钮在使用之前是白色的(即:当它不是通过AirPlay路由时),所以如果您将控件放在白色背景上,您将看不到它,但它会对轻击做出反应。将背景更改为类似于上面的红色,它就会显示出来。

票数 6
EN

Stack Overflow用户

发布于 2017-10-17 18:05:59

当有多条可用路线时,会出现空中播放路线按钮。

我发现永久显示Airplay按钮的技巧是隐藏MPVolumeView路由按钮,删除用户MPVolumeView用户交互,并使用UIButton包装器定位路由按钮操作。

代码语言:javascript
复制
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)
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16323019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档