MPVolumeView AirPlay按钮没有显示
使用MPVolumeView
我想为应用程序的音频创建一个AirPlay输出按钮。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;
}
对设备进行测试时,它的工作原理:
谢谢,更新到initWithFrame:CGRectMake(20,20,220,20)但仍然:/ – 2013-05-01 18:11:37
我假设self.view是对ViewController的主视图的引用?而且,因为编译时没有问题,你已经在头文件中添加了相应的import语句? – 2013-05-01 18:17:40
是的,self.view与所有其他元素一起工作,没有问题,导入MediaPlayer/MediaPlayer.h。有任何想法吗? – 2013-05-01 18:21:44
这可能是您将您的VolumeView放在白色背景下。 Airplay路由按钮在使用之前是白色的(即:当它不通过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)
}
我刚刚使用你的代码,它工作正常。 airplay按钮显示出来,并且动作起作用......您是将此添加到tableView还是其他内容? – 2013-05-01 18:34:01