2014-11-05 51 views
5

任何想法爲什麼我可以讓我的AVPlayerViewController類播放內容,但無法讓它顯示播放控件?玩家按預期加載內容,但就是這樣。iOS AVPlayerViewController不顯示播放控件

在我MediaPlayerViewController.m類,我有:

#pragma mark - Player 
- (void)setupPlayer{ 

    // Sample URLs to use either a local file or streaming URL 
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"Besan" withExtension:@"mp4"]; 
    //url = [NSURL URLWithString:@"http://www.youtube.com/watch?v=qcI2_v7Vj2U"]; 

    // Create an instance of the AVPlayer object 
    self.player = [AVPlayer playerWithURL:url]; 


    // Keep an eye on the status of our player 
    [self.player addObserver:self forKeyPath:@"status" options:0 context:nil]; 
} 

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

    if (object == self.player && [keyPath isEqualToString:@"status"]) { 
     if (self.player.status == AVPlayerStatusFailed){ 
      NSLog(@"AVPlayer setup failed"); 
     } else if (self.player.status == AVPlayerStatusReadyToPlay) { 
      NSLog(@"AVPlayer is ready to play"); 
      [self.player play]; 
     } 
    } 
} 
+0

我有這個問題也是如此。當您刪除observeValueForKeyPath方法時會顯示控件。現在尋找某種解決方法... – 2014-12-09 17:56:58

回答

4

正如我在這個問題的評論中指出,該控件,當你在AVPlayerViewController實施- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context消失。它可能是一個空的實現;玩家控制將不會出現。這必須是一個框架錯誤,我感到震驚(震驚!),蘋果沒有抓住這個。

我的解決方案是在不同的類中實現觀察者。我創建從NSObject的繼承了VideoObserver類,並進行設置,就像這樣:

@implementation VideoObserver 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
    if ([keyPath isEqualToString:@"status"]) { 
     NSLog(@"Change: %@", change); 
    } 
} 
@end 

在AVPlayerViewController,我設置了觀察者的屬性,然後用它來啓動觀察:

self.observer = [VideoObserver new]; 

NSURL * videoURL = [NSURL URLWithString:@"http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"]; 
self.videoPlayer = [AVPlayer playerWithURL:videoURL]; 

[self.videoPlayer addObserver:self.observer forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial context:nil]; 

我在Github上的posted my demo project

+0

謝謝Aaron。您的解決方法有效,感謝您的答案。 – David 2015-09-11 00:00:12

0

也許有趣,但要確保控件實際上並不隱藏在TabBar下面。還沒有找到解決辦法,但那件小事給了我一些悲傷,並花了一點時間才意識到 - 但控制措施實際上仍然存在。

11

這不是一個框架錯誤,但與鍵值觀測的工作方式有關。通過實施

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

在你的ViewController中,你重寫了AVPlayerViewController的實現。解決的辦法是增加一個背景下,註冊觀察者時:

static NSString* const AVPlayerStatusObservationContext = @"AVPlayerStatusObservationContext"; 

然後註冊您的觀察者這樣

[self.player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionPrior context:(__bridge void *)(AVPlayerStatusObservationContext)]; 

並做observeValueForKeyPath

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

    if (context == (__bridge void *)(AVPlayerStatusObservationContext)) 
    { 
     NSLog(@"Change Object %@, Value %@",object,change); 
    } else { 
     [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; 
    } 
} 
+0

真棒回答! – Nikita 2015-08-23 15:26:04

+0

該文檔聲明「不要子類AVPlayerViewController。覆蓋此類的方法不受支持,並導致未定義的行爲。」我猜這個問題是他們不希望我們這樣做的一個原因:) – fluidsonic 2015-12-18 00:13:53

+0

這應該是被接受的答案。 – 2016-03-13 19:21:16

相關問題