2013-05-01 68 views
4

使用MPVolumeView我想爲應用程序的音頻創建一個AirPlay輸出按鈕。MPVolumeView AirPlay按鈕沒有顯示

MPVolumeView *volumeView = [ [MPVolumeView alloc] initWithFrame:CGRectMake(20, 20, 220, 20)]; 
    [volumeView setShowsVolumeSlider:NO]; 
    [volumeView setShowsRouteButton:YES]; 
    [volumeView sizeToFit]; 
    [self.view addSubview:volumeView]; 
    [volumeView release]; 

錯誤/問題沒有,但它不顯示,有什麼想法?
謝謝!

+0

我剛剛使用你的代碼,它工作正常。 airplay按鈕顯示出來,並且動作起作用......您是將此添加到tableView還是其他內容? – 2013-05-01 18:34:01

回答

3

代替init,發送它initWithFrame:(CGRect)消息。好像認爲是存在的,它只是有(0,0,0,0)

這裏框架的代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    ViewController *vc = [[ViewController alloc] init]; 
    [vc.view setBackgroundColor:[UIColor redColor]]; 
    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(20, 20, 200, 50)]; 
    [volumeView setShowsVolumeSlider:YES]; 
    [volumeView setShowsRouteButton:YES]; 
    [volumeView sizeToFit]; 
    [vc.view addSubview:volumeView]; 
    UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 50)]; 
    testLabel.text = @"TESTING"; 
    [vc.view addSubview:testLabel]; 
    [self.window setRootViewController:vc]; 
    [self.window makeKeyAndVisible]; 
    [vc viewDidLoad]; 
    return YES; 
} 

對設備進行測試時,它的工作原理:

enter image description here

+0

謝謝,更新到initWithFrame:CGRectMake(20,20,220,20)但仍然:/ – 2013-05-01 18:11:37

+0

我假設self.view是對ViewController的主視圖的引用?而且,因爲編譯時沒有問題,你已經在頭文件中添加了相應的import語句? – 2013-05-01 18:17:40

+0

是的,self.view與所有其他元素一起工作,沒有問題,導入MediaPlayer/MediaPlayer.h。有任何想法嗎? – 2013-05-01 18:21:44

6

這可能是您將您的VolumeView放在白色背景下。 Airplay路由按鈕在使用之前是白色的(即:當它不通過AirPlay進行路由時),所以如果將控件放在白色背景下,則不會看到它,但會響應輕擊。如上所示,將背景改爲紅色,並顯示。

0

當有多條路線可用時,會顯示Airplay路線按鈕。

竅門我發現永久顯示Airplay按鈕是隱藏MPVolumeView路由按鈕,刪除用戶MPVolumeView用戶交互和目標與UIButton包裝的路由按鈕操作。

var airplayRouteButton: UIButton? 

private func airPlayButton() -> UIButton { 

    let wrapperView = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44)) 
    wrapperView.setImage(YOUR_AIRPLAY_IMAGE, for: UIControlState.normal) 
    wrapperView.backgroundColor = .clear 
    wrapperView.addTarget(self, action: #selector(PlayerView.replaceRouteButton), for: UIControlEvents.touchUpInside) 

    let volumeView = MPVolumeView(frame: wrapperView.bounds) 
    volumeView.showsVolumeSlider = false 
    volumeView.showsRouteButton = false 
    volumeView.isUserInteractionEnabled = false 

    self.airplayRouteButton = volumeView.subviews.filter { $0 is UIButton }.first as? UIButton 

    wrapperView.addSubview(volumeView) 

    return wrapperView 
} 

@objc private func replaceRouteButton() { 
    airplayRouteButton?.sendActions(for: .touchUpInside) 
}