2012-09-07 6 views
7

2 일 동안이 문제를 해결하려고 시도했지만 포기합니다. 임명 된 airplay 단추를 실행하는 것을 시도해 Im (나는 배경을 백색 beacuse해야하고 단추는 검정이어야한다). 필자는 interfacebuilder에서 뷰를 추가하고 mpVolumeView를 선택했다. 그런 다음 연결을 만들고 다음 코드를 작성했습니다.airplay가 활성화되어있을 때 Airplay 버튼을 사용자 정의하는 방법

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]; 
    } 
} 

"player"는 AVPLayer 기반의 싱글 톤입니다. 그러나 airPlay가 활성 상태인지 확인하는 경우 항상 false를 반환합니다. 어쩌면 단지 비디오를 사용하는 것이 아니라 사운드를 사용하는 것일 수도 있습니다.

내 질문은 어떻게하면 버튼을 변경할 수 있습니까 ... 오렌지가 하나의 인터페이스 (나머지 인터페이스와 일치하도록)를 말할 수 있습니다. 스트리밍 할 때 (사과가 파란색으로 바뀌는 것처럼). 나는 모든 것을 시도했지만 전혀 작동하지 않습니다. 도와주세요.

+0

chikuba,이 문제를 어떻게 처리할까요? 내 VoIP 전화 iOS 앱에서 동일한 문제가 발생합니다. 제안을 할 수 있습니까? 감사! –

답변

18

편집 : 아래 코드는 아이폰 OS 6.0부터 시작하여 모두에서 iOS 5, 6, 작동

있지만 훨씬 쉽게이 작업을 수행 할 수있는 공식적인 방법이있다. MPVolumeView, 특히 – setRouteButtonImage:forState:의 설명서를 살펴보십시오.

==== 올드 답 : ====

이 달성하기 매우 어렵습니다,하지만 난 iOS 용 방법을 5.0 발견했다. 우선, 당신의 ViewController에 다음 행을 추가 : 당신의 viewDidLoad에서

#import <AudioToolbox/AudioToolbox.h> 

, 당신은 이미 바로 물건의 대부분을하고있는이 내 코드입니다 :

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 Button을 바꿔줍니다. 업데이트는 어떨까요? 우리는 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 메서드 일치를 호출하는 것입니다. ingly.

콜백이 어떻게 작동하는지에 대한 자세한 내용은 Apple Audio Session Programming Guide, 특히 this section을 참조하십시오.

+0

당신은 당신 자신을 진정한 rep 부스트 : 나는 어제 거의 같은 해결책을 찾았지만, 당신은 (예를 들어 청취자를 제거하고) 내 머릿속에 있어야만하는 정말 좋은 것들을 지적했다. 정말 고마워요! – chikuba

+0

알파 관찰자가 내 솔루션에 필요한 것입니다. 좋은 물건! iOS 8에서 D –

+0

을 변경해야합니다. if (CFArrayGetCount (outputs)> 0)을 if (출력 && CFArrayGetCount (outputs)> 0) –

0

체크 아웃이 작동하지만, 에어 플레이 기호 검은 색 버튼을 시도 할 것인지 잘 모르겠어요이 유사한 link here.

을 한 후 볼 어렵다 에어 플레이 버튼을 상단에 넣어. 사용자 상호 작용을 사용 안 함으로 설정해야합니다. 시도해보십시오. 더 쉬운 해결책 일 수 있습니다.

* 특성 관리자 및 ID 관리자에서 사용자 상호 작용을 사용하지 않도록 설정하는 것을 잊지 마십시오.

관련 문제