Hello all,
I am trying to create a simple control script so I can have basic video player control using Playmaker. I trying to figure out how to code the “Seek” function to skip to a designated frame while still playing the video. Here is my code thus far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class VideoPlayerControlMod : MonoBehaviour {
public UnityEngine.Video.VideoPlayer vPlayer;
public long longFrame;
public int currentFrame;
public int currentSeek;
public long longSeek;
void Update () {
longFrame = (vPlayer.frame);
currentFrame = (int)longFrame;
Debug.Log("frameCount " + vPlayer.frame);
}
void OnPlay()
{
vPlayer.Play();
}
void OnPause()
{
vPlayer.Pause();
}
void OnStop()
{
vPlayer.Stop();
}
void OnSeek()
{
longSeek = (long)currentSeek;
vPlayer.frame += (longSeek);
}
}
Everything is working except seeking. Any help would be much appreciated.

Hi Vern. You’re close. I’d do something like this:
using UnityEngine;
using UnityEngine.Video;
[RequireComponent( typeof( VideoPlayer ) )]
public class VideoPlayerControlMod : MonoBehaviour
{
private VideoPlayer vPlayer;
// Use this for initialization
void Start()
{
vPlayer = GetComponent<VideoPlayer>();
}
public void Play()
{
vPlayer.Play();
}
public void Pause()
{
vPlayer.Pause();
}
public void Stop()
{
vPlayer.Stop();
}
public void Seek( int frame )
{
vPlayer.frame = frame;
}
}
Hi there !
I’m also trying to use the video Player and has a similar problem with the seek function.
Here is mine, triggerred by a slider :
public void Seek(float nTime)
{
if (!IsPrepared)
{
Debug.Log("Video Not Prepared.");
return;
}
nTime = Mathf.Clamp(nTime, 0, 1);
vid.time = nTime * Duration;
Debug.Log("Seeking.");
}
It put the video into a material override just working fine. But when I seek in the video with the slider, I get “Seeking” in the debug log but almost 4/5 times, the video freezes… Looks like a refresh issue… any help ?
Edit : looks like the first seeking works well. But till the second one, the video image freezes, even if the video keeps playing (I see it with another slider with its value = video.Time).
@ThalesUnity What platform are you trying to use seeking on? I ran into problems on Mac, iOS, and Android. However, I got seeking working very well in WIndows (I have a powerful machine, so maybe that helps).
Here is my code if you want a look:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
public class VideoPlayerControl : MonoBehaviour {
[Header("Settings")]
[Range(0.1f, 10f)]
public float playbackSpeed = 1f;
[Header("UI Controls")]
public Slider progressBar;
//Private vars
VideoPlayer vPlayer;
public int vidFrameLength;
bool shouldStartPlaying = true;
// Use this for initialization
void Start () {
vPlayer = gameObject.GetComponent<VideoPlayer>();
vPlayer.playbackSpeed = playbackSpeed;
vPlayer.Prepare();
vidFrameLength = (int)vPlayer.frameCount;
progressBar.maxValue = vidFrameLength;
}
// Update is called once per frame
void Update () {
if(shouldStartPlaying && vPlayer.isPrepared){
Play();
shouldStartPlaying = false;
}
}
public void Play(){
vPlayer.Play();
}
public void Pause(){
vPlayer.Pause();
}
public void Stop(){
vPlayer.Stop();
}
//This is called by the slider (Event Trigger: End Drag)
public void SetFrameBySlider(){
JumpToFrame(progressBar.value);
}
public void JumpToFrame(float frame){
vPlayer.frame = (long)frame;
}
}
1 Like
I’m on Windows (7) too. I’m currently using a placeholder video for my tests. It looks like it is due to some issues in the video loading after seeking. I lowered the video speed (Playback speed) just to check, and at half speed I have almost no issue ! So that’s probably a performance issue…
OK, I’m on Windows 10 Pro, and I think that be part of the problem. Saw somewhere that Windows 7 had some issues with VideoPlayer. Also, what kind of performance spike are you seeing when you try seeking? I get about 50% CPU usage just when seeking, and I have a i7-6800k CPU. (My GPU, GTX 1080, does not take any of the load for whatever reason.) Also, are you preparing the video at Start?
Hello All
this is the solution
public void seek( )
{
if (videoPlayer && slider && slider.value != _setVideoSeekSliderValue)
{
videoPlayer.time=(slider.value * Duration);
}
}