- Home /
Question by
jRocket · Apr 05, 2017 at 01:09 PM ·
apimovietexture
5.6 VideoPlayer duration of URL
I am trying to get the duration of a video specified via URL in the new VideoPlayer. I know that VideoClips have a duration, but there doesn't seem to be an API for getting duration from a specified URL.
I have tried calculating it from frameCount and frameRate, ie
videoPlayer.prepareCompleted += (val) =>
{
duration = videoPlayer.frameCount / videoPlayer.frameRate;
}
but the value is off by a fraction of a second. Is there an accurate way to get the duration of video from a URL?
Comment
The fraction of a second offset doesn't bother me, but how do you use prepareCompleted in C# ?
Answer by Suraksha226 · Aug 21, 2018 at 07:45 AM
Here is my solution to get the total length of a video by using URL, i made use of C# System.TimeSpan
void TotalTimeOfVideo()
{
double time = videoplayer.frameCount / videoplayer.frameRate;
System.TimeSpan VideoUrlLength = System.TimeSpan.FromSeconds(time);
string minutes = (VideoUrlLength.Minutes).ToString("00");
string seconds = (VideoUrlLength.Seconds).ToString("00");
TotalMinutes.text = minutes;
TotalSeconds.text = seconds;
}
The video MUST be prepared to work:
videoPlayer.prepareCompleted += TotalTimeOfVideo();
videoPlayer.prepare();
If not, it will throw errors or NaN