Search Unity

Question How to add loading, before play Video from server

Discussion in 'Audio & Video' started by Virat-Gangurde, Jun 29, 2020.

  1. Virat-Gangurde

    Virat-Gangurde

    Joined:
    Aug 20, 2019
    Posts:
    12
    I want to play video which is located on Apache server. But depends on network it's taking some time. I want to add loading bar for video. I have done loading bar for image, but stuck for videos. Below code snippet for image texture loading from server.

    using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url))
    {
    while (!uwr.isDone)
    {
    UnityWebRequestAsyncOperation operation = uwr.SendWebRequest();
    loadingScreen.SetActive(true);
    //loadingCanvas.GetComponent<Canvas>().targetDisplay = 0;
    DashboardCanvas.GetComponent<Canvas>().targetDisplay = 0;
    while (!operation.isDone)
    {
    float progress = Mathf.Clamp01(operation.progress / 0.9f);
    slider.value = progress;
    int progressInt = Convert.ToInt32(progress * 100f);
    yield return null;
    }
    loadingScreen.SetActive(false);
    }
    }

    As highlighted in bold, I could not figure out which method to be used to retrieve video server.
    Appreciate your help and Thanks in advanced.
     
  2. DominiqueLrx

    DominiqueLrx

    Unity Technologies

    Joined:
    Dec 14, 2016
    Posts:
    260
    Hi!

    The usual way to play a movie from the web, with Unity, is to point the VideoPlayer to the movie using the VideoPlayer.url property and calling Play(). But this, as you are pointing out, does not report buffering progress and is indeed a missing feature in the VideoPlayer that we may address eventually.

    Here are two options; none of them is perfect but maybe adequate for your use case:
    1. Instead of calling VideoPlayer.Play() immediately, you can add a handler on the VideoPlayer.prepareCompleted event and then call VideoPlayer.Prepare(). This does not expose progress, but at least you can have a spinning cursor in the mean time and have it disappear as soon as buffering is considered sufficient to start playback.
    2. If it's acceptable for you to not start the playback until the movie is fully downloaded, then you can just use UnityWebRequest.Get directly to download the file locally. This will get you the same progress reporting that you're using for textures. Once downloaded, you can have your VideoPlayer use the local file by setting the VideoPlayer.url property to the filesystem path of the downloaded file.
    Hope this helps,

    Dominique Leroux
    A/V developer at Unity
     
    Virat-Gangurde likes this.
  3. Virat-Gangurde

    Virat-Gangurde

    Joined:
    Aug 20, 2019
    Posts:
    12
    Thanks for Solution DominiqueLrx