gpt4 book ai didi

objective-c - airplay 处于事件状态时如何自定义 Airplay 按钮

转载 作者:太空狗 更新时间:2023-10-30 03:35:11 27 4
gpt4 key购买 nike

我已经尝试解决这个问题 2 天了,但我放弃了。我正在尝试实现一个自定义的 airplay 按钮(我必须这样做,因为背景是白色的,按钮必须是黑色的)。我在 interfacebuilder 中添加了一个 View 并为其选择了 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。也许这只是因为我使用的是声音,而不是视频。

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

最佳答案

编辑:

虽然下面的代码适用于 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 框架来确定当前正在播放的 audioSource。

- (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 的 @implementation 之上添加此 C 函数:

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 的 Audio Session Programming Guide , 具体来说 this section有关回调的确切工作方式等的详细信息。

关于objective-c - airplay 处于事件状态时如何自定义 Airplay 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12318377/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com