AVPlayer seekToTime无法在正确的位置播放 [英] AVPlayer seekToTime does not play at correct position

查看:2038
本文介绍了AVPlayer seekToTime无法在正确的位置播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个播放HLS视频流的AVPlayer。我的用户界面提供了一行按钮,一个用于视频中的每个章节(按钮标记为1,2,3)。该应用程序从服务器下载一些元数据,该服务器包含以秒表示的章节切入点列表。例如,一个视频的长度为12分钟 - 章节切入点的列表为0,58,71,230,530等等。

I have an AVPlayer which is playing a HLS video stream. My user interface provides a row of buttons, one for each "chapter" in the video (the buttons are labeled "1", "2", "3"). The app downloads some meta-data from a server which contains the list of chapter cut-in points denoted in seconds. For example, one video is 12 minutes in length - the list of chapter cut-in points are 0, 58, 71, 230, 530, etc., etc.

当用户点击其中一个章节按钮时,按钮处理程序代码执行以下操作:

When the user taps one of the "chapter buttons" the button handler code does this:

            [self.avPlayer pause];

    [self.avPlayer seekToTime: CMTimeMakeWithSeconds(seekTime, 600) 
              toleranceBefore: kCMTimeZero 
               toleranceAfter: kCMTimeZero 
            completionHandler: ^(BOOL finished) 
            {
                [self.avPlayer play];
            }];

其中seekTime是包含切入点的局部变量(如上所述)。

Where "seekTime" is a local var which contains the cut-in point (as described above).

问题是视频并不总是从正确的点开始。有时确实如此。但有时它会在请求的seekTime之前的任何时间从十分之一秒到2秒。它永远不会在请求的seekTime之后启动。

The problem is that the video does not always start at the correct point. Sometimes it does. But sometimes it is anywhere from a tenth of a second, to 2 seconds BEFORE the requested seekTime. It NEVER starts after the requested seekTime.

以下是视频编码的一些统计数据:

Here are some stats on the video encoding:

编码器:handbrakeCLI
编解码器:h。 264
帧率:24(实际上,23.976 - 与拍摄方式相同)
视频比特率:多比特率(64/150/300/500/800/1200)
音频比特率: 128k
关键帧:23.976(每秒1次)

Encoder: handbrakeCLI Codec: h.264 Frame rate: 24 (actually, 23.976 - same as how it was shot) Video Bitrate: multiple bitrates (64/150/300/500/800/1200) Audio Bitrate: 128k Keyframes: 23.976 (1 per second)

我当然使用Apple mediafilesegmenter工具和variantplaylistcreator来生成播放列表。

I am using the Apple mediafilesegmenter tool, of course, and the variantplaylistcreator to generate the playlist.

这些文件是从Amazon Cloud / S3存储桶提供的。

The files are being served from an Amazon Cloud/S3 bucket.

我还不清楚的一个方面是CMTimeMakeWithSeconds - 我根据我读过的不同文章/文档尝试了几种变体。例如,在上面的摘录我正在使用:

One area which I remain unclear about is CMTimeMakeWithSeconds - I have tried several variations based on different articles/docs I have read. For example, in the above excerpt I am using:

CMTimeMakeWithSeconds(seekTime,600)

CMTimeMakeWithSeconds(seekTime, 600)

我也有试过:

CMTimeMakeWithSeconds(seekTime,1)

CMTimeMakeWithSeconds(seekTime, 1)

我不知道哪个是正确的,尽管看起来似乎产生相同的不一致结果!

I can't tell which is correct, though BOTH seem to produce the same inconsistent results!

我也试过:

CMTimeMakeWithSeconds(seekTime,23.967)

CMTimeMakeWithSeconds(seekTime, 23.967)

有些文章声称这类似于分子/分母,所以n / 1应该是正确的,其中'n'是秒数(如CMTimeMakeWithseconds(n,1))。但是,代码最初是由一个不同的程序员(现在已经不在了)创建的,他使用了600号码作为preferredTimeScale(即CMTimeMakeWithseconds(n,600))。

Some articles claim this works like a numerator/denomenator, so n/1 should be correct where 'n' is number of seconds (as in CMTimeMakeWithseconds(n, 1)). But, the code was originally created by a different programmer (who is gone now) and he used the 600 number for the preferredTimeScale (ie. CMTimeMakeWithseconds(n, 600)).

任何人都能提供任何关于我做错的线索,或者即使我想要实现的那种准确性甚至可能吗?

Can anyone offer any clues as to what I am doing wrong, or even if the kind of accuracy I am trying to achieve is even possible?

如果有人想提供替代解决方案,我们已经在考虑将视频分成不同的流,每章一个,但我们不相信将给我们相同的性能,因为更改章节将需要更长的时间,因为必须创建和加载新的AVPlayerItem等等等。所以如果你认为这是唯一可行的解​​决方案(我们我希望这会达到我们想要的结果 - 也就是说,每章都会在我们想要的地方开始。)随意说出来。

And in case someone is tempted to offer "alternative" solutions, we are already considering breaking the video up into separate streams, one per chapter, but we do not believe that will give us the same performance in the sense that changing chapters will take longer as a new AVPlayerItem will have to be created and loaded, etc., etc., etc. So if you think this is the only solution that will work (and we do expect this will achieve the result we want - ie. each chapter WILL start exactly where we want it to) feel free to say so.

提前致谢!

推荐答案

int32_t timeScale = self.player.currentItem.asset.duration.timescale;
CMTime time = CMTimeMakeWithSeconds(77.000000, timeScale);
[self.player seekToTime:time toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];

我遇到'seekToTime'问题。我用这段代码解决了我的问题。 'timescale'是解决这个问题的绝招。

I had a problem with 'seekToTime'. I solved my problem with this code. 'timescale' is trick for this problem.

这篇关于AVPlayer seekToTime无法在正确的位置播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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