两个AVPlayer视频不同步(快速) [英] Two AVPlayer videos out of sync (Swift)
问题描述
我有两个AVPlayer()
项目正在播放持续时间相同(10秒)的视频.目标是让它们循环并彼此保持同步.我将它们添加为相同UIView
的子层,然后在它们中的每一个上调用player.play()
.
I have two AVPlayer()
items playing videos of the same duration (10 seconds). The goal is to have them loop and stay in sync with one another. I add them as sublayers of the same UIView
and then call player.play()
on each one of them.
问题是,由于代码执行显然有最小的延迟,因为一个执行在另一个执行之后被调用,因此视频不同步(尽管只有几毫秒,但很明显).
The problem though is that as code execution obviously has the slightest delay as one is called after the other one, the videos are out of sync (although only a few milliseconds, it is noticeable).
我没有看到其他帖子所建议的创建AVMutableComposition的选项,那么是否有两个真正独立的播放器真正保持同步并同时播放呢?
I do not have the option to create an AVMutableComposition as I have seen other posts suggest, so is there anyway to have two separate players truly stay in sync and play EXACTLY at the same time?
谢谢!
推荐答案
如果要实现同步,则应使用AVPlayer分别加载视频并观察每个播放器的AVPlayerItemStatus
属性.只有当所有播放器的状态都为.readyToPlay
时,您才可以循环播放播放器并设置.rate
属性.
If you want to achieve the sync, you should load the videos separately with AVPlayer and observe the AVPlayerItemStatus
property of each player. Only when all of the players have the status .readyToPlay
you can loop through the players and set the .rate
property.
您还可以使用setRate(_:time:atHostTime:)
同步它们.不要忘记在调用setRate
之前开始使用preroll(atRate:completionHandler:)
加载媒体数据.基本上:
You can also synchronize them by using setRate(_:time:atHostTime:)
. Don't forget begin loading media data using preroll(atRate:completionHandler:)
before calling setRate
. Basically:
- 等待
readyToPlay
-
preroll(atRate:completionHandler:)
当所有玩家都准备就绪时 -
setRate(_:time:atHostTime:)
所有玩家都已预卷
- wait for
readyToPlay
preroll(atRate:completionHandler:)
when all players are readysetRate(_:time:atHostTime:)
when all players were prerolled
这篇关于两个AVPlayer视频不同步(快速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!