- Home /
Unity 5.6 videoplayer: How to get the total length of a video?
How do I get the total length of a video played back in the videoplayer (new since Unity 5.6)? A C# code snippet would be awesome! I need this for a couple of things, e. g. for creating a progress bar. Thanks
You can start with following script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
using System;
public class VIdeoBeheviour : $$anonymous$$onoBehaviour {
public VideoPlayer vplayer;
public Slider videoslider;
public bool dragging = false;
void Start()
{
vplayer.Play ();
Debug.Log ("started " + vplayer.frameCount);
}
void Update()
{
if (dragging) {
vplayer.frame = Convert.ToInt64 (videoslider.value);
}
else
{
videoslider.maxValue = vplayer.frameCount;
videoslider.value = vplayer.frame;
}
//Debug.Log ("update " + vplayer.frame + " " + vplayer.frameCount + " " + videoslider.value);
}
public void OnBeginDrag()
{
dragging = true;
}
public void OnEndDrag()
{
dragging = false;
}
}
BESIDE This you need to create add event trigger for the slider as shown in the picture .....i just created this script few days ago ....It works fine for unity engine , perhaps desktop also but it is slow for android i will keep you up to date if i find the solution . For creating event trigger :- go to add component and add event trigger Add a new event type onpointerdown and onpointerup * put the above script on a gameobject and and drag that gameobject in the second picture area .... now see the first image and implement the same functions.
There are some debug functions its for my own purpose but you can skip it on script if you want.
:)
@kharal Your example is awesome! $$anonymous$$any thanks! Attached the UnityPackage demonstrating this for download. link text
Answer by ashishsfb · May 17, 2017 at 07:22 AM
@vrbullshit Length of a video is the property of a video clip instead of the video player. So to get the length of a video clip, through a video player you can use
float length = videoPlayer.clip.length;
@ashishsfb Thanks! It works. However, it´s a double, not a float.
Your answer
Follow this Question
Related Questions
Play, seek a video 1 Answer
How to play video on WEBGL 0 Answers
Unity4.7.2 IOS13.1 Handheld.PlayFullScreenMovie can't play video. 0 Answers
VideoPlayer Completed Event Handler 0 Answers