iOS AVAsset.duration 对于 HTTP 实时流媒体为零,但适用于渐进式

发布于 2024-11-06 13:27:58 字数 696 浏览 3 评论 0 原文

我有一个 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎会甘心 2024-11-13 13:27:58

https://developer.apple.com/library/ios/releasenotes/AudioVideo/RN-AVFoundation-Old/#//apple_ref/doc/uid/TP40011199-CH1-SW4

上面的文档提到持续时间应该现在可以从 AVPlayerItem 实例获取,而不是其相应的 AVAsset。为了通过键值观察获取当前播放器项目的持续时间,我使用以下方法(最初从 NGMoviePlayer 是为 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 和 文档 指定:

特别是,基于流媒体的 URL 资产报告的持续时间通常为 kCMTimeIndefinite,而相应 AVPlayerItem 的持续时间可能不同,并且在播放时可能会发生变化。

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 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];
    ...
}

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文