Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
short Unity C# script example for smoothly cutting / playing a playllist of VideoClips, by using one VideoPlayer (activeCam) to play the video and another VideoPlayer (otherCam) to cue up the next video... MIT License.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
public class SmoothVideoPlaylist : MonoBehaviour {
// to smoothly cut between two videos, we need two VideoPlayers
// make sure you create two VideoPlayers and assign them in the Inspector
public VideoPlayer activeCam, otherCam;
// edit this in the inspector, and fill it with your video clips
public List<VideoClip> playlist = new List<VideoClip>();
// internal var, keeps track of whether there's another clip cued up
VideoClip nextClip;
void Start () {
// play the first video in the playlist
PrepareNextPlaylistClip();
SwitchCams(activeCam);
// setup an event to automatically call SwitchCams() when we finish playing
activeCam.loopPointReached += SwitchCams;
otherCam.loopPointReached += SwitchCams;
}
void Update () {
// when the current video is 0.1 seconds away from ending, prepare the next video clip on otherCam
if ( nextClip == null && activeCam.time >= activeCam.clip.length-0.1 ) {
PrepareNextPlaylistClip();
}
// FAST FORWARD CHEAT, hold down F to speed-up video
if ( Input.GetKey(KeyCode.F) ) {
Time.timeScale = 16f;
activeCam.playbackSpeed = 16f;
otherCam.playbackSpeed = 16f;
} else {
Time.timeScale = 1f;
activeCam.playbackSpeed = 1f;
otherCam.playbackSpeed = 1f;
}
}
// swaps the VideoPlayer references, so activeCam is always the active VideoPlayer
void SwitchCams (VideoPlayer thisCam) {
activeCam = otherCam;
otherCam = thisCam;
activeCam.targetCameraAlpha = 1f;
otherCam.targetCameraAlpha = 0f;
Debug.Log("new clip: " + nextClip.name );
nextClip = null;
}
// cues up the next video clip in the playlist
void PrepareNextPlaylistClip () {
nextClip = playlist[0];
otherCam.clip = nextClip;
otherCam.Play();
playlist.RemoveAt(0);
}
}
@DeafPhilosopher1

This comment has been minimized.

Copy link

DeafPhilosopher1 commented Mar 11, 2020

This works beautifully. I wondered, how do I click on an UI button to go to next clip or go back to previous clip as listed in the inspector?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.