2011-04-03 79 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”属性。