Search Unity

Question How to unload video with URL?

Discussion in 'Audio & Video' started by inkletom, Jul 28, 2020.

  1. inkletom

    inkletom

    Joined:
    Oct 13, 2016
    Posts:
    63
    Hey! I've noticed that setting "videoPlayer.url" to null causes an error - it seems that it tries to load it as a URL.
    I'm looking to unload the playing video and reset the state of the player for another video.

    What is the correct way to reset a VideoPlayer?

    Ta!
    Tom
     
    CloudyVR likes this.
  2. DominiqueLrx

    DominiqueLrx

    Unity Technologies

    Joined:
    Dec 14, 2016
    Posts:
    260
    Hi Tom!

    Indeed, the VideoPlayer is pesky in that it doesn't allow you to clear the url once it has been set. But just calling Stop() will clear any resource that has been allocated.

    Hope this helps,

    Dominique Leroux
    A/V developer at Unity
     
  3. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    Trying to clear the current video by:

    videoPlayer.url = null;

    -or-
    videoPlayer.url = "";


    results in:

    ERROR: Can't play movie []

    Is there any way around this? Or can I check if there is a loaded resource? Otherwise my code thinks the videoplayer is always loaded.

    Checking
    videoPlayer.isPrepared
    does not work either, how do I fix this?
     
  4. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    I managed to solve this by adding a new VideoPlayer and destroying the old one (while copying all parameters except for the URL).

    Here's an extension method that does all the work of copying the existing property values to the new VideoPlayer with the exception of the URL field:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.Video;
    3. using System.Reflection;
    4. public static class VideoPlayerExtensions
    5. {
    6.    public static VideoPlayer MakeCopy(this VideoPlayer original, bool copyURL=false)
    7.    {
    8.       var copy = original.gameObject.AddComponent<VideoPlayer>();
    9.       PropertyInfo[] p = original.GetType().GetProperties();
    10.  
    11.       foreach (PropertyInfo prop in p)
    12.       {
    13.          if (!copyURL && prop.Name.Equals("url"))
    14.             continue;
    15.          try
    16.          {
    17.             prop.SetValue(copy, prop.GetValue(original));
    18.          }
    19.          catch
    20.          {}
    21.       }
    22.    
    23.       GameObject.Destroy(original);
    24.  
    25.       return copy;
    26.    }
    27. }
    28.  
    29. //Usage example:
    30. class VideoPlayerExample : MonoBehaviour
    31. {
    32.    [SerializeField]
    33.    private VideoPlayer m_videoPlayer;
    34.  
    35.    public void OnEnable()
    36.    {
    37.       m_videoPlayer = m_videoPlayer.MakeCopy(); //make a copy of the video player and all properties except for the URL
    38.    }
    39. }
    It would be very nice if Unity would add a Unload() function to their video player, but since Unity doesn't seem to be concerned about this I had to develop my own solution.

    I hope this helps someone.