我有一个 iOS 应用程序,可以播放来自 HTTP Live 流“playlist.m3u8”的视频,并且我有一个使用 AVPlayer 创建的自定义播放器。为了处理正常的用户交互(例如擦洗),我需要获取视频的持续时间,但出于某种原因,在使用 xcode 4.0 的 iOS 4.3 上,当我调用以下代码时,我得到一个 CMTime,当转换为秒时给出 NaN - 我知道它在做什么,因为 CMTimeValue = 0 和 CMTimeScale = 0 给出了 NaN 和 CMTimeFlags = 17,这更奇怪。
这是我使用的代码,一点也不复杂:
AVPlayerItem *pItem = mPlayer.currentItem;
AVAsset* asset = pItem.asset;
CMTime d = asset.duration;
double duration = CMTimeGetSeconds(asset.duration);
我还应该提到,我确实监视加载播放列表的状态,以确保它在我开始播放/清理之前已准备好:
[mPlayer addObserver:self forKeyPath:@"currentItem.status" options:0 context:VideoPlaybackViewDelegateStatusContext];
感谢任何人都可以提供的有关此问题的任何帮助。
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.
发布评论
评论(1)
https://developer.apple.com/library/ios/releasenotes/AudioVideo/RN-AVFoundation-Old/#//apple_ref/doc/uid/TP40011199-CH1-SW4
上面的文档提到持续时间应该现在可以从
AVPlayerItem
实例获取,而不是其相应的AVAsset
。为了通过键值观察获取当前播放器项目的持续时间,我使用以下方法(最初从NGMoviePlayer
是为 iOS 4.0 编写的):我在我的播放器中实现了上述更改,并且持续时间现在正在运行! AVFoundation 中的这一更改是问题的根本原因。 CMTimeFlags = 17 表示 kCMTimeFlags_Indefinite & kCMTimeFlags_Valid 和 文档 指定:
https://developer.apple.com/library/ios/releasenotes/AudioVideo/RN-AVFoundation-Old/#//apple_ref/doc/uid/TP40011199-CH1-SW4
The docs above mention that duration should now be obtained from the
AVPlayerItem
instance, rather than its correspondingAVAsset
. To get the duration from the current player item via key-value observing, I use the following method (originally pulled fromNGMoviePlayer
which was written for iOS 4.0):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: