chi*_*uba 7 cocoa-touch objective-c airplay
一直试图解决这个问题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];
}
}
Run Code Online (Sandbox Code Playgroud)
"玩家"是基于AVPLayer的singelton.但是,在检查airPlay是否处于活动状态时,它始终返回false.也许它只是因为使用声音,而不是视频.
所以我的问题是,我怎么能将按钮更改为...当airplay正在流式播放时(就像苹果将其变为蓝色一样),让我们说一个橙色的按钮(只是为了匹配界面的其余部分).我已经尝试过所有的东西而且根本就没有用.请帮我.
avf*_*avf 18
编辑:
虽然下面的代码适用于iOS 5和6,但从iOS 6.0开始,有一种官方的方法可以做到这一点,这要容易得多.请MPVolumeView
具体看一下文档– setRouteButtonImage:forState:
.
====旧答案:====
这很难实现,但我找到了iOS 5.0+的方法.首先,将以下行添加到ViewController:
#import <AudioToolbox/AudioToolbox.h>
Run Code Online (Sandbox Code Playgroud)
在你的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];
}
}
Run Code Online (Sandbox Code Playgroud)
这是辅助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];
}
Run Code Online (Sandbox Code Playgroud)
为了完成起见,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]];
}
}
Run Code Online (Sandbox Code Playgroud)
现在是有趣的部分.这是isAirPlayActive
辅助方法.它使用AudioSession框架来确定当前正在播放的audioSource.
- (BOOL)isAirPlayActive{
CFDictionaryRef currentRouteDescriptionDictionary = nil;
UInt32 dataSize = sizeof(currentRouteDescriptionDictionary);
AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &dataSize, ¤tRouteDescriptionDictionary);
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;
}
Run Code Online (Sandbox Code Playgroud)
因此,所有这些代码都会在应用启动时正确更改AirPlay按钮.更新怎么样?我们需要监听AudioSource的变化.将以下行添加到您的viewDidLoad
:
AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, audioRouteChangeCallback, (__bridge void*)self);
Run Code Online (Sandbox Code Playgroud)
不要忘记取消注册dealloc
:
- (void)dealloc {
[self.airPlayButton removeObserver:self forKeyPath:@"alpha"];
AudioSessionRemovePropertyListenerWithUserData(kAudioSessionProperty_AudioRouteChange, audioRouteChangeCallback, (__bridge void*)self);
}
Run Code Online (Sandbox Code Playgroud)
并在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];
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,它所做的只是确定AirPlay输出源是否处于活动状态并相应地调用该setAirPlayButtonSelected
方法.
请参阅Apple的音频会话编程指南,特别是本节,了解有关回调如何正常工作的详细信息等.