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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

WebGL is not playing Video -- CORS

Discussion in 'WebGL' started by katiejenkinswebdev, Jul 26, 2019.

  1. katiejenkinswebdev

    katiejenkinswebdev

    Joined:
    Apr 23, 2019
    Posts:
    2
    Our WebGL build is not playing our Video we have attached to a Video Player Game Object through a URL that points to it's location under Assets --> Streaming Assets. We get a CORS issue when we try to run locally in Chrome, Safari, and Firefox. We STILL get the issue when we run our http-server with the --cors flag on Node.js. Any help is much appreciated!!
     
  2. savely00

    savely00

    Joined:
    Feb 27, 2014
    Posts:
    36
    Hey everyone,
    I am having problem playing video in Unity WebGL build. I am using Unity 2019.2.
    For testing purposes I build an asset bundle from the video clip and put it into the StreamingAssets folder. According to log video is successfully loaded from the bundle but cannot play. I tried latest Chrome and Firefox. Any recommendations or links that could help to solve the problem would be appreciated.

    I am receiving the following error in player.errorReceived: "VideoPlayer cannot play clip : [path to the clip location on windows system]."

    Partial Source:

    var request = loadedBundle.LoadAssetAsync<VideoClip>(videoClipName);
    yield return request;
    var clip = request.asset as VideoClip;

    if (null == clip)
    yield break;

    player.source = UnityEngine.Video.VideoSource.VideoClip;
    player.clip = clip;
    player.Prepare();
     
    Last edited: Aug 22, 2019
  3. katiejenkinswebdev

    katiejenkinswebdev

    Joined:
    Apr 23, 2019
    Posts:
    2
    Sorry to hear you are having the same issue! Just a heads up, our team discovered that there is a bug in Unity 2019 that will block videos. However, we are using 2018 and still have the same issue with WebGL.
     
    savely00 likes this.
  4. Zyrathius

    Zyrathius

    Joined:
    Dec 18, 2012
    Posts:
    20
    I'm having a similar issue as well, but mine is just an audio clip and I am trying to load using UnityWebRequest. I am however loading it from the streaming assets folder as well. I do get mine to play in a locally ran version of Firefox but not when running it from the web. If I figure mine out, I'll post what I do in hopes that it helps.
     
  5. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    267
    You cannot play audio or video received from a web request inside Unity WebGL in a practical way. I'd recommend looking at how to do this correctly in pure Javascript first, and then working around that. You will need to understand that a `.play()` call must occur inside an onclick handler.
     
  6. jsteinkamp

    jsteinkamp

    Joined:
    Mar 21, 2016
    Posts:
    2
    Hi Add a script like this to your object, don't use the VideoPlayer, the script adds one. Type the name of your video file in the Inspector for the script. Make sure your video is in a StreamingAssets folder.
    Your WebGL video will show up in Microsoft browsers and Mac Safari with some preference changes. Chrome and Firefox not really working.
    using UnityEngine;
    using System.IO;
    using UnityEngine.Video;
    public class video_on_object : MonoBehaviour
    {
    private UnityEngine.Video.VideoPlayer videoPlayer;
    public string videofile;
    public GameObject this_object_cam;
    void Start()
    {
    GameObject cam = this.gameObject;
    videoPlayer = cam.AddComponent<UnityEngine.Video.VideoPlayer>();
    // Obtain the location of the video clip.
    videoPlayer.url = Path.Combine(Application.streamingAssetsPath, videofile);
    videoPlayer.isLooping = true;
    videoPlayer.Play();
    }
    }
     
  7. SeanWebber

    SeanWebber

    Joined:
    Jun 17, 2020
    Posts:
    6
    Sorry I'm an absolute noob with coding

    Which bits of this code need editing with my project specific names?
     
  8. melisahadimoglu

    melisahadimoglu

    Joined:
    May 14, 2020
    Posts:
    1
    I have used this solution with Unity 2019.4 and it indeed works as promised on Microsoft Edge. Thanks. Although I was wondering if anyone here managed to make it work on mainstream browsers such as Chrome? It is quite incredible to me how complicated this is for such a basic feature. I feel like I'm missing something. Has Unity officially made an announcement/solution about this bug yet?
     
  9. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    241
    Play a video from Application.streamingAssetsPath work for all browers here...
    But i get a black image without streaming. Makes no sense to me. (with and without transcoding)
    (Unity 2020.1. 6/7/8)
     
  10. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    944
    It is a known issue that video does not work if it is embedded to the build. We are considering removing this support altogether, because streaming video on the web is a much better solution to preserve fast page startup times anyways.
     
  11. yurikemch

    yurikemch

    Joined:
    Feb 16, 2020
    Posts:
    6
    Hi,

    I used this code, and attached it to a sphere.
    Then I added the video to the StreamingAssets folder.
    But it still doesn't work in my WebGL build.
    (it became all white like the attached screenshot)

    Other info:
    Unity build: 2019.4.13
    My video has alpha value, and it's a .webm file.

    Did I miss anything?

    Best regards!
     

    Attached Files:

  12. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    494
  13. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,796
    You can use the Moesif CORS browser extension for Chrome to fix this.
    Just install it, then enable the CORS option.

    Make sure to turn it off again when you're finished, otherwise some of your other websites you browse will have unintended behaviour!