使用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];
错误/问题没有,但没有出现,有任何想法吗? 谢谢!
答案 0 :(得分:6)
可能是您将VolumeView放在白色背景上。 Airplay路线按钮在使用之前是白色的(即:当它没有通过AirPlay路由时),所以如果你把控件放在白色背景上,你就不会看到它,但它会响应水龙头。将背景更改为红色,如上所示,并显示。
答案 1 :(得分: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;
}
在设备上进行测试时有效:
答案 2 :(得分:1)
当有多条路线可用时,会出现Airplay路线按钮。
Trick我发现永久显示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)
}