使用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];Errors / issues none,但它没有显示,有什么想法吗?
谢谢!
发布于 2013-05-02 02:00:03
不是初始化,而是发送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;
}当在设备上测试时,它可以工作:

发布于 2014-03-07 05:31:41
可能是你把你的VolumeView放在白色的背景上。Airplay路由按钮在使用之前是白色的(即:当它不是通过AirPlay路由时),所以如果您将控件放在白色背景上,您将看不到它,但它会对轻击做出反应。将背景更改为类似于上面的红色,它就会显示出来。
发布于 2017-10-17 18:05:59
当有多条可用路线时,会出现空中播放路线按钮。
我发现永久显示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)
}https://stackoverflow.com/questions/16323019
复制相似问题