Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Webgl build won't play Video.

Discussion in 'Web' started by JzKonain, Feb 24, 2019.

  1. JzKonain

    JzKonain

    Joined:
    Apr 13, 2017
    Posts:
    4
    I have used video player to play video, everything works fine in unity but when I build it and run on firefox or any other browser,the video won't play. I have tried different settings and different video formats but nothing seems to work. I don't get any errors.
     
  2. JzKonain

    JzKonain

    Joined:
    Apr 13, 2017
    Posts:
    4
    After Lots of effort and internet research found a solution in the comments.

    In editor create a folder with the name StreamingAssets and put your video file into then (the video format I uses is .mp4);

    script:
    Create a VideoPlayer component and assign in a plan case needs video in scene or camera if you need video full screen;
    Use this command to combine the streaming assets path with the video file name and extension:
    videoPlayer.url = System.IO.Path.Combine (Application.streamingAssetsPath,"myFile.mp4");

    and, call play mode:
    videoPlayer.Play();
     
  3. HeatherK

    HeatherK

    Joined:
    Nov 13, 2019
    Posts:
    12
    Hello, JzKonain, Where do you add this command?
     
  4. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    242
    System.IO don't work in WebGL. You have to do something like this:
    Code (CSharp):
    1. public VideoPlayer _videoPlayer = null; //your videoplayer component
    2. if (_videoPlayer )
    3. {
    4.   _videoPlayer.url = Application.streamingAssetsPath + "/" + "myvideo.mp4";
    5.   _videoPlayer.Prepare();
    6.   _videoPlayer.Play();
    7. }
    And the VideoPlayer source should be URL instead of VideoClip
     
    conordeasy1988 and Xirantics like this.
  5. conordeasy1988

    conordeasy1988

    Joined:
    Mar 15, 2022
    Posts:
    2
    bro where do you add this command ?
     
  6. conordeasy1988

    conordeasy1988

    Joined:
    Mar 15, 2022
    Posts:
    2
    is this an extra script for the Video Player game object ?
     
  7. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    242
    Here is a script ready to use on a empty GameObject in a Canvas (UI).
    You have to assign a RenderTexture and add the filenames.

    The files have to be in the "StreamingAssets" folder!
    https://docs.unity3d.com/Manual/StreamingAssets.html

    StreamingExample.cs
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEngine.Video;
    5.  
    6. [RequireComponent(typeof(RawImage))]
    7. [RequireComponent(typeof(CanvasGroup))]
    8. [RequireComponent(typeof(VideoPlayer))]
    9. public class StreamingExample : MonoBehaviour
    10. {
    11.     public RenderTexture _renderTexture = null; // target texture for the video
    12.     public List<string> _videoFileNames = new(); // your video filenames (myvideo.mp4 e.g.)
    13.     private VideoPlayer _videoPlayer = null;
    14.     private CanvasGroup _canvasGroup = null;
    15.     private RawImage _rawImage = null;
    16.  
    17.     private void Start()
    18.     {
    19. #if UNITY_EDITOR
    20.         // Check files
    21.         foreach (string videoFilename in _videoFileNames)
    22.         {
    23.             string fullpath = Application.streamingAssetsPath + "/" + videoFilename;
    24.             if (!System.IO.File.Exists(fullpath))
    25.             {
    26.                 Debug.LogWarning("File not found: " + fullpath);
    27.                 return;
    28.             }
    29.         }
    30. #endif
    31.         // Set up the videoplayer...
    32.         _videoPlayer = GetComponent<VideoPlayer>();
    33.         _videoPlayer.loopPointReached += OnEndReached;
    34.         _videoPlayer.prepareCompleted += OnPrepareCompleted;
    35.         _videoPlayer.playOnAwake = false;
    36.         _videoPlayer.source = VideoSource.Url;
    37.         _videoPlayer.renderMode = VideoRenderMode.RenderTexture;
    38.         _videoPlayer.targetTexture = _renderTexture;
    39.  
    40.         // Add the target texture to the rawimage component
    41.         _rawImage = GetComponent<RawImage>();
    42.         _rawImage.texture = _videoPlayer.targetTexture;
    43.  
    44.         // Canvasgroup component (for fade in or out... todo...)
    45.         _canvasGroup = GetComponent<CanvasGroup>();
    46.         _canvasGroup.alpha = 1f;
    47.  
    48.         // Load the first video file on start...
    49.         if (_videoFileNames.Count > 0) LoadVideo(0);
    50.         else Debug.Log("No files available");
    51.     }
    52.  
    53.     private void LoadVideo(int fileIndex)
    54.     {
    55.         _videoPlayer.url = Application.streamingAssetsPath + "/" + _videoFileNames[fileIndex];
    56.         _videoPlayer.Prepare();
    57.     }
    58.  
    59.     private void OnPrepareCompleted(VideoPlayer vp)
    60.     {
    61.         vp.Play();
    62.     }
    63.  
    64.     private void OnEndReached(VideoPlayer vp)
    65.     {
    66.         _videoPlayer.targetTexture.Release();
    67.         _videoPlayer.Stop();
    68.     }
    69. }
     
    Last edited: Oct 19, 2022
    Muuuuuuu_Eva likes this.