2011-04-03 76 views
0

我真的不熟悉Obj-C和iOS開發,我在這裏發現了非常有用的信息,但這裏有一個問題,我沒有找到答案。AVQueuePlayer流狀態?

我收到了從url播放音頻流的AVQueuePlayer的實例。

我怎麼知道音頻流被加載?例如,當我按下「播放」按鈕時,按下按鈕和實際開始播放流媒體之間會有幾秒鐘的延遲。 我看着developer.apple.com庫,並沒有找到任何方法,我可以用它來檢查AVQueuePlayer的狀態。在AVPLayer中有一個,但AVPlayer不支持http上的流,據我所知。

謝謝。

回答

1

我不確定你的意思是「加載」:你的意思是什麼時候該項目是滿載或當該項目準備播放?

AVQueuePlayer支持http流(HTTP Live和文件)的方式與AVPlayer相同。您應該查看AVFoundation Programming Guide, Handling Different Types of Asset

最常見的情況是當物品準備好玩時,我會回答那個問題。如果你正在與iOS正與AVQueuePlayer < 4.3,則需要通過觀察AVPlayerItem狀態鍵的值來檢查的AVPlayerItem狀態:

static int LoadingItemContext = 1; 

- (void)loadExampleItem 
{ 
    NSURL *remoteURL = [NSURL URLWithString:@"http://media.example.com/file.mp3"]; 
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:remoteURL]; 
    // insert the new item at the end 
    if (item) { 
     [self registerAVItemObserver:item]; 
     if ([self.player canInsertItem:item afterItem:nil]) { 
      [self.player insertItem:item afterItem:nil]; 
      // now observe item.status for when it is ready to play 
     } 
    } 
} 

- (void)registerAVItemObserver:(AVPlayerItem *)playerItem 
{ 
    [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:(void*)&LoadingItemContext]; 
} 

- (void)removeAVItemObserver:(AVPlayerItem *)playerItem 
{ 
    @try { 
    [playerItem removeObserver:self forKeyPath:@"status"]; 
    } 
    @catch (...) { } 
} 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
if (context == &LoadingItemContext) { 
     AVPlayerItem *item = (AVPlayerItem*)object; 
     AVPlayerItemStatus status = item.status; 
     if (status == AVPlayerItemStatusReadyToPlay) { 
      // now you know you can set your player to play, update your UI... 
     } else if (status == AVPlayerItemStatusFailed) { 
      // handle error here, i.e., skip to next item 
     } 
    } 
} 

這只是一個預4.3例子。 4.3之後,您可以使用AVFoundation Programming Guide, Preparing an Asset For Use中的代碼示例(loadValuesAsynchronouslyForKeys:completionHandler)加載遠程文件(或HTTP Live播放列表)。如果您使用loadValuesAsynchronouslyForKeys作爲HTTP Live流,則應該觀察@「tracks」屬性。