2

我正在开发一个使用 MPMoviePlayerController 支持 AirPlay 的 iPhone 应用程序。但是,我需要在我的自定义视图中显示这个 AirPlay 按钮。因此,我采用 MPVolumeView 并将其添加到我的自定义视图中;从 MPVolumeView 中删除了除 AirPlay 按钮之外的所有子视图。

问题是:1)我可以更改音量视图的框架,使其适合我的自定义视图的角落,并具有 AirPlay 按钮的大小吗?我知道可以以编程方式处理这个问题;这样做是否有效?链接自定义 Airplay 按钮的外观提到我们不应该改变 AirPlay 按钮的形状和位置。

2) 我需要将自定义图像设置为 AirPlay 按钮,以使其在美学上与我的自定义视图相匹配。我该怎么做呢?

3) 只要不存在带有 AirPlay 的设备,AirPlay 按钮就会从 MPVolumeView 中消失。按钮消失时是否有任何通知?当 AirPlay 按钮不存在时,我需要调整我的自定义视图是否有任何方法可以识别 AirPlay 按钮是否存在?MPVolumeView 子视图数组有这个按钮,即使在 MPVolumeView 中没有显示,它也不处于隐藏状态。

谢谢和问候, 迪帕

4

2 回答 2

5

你应该看看这个答案:自定义 Airplay 按钮的外观

它与我的答案基本相同,但更详细。我认为它回答了你的大部分问题。

为了您的方便,将其粘贴在这里:


在接受@Erik B 的回答并将赏金授予他之后,我发现需要进行更多调整才能使其正常工作。我在这里发帖是为了未来的 SO 搜索者。

我看到的问题是按钮的内部机制会根据当前的播放状态分配图像。因此,如果 Airplay 接收器消失或状态以某种方式改变,我在初始化期间所做的任何自定义都不会保留。alpha为了解决这个问题,我在按钮的键上设置了一个 KVO 观察。我注意到按钮总是淡入/淡出,这是alpha.

MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectZero];
[volumeView setShowsVolumeSlider:NO];
for (UIButton *button in volumeView.subviews) {
    if ([button isKindOfClass:[UIButton class]]) {
        self.airplayButton = button; // @property retain
        [self.airplayButton setImage:[UIImage imageNamed:@"airplay.png"] forState:UIControlStateNormal];
        [self.airplayButton setBounds:CGRectMake(0, 0, kDefaultIconSize, kDefaultIconSize)];
        [self.airplayButton addObserver:self forKeyPath:@"alpha" options:NSKeyValueObservingOptionNew context:nil];
    }
}
[volumeView sizeToFit];

然后我观察按钮的变化值alpha

- (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.png"] forState:UIControlStateNormal];
        [(UIButton *)object setBounds:CGRectMake(0, 0, kDefaultIconSize, kDefaultIconSize)];
    }
}

如果您销毁按钮,请不要忘记移除观察者

- (void)dealloc {
    [self.airplayButton removeObserver:self forKeyPath:@"alpha"];
    …
}

根据代码观察,如果 Apple 更改内部视图层次结构MPVolumeView以添加/删除/更改视图以便出现不同的按钮,则该按钮将中断。这使得它有点脆弱,所以使用风险自负,或者想出一个计划 b,以防发生这种情况。我已经在生产中使用它一年多了,没有任何问题。如果您想查看它的实际效果,请查看Ambiance中的主播放器屏幕

于 2011-10-28T17:09:19.063 回答
0

通过将 MPVolumeView 添加到 UIBarButtonItem 我做得更简单

- (void) initAirPlayPicker {
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(0,0,44,44)];
volumeView.tintColor = [UIColor blueColor];
volumeView.backgroundColor = [UIColor clearColor];

UIImage *imgAirPlay = nil;

for( UIView *wnd in volumeView.subviews ) {
    if( [wnd isKindOfClass:[UIButton class] ]) {
        self.airPlayWindow = (UIButton*) wnd;
        UIImage *img = _airPlayWindow.currentImage;
        imgAirPlay = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [volumeView setRouteButtonImage: imgAirPlay forState:UIControlStateNormal];
        break;
    }
}

volumeView.showsRouteButton = YES;
volumeView.showsVolumeSlider = NO;
[volumeView sizeToFit];
self.airPlayButton = [[UIBarButtonItem alloc] initWithCustomView:volumeView];
}

通过将 MPVolumeView 添加到视图层次结构中,它会收到有关 Airplay 状态的通知并且可以正常工作。

于 2015-10-15T09:12:54.027 回答