Video playback using url index with multiple videos for WEBGL

I’m trying to create a video player that can play and skip tracks using an index of clips. My current code creates fields for the index to drop in video clips. Originally this worked when using video clips, but now I need to access the clips via URL. This is the code I have. Now, however, I need to switch the index to one that supports URLS.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Video;

public class VideoPlayback : MonoBehaviour {

  • public VideoClip.[ ] videoclips;
  • private VideoPlayer videoplayer;
  • private int videoClipIndex;
  • public string url;
  • private void Awake()
  • {
  • videoplayer = GetComponent();
  • }
  • void Start()
  • {
    • videoplayer.clip = videoclips[0];
    • }
  • public void PlayNext()
  • {
  • videoClipIndex++;
  • if (videoClipIndex >= videoclips.Length)
  • {
  • videoClipIndex = videoClipIndex % videoclips.Length;
  • }
  • videoplayer.clip = videoclips[videoClipIndex];
  • videoplayer.Play();
  • }
  • public void PlayPrevious()
  • {
  • videoClipIndex–;
  • if (videoClipIndex >= videoclips.Length)
  • {
  • videoClipIndex = videoClipIndex % videoclips.Length;
  • }
  • videoplayer.clip = videoclips[videoClipIndex];
  • videoplayer.Play();
  • }
  • public void PlayPause()
  • {
  • if (videoplayer.isPlaying)
  • {
  • videoplayer.Pause();
  • }
  • else
  • {
  • videoplayer.Play();
  • }
  • }

If someone can suggest a way to convert the index elements to allow URLS to be added rather than actual clips, that would be most helpful.

Thanks

8225061--1074351--VideoClips.JPG

@siennakei Have you got a single URL, without an array, working first? Please share working code from a simpler example, then build on success.

Hey @JeffDUnity3D , thanks for replying.

I have the videos uploaded on Github, Usually it would work when switching the source to URL and adding the URL in the URL field in the built-in video player, but as it’s an index of URLs I’m looking to create rather than having just one single video, i’m not sure how to go about this.
8225091--1074354--upload_2022-6-22_15-49-10.png

Correct, don’t use an array yet. Just create a separate example that simply plays a single video URL. What is the URL? And you said “usually it would work”, please provide the code that was working for you. Once you get a simple example working, then implement an array of URLs https://docs.unity3d.com/Manual/Video.html

The creating an array of URLs is what I’m struggling with, as I haven’t ever done this. Like I said, previously it worked within the built-in video player where you just add in the URL as the source, so there was no need for me to write any code myself which is why I do not have any code to show. But this was for a single video. Now i have multiple videos on github that I’d like to index:
https://siennakei.github.io/ATVideos/
This is where they are stored.
Writing code for an array of URLs is where i need help with.

Do you have an example with a SINGLE URL working via script, without an array? If you don’t have that working, an array won’t work either obviously. My suggestion is to get the basics working first. The URL you provided does not launch a video for me, please provide the exact URL you are using. Does it work when you enter it in the Inspector? Great! Now do the same thing via script with a single video. THEN work on the array problem.

I don’t have a script that uses URL’s as the source. My existing code uses video clips as the array:

  • public VideoClip.[ ] videoclips;
  • private VideoPlayer videoplayer;
  • private int videoClipIndex;
  • private void Awake()
  • {
  • videoplayer = GetComponent();
  • }
  • void Start()
  • {
  • videoplayer.clip = videoclips[0];
  • }

I just thought there might be a simple way to convert this existing code from video clips to URL’s as the source.
The link i provided was for the folder containing all videos, but for example here is a single video:

So I already have the video player set up with indexing, i just need to change the source from video clips to URLs in the above code. One single URL works fine in the built-in player, but for many, obviously i need to edit the code I have.

You say if it works in the inspector, then great, do the same in script, but this is what I’m struggling with :smile:

I have tried changing the videoPlayer to VideoPlayer.Url but i keep getting an error message.

@siennakei Yes, if you don’t have a single URL working, then an array of them would not either, was my point. This code worked for me:

myPlayer.url = "https://siennakei.github.io/ATVideos/ChronoTrigger.mp4";
myPlayer.Play();

Thank you, yes I have managed to get the player to work with URL

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;

public class VideoPlayerURL : MonoBehaviour
{
public VideoPlayer videoPlayer;
public string videoUrl = “https://siennakei.github.io/ATVideos/ChronoTrigger.mp4”;

// Start is called before the first frame update
void Start()
{
videoPlayer.url = videoUrl;
}

// Update is called once per frame
void Update()
{

}

public void PlayPause()
{
if (videoPlayer.isPlaying)
{
videoPlayer.Pause();
}

else
{
videoPlayer.Play();
}
}
}

Now I need to add an array. I’m not sure how to do this, could you please help with how to add the fields to add different URLs if possible?

@siennakei You just need an array of strings.

string[ ] myURLs = {“https://…”, “https://…”, “etc”};

myPlayer.url = myURLs[0]; //etc

OK thanks for your help.

1 Like