对于HTTP直播,iOS AVAsset.duration为零,但适用于渐进式 [英] iOS AVAsset.duration is zero for HTTP live streaming but works for progressive
问题描述
我有一个iOS应用程序,可以播放来自HTTP Live流playlist.m3u8的视频,我有一个使用AVPlayer创建的自定义播放器。为了处理正常的用户交互,例如擦洗,我需要获取视频的持续时间,但出于某种原因,在使用xcode 4.0的iOS 4.3上,当我调用以下代码时,我得到一个CMTime,当转换为秒时给出一个NaN - 我知道它正在做什么,因为CMTimeValue = 0和CMTimeScale = 0,它给出NaN和CMTimeFlags = 17,这更奇怪。
I have an iOS app that plays video from a HTTP Live stream "playlist.m3u8" and I have a custom player created using AVPlayer. To handle normal user interactions such as scrubbing i need to get the duration of the video, but for some reason on iOS 4.3 using xcode 4.0 when I call the following code I get a CMTime that when converted to seconds gives a NaN -- I know what it's doing because CMTimeValue = 0 and CMTimeScale = 0 which gives the NaN and the CMTimeFlags = 17 which is even more strange.
这是我使用的代码不是复杂的:
Here's the code I uses which isn't complex at all:
AVPlayerItem *pItem = mPlayer.currentItem;
AVAsset* asset = pItem.asset;
CMTime d = asset.duration;
double duration = CMTimeGetSeconds(asset.duration);
我还应该提一下,我确实监控加载播放列表的状态,以确保它在我之前准备就绪开始播放/擦洗:
I should also mention that I do monitor the status of the loading playlist to make sure it's ready before i start playing/scrubbing:
[mPlayer addObserver:self forKeyPath:@"currentItem.status" options:0 context:VideoPlaybackViewDelegateStatusContext];
感谢您对此问题的任何帮助。
Thanks for any help on this issues anyone could provide.
推荐答案
上面提到的文档现在应该从 AVPlayerItem
实例中获取持续时间,而不是相应的 AVAsset
。为了通过键值观察获得当前玩家项目的持续时间,我使用以下方法(最初来自 NGMoviePlayer
是为iOS 4.0编写的:
The docs above mention that duration should now be obtained from the AVPlayerItem
instance, rather than its corresponding AVAsset
. To get the duration from the current player item via key-value observing, I use the following method (originally pulled from NGMoviePlayer
which was written for iOS 4.0):
- (void)loadPlayerWithItem:(AVPlayerItem *)playerItem {
self.player = [AVPlayer playerWithPlayerItem:playerItem];
...
// changed this from previous value currentItem.asset.duration
[self.player addObserver:self forKeyPath:@"currentItem.duration"
options:0
context:nil];
...
}
我在播放器中实现了上述更改持续时间现在正在运作! AVFoundation的这一变化是该问题的根本原因。 CMTimeFlags = 17表示kCMTimeFlags_Indefinite& kCMTimeFlags_Valid,以及 docs 指定:
I implemented the above change in my player and the duration is working now! This change in AVFoundation was the root cause of the issue. CMTimeFlags = 17 indicates kCMTimeFlags_Indefinite & kCMTimeFlags_Valid, and the docs specify:
特别是,URL资产为基于流媒体的媒体报告的持续时间是通常为kCMTimeIndefinite,而相应的AVPlayerItem的持续时间可能不同,并且可能会在播放时发生变化。
In particular, the duration reported by the URL asset for streaming-based media is typically kCMTimeIndefinite, while the duration of a corresponding AVPlayerItem may be different and may change while it plays.
这篇关于对于HTTP直播,iOS AVAsset.duration为零,但适用于渐进式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!