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

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

问题描述

这样做:我有一个 AVPlayer ,在加载后立即播放视频。



在开始的视频,我想在给定的位置玩它。所以我等待资源准备好使用KVO的Play:

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

然后寻找给定时间

  [playerItem seekToTime:timeInTheMiddleOfTheVideo completionHandler:myHandler]; 

但播放器将始终从初始搜索的视频开始。



更新
我尝试了一个dispatch_after几秒钟,但也不工作。它在播放后工作,但从来没有。 self.player.currentItem.duration 我一直得到 0



作为 timescale

解决方案

几个可能会帮助你的事情:


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


  2. 15秒,则它可以从视频的开始开始,而不是最近的关键帧的位置。要强制玩家寻找确切的位置(注意:这将需要几秒钟时间),请执行以下操作:

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

    或者您可以在指定点之前/之后显式地寻找关键帧,方法是指定一个参数infinity:

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



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

This doesn't:

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)

And then seek to the given time

[playerItem seekToTime:timeInTheMiddleOfTheVideo completionHandler:myHandler];

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

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

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

解决方案

Couple of things which may help you:

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

  2. 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天全站免登陆