Accessing dimensions (width/height) from a VideoPlayer source URL

爱⌒轻易说出口 提交于 2019-12-02 01:29:00

问题


I am working on a Unity project that requires me to download a video file (mp4 format) and play it using the VideoPlayer component. Because the file is downloaded at runtime, I effectively have to "stream" it via a url to its downloaded location, as opposed to loading it as a VideoClip.

To accurately size the target RenderTexture and GameObject the video will be playing to, I need the dimensions of the video file itself. Because it is not a VideoClip I cannot use:

VideoClip clip = videoPlayer.clip;
float videoWidth = clip.width;
float videoHeight = clip.height;

Because I am using a source URL as opposed to a VideoClip, this will return null.

Can I get the video dimensions directly from the file somehow?


回答1:


You can retrieve these information from the Texture that VideoPlayer constructs.

Get VideoPlayer

VideoPlayer videoPlayer = GetComponent<VideoPlayer>();

Get the Texture VideoPlayer

Texture vidTex = videoPlayer.texture;

Get VideoPlayer dimension width/height

float videoWidth = vidTex.width;
float videoHeight = vidTex.height;

Make sure to only get the texture after videoPlayer.isPrepared is true. See my other answer for full code on how to play video make it display on RawImage component.



来源:https://stackoverflow.com/questions/45307892/accessing-dimensions-width-height-from-a-videoplayer-source-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!