Home > Blockchain >  Check for video player is ready
Check for video player is ready

Time:08-29

I am checking if the video player is done preparing so I can switch the current image to a render texture when playing. Currently I am having a slight delay to switch it. But is there a way to check if the video is ready to play because without the delay, I get a black render texture for half a second.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
 
public class VideoPlay : MonoBehaviour {
    private VideoPlayer vp;
    public RenderTexture rt;
    private RawImage image;
 
    // Start is called before the first frame update
    void Start () {
        rt.Release ();
        vp = GetComponent<VideoPlayer> ();
        image = GetComponent<RawImage> ();
 
        PlayVideo ();
    }
 
    public void PlayVideo () {
        vp.Prepare ();
        vp.Play ();
        StartCoroutine("FlipRT");
    }
 
    IEnumerator FlipRT(){
        yield return new WaitForSeconds(0.5f);
        image.texture = rt;
    }
 
}

CodePudding user response:

videoPlayer has an event called .prepareCompleted which fires once the video is ready

void Start () {
      rt.Release ();
      vp = GetComponent<VideoPlayer> ();
      image = GetComponent<RawImage> ();
      vp.prepareCompleted =OnVideoReady;
      PlayVideo ();
}
void OnVideoReady(VideoPlayer readyPlayer)
{
    readyPlayer.Play ();
    StartCoroutine(FlipRT());
}
public void PlayVideo ()
{
    vp.Prepare ();    
}

On a side note, starting a coroutine using a string is not a great idea, as it might fail in runtime, and is using slow reflection lookup to find an ienumerator which is defined literally in the next line

  • Related