0

I'm trying to load and play a Video from a URL using the Unity VideoPlayer and everything works using this steeps.

Now I'm facing some troubles because I'm not able to access the clip properties throught my code, in fact when I use an URL to load a VideoClip the clip property in the VideoPlayer always return null.

I want this properties because I'm interested in the Width and Height of the video clip so I can resize them accord to my canvas or panel and it may vary depending on the video aspect ratio.

Because I'm using a RenderToTexture to display the video every time I ask for the Width and Height it returns the {w,h} of the RTT instead of the {w,h} of the video.

Is there a way to get arround this?

Thanks in advance.

I'm using Unity 2017.3.0f3

public string VideoPath = string.Empty;
public VideoPlayer player;

public delegate IEnumerator OnVideoReady();
public static event OnVideoReady VideoReady;

IEnumerator Start()
{
    player = GetComponent<VideoPlayer>();

    player.playOnAwake = false;
    player.enabled = false;

    yield return null;

    // ABSOLUTE PATH TO THE VIDEO FILE
    var path = UnifiedIO.SystemIO.Internal.Path.GetFullPath(VideoPath);

    if(Debug.isDebugBuild)
    {
        Debug.Log("*** Path to 360 file : " + path);
    }
    // PREPARE ALL THE MEDIA DATA
    player.source = VideoSource.Url;
    player.url = path;
    player.playOnAwake = true;
    player.enabled = true;
    player.Prepare();

    yield return new WaitUntil(()=> player.isPrepared); 
    player.Play();


    if(Debug.isDebugBuild)
    {
        Debug.LogFormat("*** Video Player information; aspect ratio {0}; frame {1}; frame count {2}, frame rate {3}," +
                        "playback speed {4}, source {5}, url {6}",
                        player.aspectRatio,
                        player.frame, 
                        player.frameCount,
                        player.frameRate, 
                        player.playbackSpeed, 
                        player.source,
                        player.url);


        Debug.LogFormat("*** Clip information; frame rate {0}, Width {1}, height {2}, lenght {3}, name {4}, " +
                        "original path {5}, pixel aspect ratio denominator {6}, pixel aspect ratio numerator {7}",
                        player.clip.frameRate,
                        player.clip.width,
                        player.clip.height,
                        player.clip.length,
                        player.clip.name,
                        player.clip.originalPath,
                        player.clip.pixelAspectRatioDenominator,
                        player.clip.pixelAspectRatioNumerator);
    }

    if (VideoReady != null)
        StartCoroutine(VideoReady());

    yield return new WaitForEndOfFrame();

    if(FindObjectOfType<PanelFadeAnimation>())
        FindObjectOfType<PanelFadeAnimation>().EnlightenScene();
}
  • *" I ask for the Width and Height it returns the {w,h} of the RTT instead of the {w,h} of the video."* Please post the code you are using to do this and what do you mean by RTT? You just want to get width and height of the video? – Programmer Jan 12 '18 at 18:44
  • @programmer, I just update the question and add my code, in fact it's the code you use to play the video. RTT is RenderToTexture and yes, I want the width and height of the video so I can operate them but it says is null. When I use a URL in the VideoPlayer ther's no VideoClip attached? Is the video streamed? – Salvador Lemus Jan 12 '18 at 19:25
  • 1
    First of all, you don't wait for `Prepare();` to get ready with `WaitForSeconds(1);`. You yield `videoPlayer.isPrepared` until it's true. That's because there is no guarantee that prepare will finish within x second. This was probably why you were having issues with the original code. The duplicate shows how to get video dimension from url. The second duplicate link shows how to do that from a local video file. You need the first one. – Programmer Jan 12 '18 at 19:30
  • Ok, I just edit my code to remove the WaithForSeconds and, instead, waith for the VideoPlayer to be ready. Wich one link are you talking about? Even if I yield my function and waith for the VideoPlayer to be ready, it returns null when I try to access the clip properties. (Sorry, I just notice the kinks. I'll try those first and comment my results. Thanks) – Salvador Lemus Jan 12 '18 at 19:44
  • Yep, that solves everything. Thanks. – Salvador Lemus Jan 12 '18 at 19:50

0 Answers0