Search Unity

Question Video playback using url index with multiple videos for WEBGL

Discussion in 'Audio & Video' started by siennakei, Jun 22, 2022.

  1. siennakei

    siennakei

    Joined:
    Jan 13, 2021
    Posts:
    13
    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 {

    1. public VideoClip.[] videoclips;
    2. private VideoPlayer videoplayer;
    3. private int videoClipIndex;
    4. public string url;
    5. private void Awake()
    6. {
    7. videoplayer = GetComponent<VideoPlayer>();
    8. }
    9. void Start()
    10. {
    11. videoplayer.clip = videoclips[0];
    12. }
    13. public void PlayNext()
    14. {
    15. videoClipIndex++;
    16. if (videoClipIndex >= videoclips.Length)
    17. {
    18. videoClipIndex = videoClipIndex % videoclips.Length;
    19. }
    20. videoplayer.clip = videoclips[videoClipIndex];
    21. videoplayer.Play();
    22. }
    23. public void PlayPrevious()
    24. {
    25. videoClipIndex--;
    26. if (videoClipIndex >= videoclips.Length)
    27. {
    28. videoClipIndex = videoClipIndex % videoclips.Length;
    29. }
    30. videoplayer.clip = videoclips[videoClipIndex];
    31. videoplayer.Play();
    32. }
    33. public void PlayPause()
    34. {
    35. if (videoplayer.isPlaying)
    36. {
    37. videoplayer.Pause();
    38. }
    39. else
    40. {
    41. videoplayer.Play();
    42. }
    43. }
    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
     

    Attached Files:

  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    @siennakei Have you got a single URL, without an array, working first? Please share working code from a simpler example, then build on success.
     
  3. siennakei

    siennakei

    Joined:
    Jan 13, 2021
    Posts:
    13
    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.
    upload_2022-6-22_15-49-10.png
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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
     
  5. siennakei

    siennakei

    Joined:
    Jan 13, 2021
    Posts:
    13
    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.
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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.
     
  7. siennakei

    siennakei

    Joined:
    Jan 13, 2021
    Posts:
    13
    I don't have a script that uses URL's as the source. My existing code uses video clips as the array:
    1. public VideoClip.[] videoclips;
    2. private VideoPlayer videoplayer;
    3. private int videoClipIndex;
    4. private void Awake()
    5. {
    6. videoplayer = GetComponent<VideoPlayer>();
    7. }
    8. void Start()
    9. {
    10. videoplayer.clip = videoclips[0];
    11. }
    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:
    https://siennakei.github.io/ATVideos/ChronoTrigger.mp4

    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 :D

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

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    @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:


    Code (CSharp):
    1. myPlayer.url = "https://siennakei.github.io/ATVideos/ChronoTrigger.mp4";
    2. myPlayer.Play();
     
  9. siennakei

    siennakei

    Joined:
    Jan 13, 2021
    Posts:
    13
    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?
     
  10. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    @siennakei You just need an array of strings.

    string[] myURLs = {"https://...", "https://...", "etc"};

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

    siennakei

    Joined:
    Jan 13, 2021
    Posts:
    13
    OK thanks for your help.
     
    JeffDUnity3D likes this.