我不確定你的意思是「加載」:你的意思是什麼時候該項目是滿載或當該項目準備播放?
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」屬性。