首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVQueuePlayer流状态?

AVQueuePlayer流状态?
EN

Stack Overflow用户
提问于 2011-04-03 21:48:46
回答 1查看 2.8K关注 0票数 0

我对Obj-C和iOS开发非常陌生,我在这里找到了很多有用的信息,但有一个问题我没有找到答案。

我得到了AVQueuePlayer的实例,它可以从url播放音频流。

如何知道加载了音频流?例如,当我按下“播放”按钮时,在按下按钮和实际开始流媒体之间有几秒钟的延迟。我查看了developer.apple.com库,没有找到任何可以用来检查AVQueuePlayer状态的方法。在AVPLayer中有一个,但据我所知,AVPlayer不支持http上的流。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2011-04-20 12:34:47

我不确定你所说的“已加载”是什么意思:你是指当项目完全加载时,还是当项目准备好播放时?

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

最常见的情况是当一个项目准备好播放时,所以我将回答这个问题。如果您使用的是AVQueuePlayer < 4.3的iOS,则需要通过观察AVPlayerItem状态键的值来检查AVPlayerItem的状态:

代码语言:javascript
复制
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播放列表)。如果你对HTTP Live流使用loadValuesAsynchronouslyForKeys,你应该注意@"tracks“属性。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5529955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档