7

已经尝试解决这个问题 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。也许这只是因为我使用的是声音,而不是视频。

所以我的问题是,我如何将按钮更改为......让我们说一个橙色的按钮(只是为了匹配界面的其余部分),当播放播放时(就像苹果让它变成蓝色一样)。我已经尝试了一切,但它根本不起作用。请帮我。

4

2 回答 2

18

编辑:

虽然下面的代码适用于 iOS 5 和 6,但从 iOS 6.0 开始,有一种官方方法可以做到这一点,这要容易得多。只需查看 的文档MPVolumeView,特别是– setRouteButtonImage:forState:.

==== 旧答案:====

这很难实现,但我找到了适用于 iOS 5.0+ 的方法。首先,将以下行添加到您的 ViewController:

#import <AudioToolbox/AudioToolbox.h>

在您的 viewDidLoad 中,您已经做对了大部分事情,这是我的代码:

for (id current in self.volumeView.subviews){
    if([current isKindOfClass:[UIButton class]]) {
        UIButton *airPlayButton = (UIButton*)current;
        self.airPlayButton = airPlayButton;
        [self setAirPlayButtonSelected:[self isAirPlayActive]];
        [airPlayButton addObserver:self forKeyPath:@"alpha" options:NSKeyValueObservingOptionNew context:nil];
    }
}

这是辅助setAirPlayButtonSelected方法,它只是设置图像:

- (void)setAirPlayButtonSelected:(BOOL)selected {
    UIImage* image;
    if (selected) {
        image = [UIImage imageNamed:@"button-airplay-selected"];
    }else {
        image = [UIImage imageNamed:@"button-airplay"];
    }
    [self.airPlayButton setImage:image forState:UIControlStateNormal];
    [self.airPlayButton setImage:image forState:UIControlStateHighlighted];
    [self.airPlayButton setImage:image forState:UIControlStateSelected];
}

为了完整起见,observeValueForKeyPath

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    if (object == self.airPlayButton && [[change valueForKey:NSKeyValueChangeNewKey] intValue] == 1) {
        [self setAirPlayButtonSelected:[self isAirPlayActive]];
    }
}

现在是有趣的部分。这是isAirPlayActive辅助方法。它使用 AudioSession 框架来确定当前正在播放的音频源。

- (BOOL)isAirPlayActive{
    CFDictionaryRef currentRouteDescriptionDictionary = nil;
    UInt32 dataSize = sizeof(currentRouteDescriptionDictionary);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &dataSize, &currentRouteDescriptionDictionary);
    if (currentRouteDescriptionDictionary) {
        CFArrayRef outputs = CFDictionaryGetValue(currentRouteDescriptionDictionary, kAudioSession_AudioRouteKey_Outputs);
        if(CFArrayGetCount(outputs) > 0) {
            CFDictionaryRef currentOutput = CFArrayGetValueAtIndex(outputs, 0);
            CFStringRef outputType = CFDictionaryGetValue(currentOutput, kAudioSession_AudioRouteKey_Type);
            return (CFStringCompare(outputType, kAudioSessionOutputRoute_AirPlay, 0) == kCFCompareEqualTo);
        }
    }

    return NO;
}

因此,所有这些代码都会在应用启动时正确更改 AirPlay 按钮。更新呢?我们需要监听 AudioSource 的变化。将以下行添加到您的viewDidLoad

    AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, audioRouteChangeCallback, (__bridge void*)self);

不要忘记取消注册dealloc

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

    AudioSessionRemovePropertyListenerWithUserData(kAudioSessionProperty_AudioRouteChange, audioRouteChangeCallback, (__bridge void*)self);

}

并在您的 ViewController 上方添加此 C 函数@implementation

void audioRouteChangeCallback (void                   *inUserData,
                                       AudioSessionPropertyID inPropertyID,
                                       UInt32                 inPropertyValueSize,
                                       const void             *inPropertyValue) {

    if (inPropertyID != kAudioSessionProperty_AudioRouteChange) {
        return;
    }

    CFDictionaryRef routeChangeDictionary = inPropertyValue;

    CFDictionaryRef currentRouteDescriptionDictionary = CFDictionaryGetValue(routeChangeDictionary, kAudioSession_AudioRouteChangeKey_CurrentRouteDescription);
    CFArrayRef outputs = CFDictionaryGetValue(currentRouteDescriptionDictionary, kAudioSession_AudioRouteKey_Outputs);
    if(CFArrayGetCount(outputs) > 0) {
        CFDictionaryRef currentOutput = CFArrayGetValueAtIndex(outputs, 0);
        CFStringRef outputType = CFDictionaryGetValue(currentOutput, kAudioSession_AudioRouteKey_Type);

        [(__bridge SettingsViewController*)inUserData setAirPlayButtonSelected:CFStringCompare(outputType, kAudioSessionOutputRoute_AirPlay, 0) == kCFCompareEqualTo];
    }

}

如您所见,它所做的只是确定 AirPlay 输出源是否处于活动状态并setAirPlayButtonSelected相应地调用该方法。

请参阅 Apple 的音频会话编程指南,特别是本节以获取有关回调如何准确工作等的详细信息。

于 2012-09-10T23:14:47.060 回答
0

在此处查看此类似链接。

我不确定这是否可行,但尝试制作一个带有播放符号的黑色按钮,然后将其放在难以看到的播放按钮顶部。您应该将用户交互设置为禁用。试试看,这可能是一个更简单的解决方案。

*不要忘记在属性检查器和身份检查器上设置禁用用户交互。

于 2012-09-11T22:38:42.400 回答