Ios MPVolumeView AirPlay按钮未显示

Ios MPVolumeView AirPlay按钮未显示,ios,objective-c,airplay,mpvolumeview,Ios,Objective C,Airplay,Mpvolumeview,使用MPVolumeView我想为应用程序的音频创建一个AirPlay输出按钮 MPVolumeView *volumeView = [ [MPVolumeView alloc] initWithFrame:CGRectMake(20, 20, 220, 20)]; [volumeView setShowsVolumeSlider:NO]; [volumeView setShowsRouteButton:YES]; [volumeView sizeToFit];

使用
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];
错误/问题无但未显示,有什么想法吗?

谢谢

不发送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;
}
在设备上进行测试时,它会工作:


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

当有多条航路可用时,将显示Airplay航路按钮

我发现永久显示Airplay按钮的诀窍是隐藏MPVolumeView路由按钮,删除用户MPVolumeView用户交互,并使用UIButton包装器以路由按钮操作为目标

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)
}

谢谢,更新为initWithFrame:CGRectMake(20,20,220,20),但仍然:/I假设self.view是对ViewController主视图的引用?既然编译的所有内容都没有问题,那么您已经在头文件中添加了相应的导入语句了吗?是的,self.view与所有其他元素一起工作,对了,没有问题,MediaPlayer/MediaPlayer.h已导入。有什么想法吗?检查答案:你们不能使用来自模拟器的MPVolumeView。我刚刚使用了你们的代码,它工作得很好。airplay按钮出现,动作开始了……您是在将其添加到tableView还是什么?