Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Video Web GL and video

Discussion in 'Audio & Video' started by yusefkerr, Apr 21, 2020.

  1. yusefkerr

    yusefkerr

    Joined:
    Apr 5, 2011
    Posts:
    179
    I recently built a small webGL project using Unity 2019.2.1 that contains a video.

    The only way I could find to load this video successfully from an online server was to store it in "streaming assets" and load it via a URL in a script. I also had to switch down from Unity 2019.3 because the following script didn't work in that version at all (but it more-or-less works in 2019.2):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Video;
    5.  
    6. public class VideoControl : MonoBehaviour
    7. {
    8.  
    9.     private VideoPlayer videoPlayer;
    10.  
    11.     [SerializeField]
    12.     private bool shouldPlay;
    13.  
    14.     private void Awake()
    15.     {
    16.         videoPlayer = GetComponent<VideoPlayer>();
    17.         videoPlayer.url = System.IO.Path.Combine(Application.streamingAssetsPath, "canaryWharf.mp4");
    18.        
    19.     }
    20.  
    21.     private void Start()
    22.     {
    23.         videoPlayer.Play();
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.        /* if (shouldPlay)
    30.             videoPlayer.Play();
    31.         else
    32.             videoPlayer.Pause();
    33.       */
    34.     }
    35. }
    36.  
    The main problem I have is that the first time I load the project from a server to my PC or android phone, the video doesn't play, it just shows a grey rectangle. If I refresh the page, then the video loads as expected. Is there something obvious in the script preventing it loading correctly first time?

    And is there a way of loading a video from outside the Streaming assets folder, i.e. from a random youtube url? I've tried to use "Source/URL" in the Video Player inspector window, but it didn't seem to work.
     
  2. DominiqueLrx

    DominiqueLrx

    Unity Technologies

    Joined:
    Dec 14, 2016
    Posts:
    256
    Hi!

    There is an race condition issue with WebGL playback on certain browsers - which appears to be a browser bug - for which a fix is being integrated: https://issuetracker.unity3d.com/is...webgl-builds-when-the-video-is-loaded-via-url

    If you open the web browser's debug console and see GL related errors, then it's the same issue. If not please submit a new bug so we can have a look!

    As for playing videos from YouTube, this requires not only being able to read from its source, but also integrate with the business side (authentication, ads, cdn) and this isn't done. So at this point, the VideoPlayer can only play from sources that expose actual video file urls.

    Hope this helps,

    Dominique Leroux
    A/V developer at Unity
     
  3. yusefkerr

    yusefkerr

    Joined:
    Apr 5, 2011
    Posts:
    179
    Hi, thanks for the reply. The most obvious error I'm getting is this "Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. https://goo.gl/xX8pDD", and the link seems to imply that it's stopping my video from running because it Chrome thinks it's an unwanted advert. What would be the best way to solve this without having to put something in the Update() function?

    As for playing videos from youtube, I didn't really mean literally "from youtube", that was my bad explanation. What I want to do is to be able to play video/audio from outside the StreamingAssets folder. This is because (as I understand it) a user would currently have to download all the videos I put in "StreamingAssets" when they open my WebGL project. I want to avoid this so that my initial WebGL project download remains small.
     
  4. DominiqueLrx

    DominiqueLrx

    Unity Technologies

    Joined:
    Dec 14, 2016
    Posts:
    256
    Hi!

    Thanks for the added info: in this case, the next thing to verify is that your browser allows video playback autoplay for the source providing the game. For example, here are some details of what this means for Google Chrome: https://developers.google.com/web/updates/2017/09/autoplay-policy-changes

    The VideoPlayer can play from a url (VideoPlayer.url), which is the only method supported with WebGL. The url can point into StreamingAssets, or any server you want that can serve movie files. For the web browser to accept playing a movie from the server, it has to be appropriately configured for CORS (Cross-Origin ResourceSharing). To test your server's configuration, you can use www.test-cors.org, as suggested in https://forum.unity.com/threads/videoplayer-webgl-cant-play-movie-error.456378/#post-2960623

    Note that when your build contains videos in the StreamingAssets folder, these also stay on the server and don't increase the size of what is getting downloaded into the web browser. The video download only happens when you actually try to play (or access) the video file using a URL that you construct with Application.streamingAssetsPath and appending the name of the video to use.

    Hope this helps!

    Dominique Leroux
    A/V developer at Unity
     
    JoRangers likes this.
  5. Nadoc_NewLedge

    Nadoc_NewLedge

    Joined:
    Nov 26, 2020
    Posts:
    19
    Hello
    I use Amazon S3 and I did apply CORS to the repository, however, when I start the project without interacting with the browser preior to loading i get this error.
    It happened when I added an intro screen that plays a video on start.