- Home /
How Can I Get Rid Of Lag When Using the VideoPlayer in 5.6?
I have a screen in one of my scenes that I want to use to cycle through various videos that are read from a local directory. An array of available videos is generated based on what is present at a specified path, and whenever one video finishes, the video player moves to the next one in the array. Once it is at the end of the array, it goes back to the first video.
The code works the way I'd like it to. The only problem is that whenever the url is changed, the game lags rather noticeably for a couple of seconds. What makes it even worse is that this is a VR application, so the headset will just go grey and show the Unity loading screen until it corrects itself.
I was wondering if anyone had any idea as to how I could optimize this code such that there wouldn't be any lag between videos.
private void Update()
{
if (screen.url != "")
{
//Move to the next video once the current one is done
if (screen.time > 0 && !screen.isPlaying)
if (currentVideo>= gamesWithTrailers.Length)
currentVideo = 0;
else
screen.url = gamesWithTrailers[currentVideo++].trailerPath;
}
}
Did you find an answer to the lag? I have the same problem, I even tried upgrading to 2017.1 but the problem persists. @hurleybird ?
Answer by hurleybird · May 02, 2017 at 01:56 AM
I'm dealing with the same issue right now. As it stands, I don't believe there's any way around it. Hopefully this case gets addressed in a future version of the engine.
However, it's probably not a good idea to switch between clips in your update function since that could throw the timing off. Subscribe a method to VideoPlayer.loopPointReached instead. Eg.
private VideoPlayer player;
private string moviesFolder;
private string[] fileNames;
private int index;
public void Awake()
{
moviesFolder = Application.dataPath.Remove(Application.dataPath.LastIndexOf('/')) + "/Game Data/Movies/";
player.loopPointReached += PlayNext;
}
public void Play(string[] _fileNames)
{
index = 0;
fileNames = _fileNames;
player.url = moviesFolder + fileNames[index] + ".mp4";
player.Play();
}
public void PlayNext(VideoPlayer vPlayer)
{
if (index < fileNames.Length - 1)
index++;
vPlayer.url = moviesFolder + fileNames[index] + ".mp4";
vPlayer.Play();
}