- Home /
How to loop a video seamless
As read in many forums, using built-in looping in VideoPlayer doesn't work well as there is a short audio break before the video restarts. I've tried many solutions... but no way. In the end I've tried to adapt a solution found in StackOverflow according to my needs:
using System.Collections;
using UnityEngine;
using UnityEngine.Video;
public class MegaPlayer : MonoBehaviour
{
private VideoPlayer[] players = new VideoPlayer[2];
public RenderTexture targetTexture;
public string url;
void Awake()
{
for (int i = 0; i < players.Length; i++) {
players[i] = gameObject.AddComponent<VideoPlayer>();
players[i].source = VideoSource.Url;
players[i].url = url;
players[i].playOnAwake = false;
players[i].waitForFirstFrame = true;
players[i].isLooping = false;
players[i].skipOnDrop = true;
players[i].targetTexture = targetTexture;
}
}
void Start()
{
StartCoroutine(Play(0));
}
IEnumerator Play(int activePlayer)
{
// wait until the video is ready
players[activePlayer].Prepare();
while (!players[activePlayer].isPrepared) {
yield return null;
}
players[activePlayer].Play();
// wait until the current video has not finished
bool reachedHalfWay = false;
int nextPlayer = activePlayer ^ 1;
while (players[activePlayer].isPlaying) {
if (!reachedHalfWay && players[activePlayer].time >= (players[activePlayer].length / 2)) {;
reachedHalfWay = true;
players[nextPlayer].time = 0f;
players[nextPlayer].Prepare();
}
yield return null;
}
while (!players[nextPlayer].isPrepared) {
yield return null;
}
StartCoroutine(Play(nextPlayer));
}
}
The idea is to simulate isLooping
by creating 2 VideoPlayer
instances that alternate each other. When one play is half the way, I start preparing the second one so that it is ready before the current play ends.
The problem is that the result is a pure mess. It looks like both players start together anyway. Does anybody have a better idea on how to implement a seamless video looping?
Answer by pauldarius98 · Mar 02, 2021 at 02:05 PM
What i think that is happening is that when you Add the VideoPlayer, it will automatically play (because by default playOnAwake is on) and Awake is called when the object is created, before you assign the value of playOnAwake to off. What you can do is simply add at line 21 the following code:
players[i].Stop();
Either the solution above or add the videoPlayers manually on the gameObject and turn off playOnAwake (then do the setup in awake as it is now but you will already have a refference for the array of VideoPlayer so you no longer need GetComponent).
Thank u very much for the hint... but am I not creating the players manually and assigning them to the current gameObject at line 14? Perhaps I'm missing something...
I was talking about assign them in the editor. When AddComponent is called the Awake method from VideoPlayer runs and so when you do players[i].playOnAwake = false is too late cause the VideoPlayer already initialized and started to play the video (not completely sure but most likely this is the case). just call the Stop method as i described in the answer and it should work
Thank u very much for your support. It was really helpfull :-)