Skip to main content

Video seek bar

A Slider that follows an ST Video Player and seeks it when dragged. Two labels show the position and the length.

  • The Slider's On Value Changed calls OnSeek. The script moves the handle with SetValueWithoutNotify, so OnSeek only runs when a player drags it.
  • A drag raises On Value Changed every frame; the script waits until the handle has stopped for a moment and sends one SetTime, which reaches everyone.
VideoSeekBar.cs
using StageTime.SDK;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// A seek bar for an STVideoPlayer: the handle follows the video, and the labels show the position and the length.
/// Drag the handle to seek for everyone (Slider → On Value Changed → VideoSeekBar.OnSeek). The seek is sent once the
/// handle has stopped moving for a moment, so a drag sends one seek rather than one per frame.
/// </summary>
public class VideoSeekBar : StageTimeBehaviour
{
public STVideoPlayer video;
public Slider slider;
public TMP_Text timeLabel;
public TMP_Text durationLabel;
public float seekDelay = 0.25f;

private bool seekPending;
private float lastMoveTime;
private float followAfter;

// Slider → On Value Changed. SetValueWithoutNotify below doesn't call it, so this is always the player's drag.
public void OnSeek()
{
seekPending = true;
lastMoveTime = Time.time;
}

private void Update()
{
if (video == null || slider == null) return;
float duration = video.duration;

if (seekPending)
{
if (duration > 0f) SetLabel(timeLabel, slider.value * duration);
if (Time.time - lastMoveTime < seekDelay) return;
seekPending = false;
if (duration > 0f && video.canControl) video.SetTime(slider.value * duration);
followAfter = Time.time + 1f; // give the seek a moment to land before following the video again
return;
}

if (Time.time < followAfter) return;
slider.SetValueWithoutNotify(duration > 0f ? Mathf.Clamp01(video.time / duration) : 0f);
SetLabel(timeLabel, video.time);
SetLabel(durationLabel, duration);
}

private void SetLabel(TMP_Text label, float seconds)
{
if (label == null) return;
int total = Mathf.FloorToInt(Mathf.Max(0f, seconds));
label.text = (total / 60).ToString() + ":" + (total % 60).ToString("00");
}
}