已经尝试解决这个问题 2 天了,我放弃了。我试图实现一个自定义的播放按钮(我必须因为背景是白色的,按钮必须是黑色的)。我在 interfacebuilder 中添加了一个视图,并为它选择了 mpVolumeView。然后我建立了连接并编写了以下代码;
viewDidLoad.. {
.....
[_volumeView setShowsVolumeSlider:NO];
for (UIButton *button in _volumeView.subviews) {
if ([button isKindOfClass:[UIButton class]]) {
[button setImage:[UIImage imageNamed:@"airplay_icon.png"] forState:UIControlStateNormal];
[button addObserver:self forKeyPath:@"alpha" options:NSKeyValueObservingOptionNew context:nil];
[button addTarget:self action:@selector(switchAirplayButton) forControlEvents:UIControlEventTouchUpInside];
[button sizeToFit];
}
}
[_volumeView sizeToFit];
}
-(void)switchAirplayButton {
for (UIButton *button in _volumeView.subviews) {
if ([button isKindOfClass:[UIButton class]]) {
NSLog(@"%d", _controlBar.player.airPlayVideoActive);
if(_controlBar.player.airPlayVideoActive) {
[button setImage:[UIImage imageNamed:@"airplay_icon_pressed.png"] forState:UIControlStateNormal];
} else [button setImage:[UIImage imageNamed:@"airplay_icon.png"] forState:UIControlStateNormal];
[button sizeToFit];
}
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([object isKindOfClass:[UIButton class]] && [[change valueForKey:NSKeyValueChangeNewKey] intValue] == 1) {
[(UIButton *)object setImage:[UIImage imageNamed:@"airplay_icon.png"] forState:UIControlStateNormal];
[(UIButton *)object sizeToFit];
}
}
“播放器”是基于 AVPLayer 的单片机。但是,在检查 airPlay 是否处于活动状态时,它总是返回 false。也许这只是因为我使用的是声音,而不是视频。
所以我的问题是,我如何将按钮更改为......让我们说一个橙色的按钮(只是为了匹配界面的其余部分),当播放播放时(就像苹果让它变成蓝色一样)。我已经尝试了一切,但它根本不起作用。请帮我。