Skip to main content

Queue video player

Plays an ST Queue on an ST Video Player, one video per entry, for the player who queued it. It sits on the video player's GameObject, so it gets OnVideoEnd and OnVideoError, and checks the queue's first entry (GetEntryId(0)) every frame instead of waiting for OnQueueChanged, which only reaches scripts on the queue's GameObject.

  • The device of the player whose turn it is calls PlayURL, so they are the video's requester and Sync To Requester follows them.
  • When the video ends or fails, that player's device removes their entry and the next player's device starts theirs.
  • When the queue empties, the requester's device (or the master, if the requester left) stops the video.

urlFormat builds the link from the entry: {0} is its key, {1} its text. For example, https://example.com/videos/{1}.mp4, where the text is the item's video name. Set the video player's controlAccess to Everyone, because each player starts their own video. The request queue example shows the queue.

QueueVideoPlayer.cs
using StageTime.SDK;
using UnityEngine;

/// <summary>
/// On an STVideoPlayer: plays the first entry of an STQueue for the player who queued it, so Sync To Requester follows
/// the player whose turn it is. Their own device starts the video (which makes them the requester) and removes their entry when
/// it ends or fails; the next player's device then starts theirs. When the queue empties, the requester's device (or
/// the master, if the requester left) stops the video. Each player starts their own video, so the video player's
/// controlAccess must be Everyone.
/// </summary>
public class QueueVideoPlayer : StageTimeBehaviour
{
/// <summary>The queue to play.</summary>
public STQueue queue;

/// <summary>The link for an entry: {0} is its key, {1} its text. Empty = the entry's text is the link.</summary>
public string urlFormat = "https://example.com/videos/{1}.mp4";

private STVideoPlayer video;
private int currentEntry; // the first entry this device last saw (0 = empty queue)
private string myUrl = ""; // the link this device started for its own entry ("" = none)

private void Start()
{
video = GetComponent<STVideoPlayer>();
}

// Polling one id per frame is cheaper than routing OnQueueChanged from the queue's GameObject.
private void Update()
{
int entry = queue.GetEntryId(0);
if (entry == currentEntry) return;
bool hadEntry = currentEntry != 0;
currentEntry = entry;
myUrl = "";
StageTimePlayer turnPlayer = queue.currentPlayer;
if (turnPlayer != null && turnPlayer.isLocal)
{
if (!video.canControl) Debug.LogWarning("QueueVideoPlayer: set the video player's controlAccess to Everyone");
myUrl = UrlFor(0);
video.PlayURL(myUrl);
}
else if (entry == 0 && hadEntry && IsVideoLead())
{
video.Stop();
}
}

public override void OnVideoEnd() => FinishMine();

public override void OnVideoError() => FinishMine();

// Only the player whose turn it is removes their entry, and only for the video they started (not one someone loaded since).
private void FinishMine()
{
if (myUrl.Length == 0 || video.currentUrl != myUrl || queue.GetEntryId(0) != currentEntry) return;
myUrl = "";
queue.Remove(0);
}

private bool IsVideoLead()
{
StageTimePlayer requester = video.requester;
return requester != null ? requester.isLocal : Networking.IsMaster;
}

private string UrlFor(int index)
{
if (string.IsNullOrEmpty(urlFormat)) return queue.GetText(index);
return urlFormat.Replace("{0}", queue.GetKey(index).ToString()).Replace("{1}", queue.GetText(index));
}
}