加载资源后立即在 AVPlayer 中寻找某个位置 [英] Seek to a certain position in AVPlayer right after the asset was loaded

查看:27
本文介绍了加载资源后立即在 AVPlayer 中寻找某个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效:我有一个 AVPlayer 可以在视频加载后立即播放.这很好用.

This works: I have an AVPlayer which plays a video right after it was loaded. And this works fine.

这不会:

我不想在开头播放视频,而是想在给定位置播放.所以我等到资产准备好使用 KVO 播放:

Instead of starting the video at the beginning, I want to play it at a given position. So I wait until the asset is ready for Play using KVO:

BOOL isReadyToSeek = (self.playerItem.status == AVPlayerStatusReadyToPlay)

然后寻找给定的时间

[playerItem seekToTime:timeInTheMiddleOfTheVideo completionHandler:myHandler];

但是播放器总是会在初始搜索时从视频的开头开始.

But the player will always start at the beginning of the video on the initial seek.

更新我在几秒钟内尝试了 dispatch_after ,但这也不起作用.它在播放后起作用,但最初从不起作用.

Update I tried a dispatch_after with a few seconds but that does not work either. It works after playback but never initially.

当我观察 self.player.currentItem.duration 时,我不断得到 0 作为 valuetimescale.

When I observe self.player.currentItem.duration I keep getting 0 as the value and timescale.

推荐答案

可能对您有帮助的几件事:

Couple of things which may help you:

  1. 在检查玩家是否准备好开始游戏时,我会检查玩家本身以及玩家的当前项目.

  1. When checking if the player is ready to play, I check both the player itself and also the player's current item.

if(self.player.status == AVPlayerStatusReadyToPlay &&
   self.player.currentItem.status == AVPlayerItemStatusReadyToPlay) {
    [self.player play];
}

  • 如果您想要例如15 秒,如果这是最近关键帧的位置,它可能会从视频的开头开始.要强制玩家寻找确切位置(注意:这需要几秒钟才能完成),请执行以下操作:

  • It's possible that if you seek to e.g. 15 seconds, it may start from the beginning of the video instead if that is the location of the nearest keyframe. To force the player to seek to the exact location (note: this will take several seconds to do), do this:

    Float64 seconds = 500.0f;
    CMTime targetTime = CMTimeMakeWithSeconds(seconds, NSEC_PER_SEC);
    [self.player seekToTime:targetTime
     toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
    

    或者您可以通过将参数之一指定为无穷大来明确地在指定点之前/之后寻找关键帧:

    Or you can seek to the keyframe explicitly before/after the specified point by specifying one of the arguments as infinity:

    [self.player seekToTime:targetTime
     toleranceBefore:kCMTimePositiveInfinity toleranceAfter:kCMTimeZero];
    

  • 这篇关于加载资源后立即在 AVPlayer 中寻找某个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆