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

Video Unity video player renders it black

Discussion in 'Audio & Video' started by josenunes, Jul 5, 2018.

  1. josenunes

    josenunes

    Joined:
    Jun 16, 2017
    Posts:
    7
    Hello,

    I'm using the native video player in Unity 2018, using a Render texture created at runtime and a raw image.
    Everything is fine in the editor and on any standalone build. However, in WebGL the video is rendered black. The audio is audible though. I've recently migrated from Unity 5.6 to Unity 2018 - it was fine in the previous version.

    Has anyone had the same problem or is there anything new in Unity that has changed the way videos work in webgl?
    Thanks in advance
     
  2. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    596
    Hi,

    Can you playe the video with video player? I cannot do that with a rawImage and Canvas. My code:
    Code (CSharp):
    1. public class StreamVideo2 : MonoBehaviour {
    2.    
    3.     public RawImage image;
    4.     public VideoClip videoToPlay;
    5.     public VideoPlayer videoPlayer;
    6.     public AudioSource audioSource;
    7.  
    8.     void Start () {
    9.         StartCoroutine(PlayVideo());
    10.     }
    11.    
    12.     IEnumerator PlayVideo(){
    13.         videoPlayer.clip = videoToPlay;
    14.         videoPlayer.Prepare();
    15.         WaitForSeconds waitForSeconds = new WaitForSeconds(1);
    16.         while (!videoPlayer.isPrepared){
    17.             yield return waitForSeconds;
    18.             break;
    19.              //yield return null;
    20.         }
    21.        
    22.         image.texture = videoPlayer.texture;
    23.         videoPlayer.Play();
    24.         audioSource.Play();
    25.        
    26.     }
    27.    
    28. }
     
  3. waldgeist

    waldgeist

    Joined:
    May 6, 2017
    Posts:
    370
    I have the same problem. A video simple won't play on a RawImage, not even in the editor. Did you ever find a solution?
     
  4. Virat-Gangurde

    Virat-Gangurde

    Joined:
    Aug 20, 2019
    Posts:
    12
    Hi,

    There is problem for videoplayer with WebGL platform. This is bug and Unity working on this and resolved for versions as issue mentioned in below link,
    https://issuetracker.unity3d.com/is...webgl-builds-when-the-video-is-loaded-via-url

    I have checked in 2019.3.15f1 (recommended in issue list solution) , 2019.4.2f1 (LTS), 2019.3.0f6. But it’s not working.

    Kindly let if you get any work around, me too searching same issue solution.

    Thanks,
    Virat
     
  5. waldgeist

    waldgeist

    Joined:
    May 6, 2017
    Posts:
    370
    I resolved my problem. In my case, it was caused by the fact that I called the VideoPlayer's API *before* the game object I placed it on was activated. This seemed to work (because I could listen to the audio), but the video was not visible. As soon as I placed the video player to another game object to activate it right away, everything worked as expected. It is rather counter-intuitive that it kinda worked (i.e. was playing audio but not video). This made me think that there was another problem with my setup.
     
  6. josenunes

    josenunes

    Joined:
    Jun 16, 2017
    Posts:
    7
    This was about playing the video whereas target render texture and video itself were not prepared.

    Assign targetTexture to an instance of a Render texture (also, be careful here with dimensions and depth), then call, video.Prepare. Make sure you bind prepareCompleted events to the object and play the video just then.
     
    ADNOC_LNG likes this.
  7. Virat-Gangurde

    Virat-Gangurde

    Joined:
    Aug 20, 2019
    Posts:
    12
    The video rendering in WebGL build issue resolved at my end. This work in Unity 2019.4 LTS versions.
    Note:- I have observed that mp4 video file rendered in WebGL build (not wmv). If any have other types of file format status please update here.
     
  8. AR_Tehcnoplus

    AR_Tehcnoplus

    Joined:
    Jan 17, 2022
    Posts:
    15
    Hi

    I have a similar issue with the render texture remaining black but the sound playing. I got some Image targets attached with a prefab of a video player. When the image target is tracked the video needs to be played.

    Code (CSharp):
    1. public class VideoPrefabBehaviour : MonoBehaviour
    2. {
    3.  
    4.  
    5.     private VideoPlayer videoPlayer;
    6.     private VideoClip videoClip;
    7.  
    8.     DefaultObserverEventHandler parentEventObserver;
    9.     private void Awake()
    10.     {
    11.        
    12.         videoPlayer = GetComponentInChildren<VideoPlayer>();
    13.         if (!videoPlayer)
    14.             Debug.LogError("NO video player component found");
    15.         videoClip = videoPlayer.clip;
    16.         parentEventObserver = transform.parent.GetComponent<DefaultObserverEventHandler>();
    17.         parentEventObserver.OnTargetFound.AddListener(OnTargetFound);
    18.     }
    19.  
    20.     private void OnTargetFound()
    21.     {
    22.         videoPlayer.targetTexture.Release();
    23.         videoPlayer.clip = videoClip;
    24.         videoPlayer.Prepare();
    25.         videoPlayer.prepareCompleted += onPreperationComplete;
    26.         //StartCoroutine(WaitPreperationToComplete());
    27.        
    28.     }
    29.  
    30.     void onPreperationComplete(VideoPlayer vPlayer)
    31.     {
    32.         videoPlayer.Play();
    33.     }
    34. }
     
  9. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    It seems this issue was fixed in Unity 2019.4 LTS. You should probably open your own thread with your target OS, Editor version, and so on. Does it happen in your build or only in the Editor? Are you sure OnTargetFound is called?
     
  10. AR_Tehcnoplus

    AR_Tehcnoplus

    Joined:
    Jan 17, 2022
    Posts:
    15
    You are right, it actually renders in the build but it doesn't achieve what I want it to do. I will ask in a different thread.
     
    The_Island likes this.
  11. ADNOC_LNG

    ADNOC_LNG

    Joined:
    Mar 7, 2021
    Posts:
    4
    Another solution, on the same Game object you should move the Raw Image up higher than the video player. Like this when the game object activated then the raw image first will be loaded then the video player will start the video properly.
     
  12. alansin3h

    alansin3h

    Joined:
    Sep 30, 2018
    Posts:
    1
    I had an issue where the 'RawImage' worked fine in the editor, but when building for Android, it showed up black while the video played with audio.

    As a context, I'm making a VR app, using the Oculus Integration SDK and needed to play a series of videos. I followed the steps exactly as shown in the video and to solve my problem, I assigned and created the 'RenderTexture' through code and assigned it to the 'RawImage'. Here's the code:

    [SerializeField] private VideoPlayer videoPlayerObject;
    [SerializeField] private RawImage rawImageObject;
    [SerializeField] private VideoClip videoClip;

    private void Play()
    {
    videoPlayerObject.clip = videoClip;
    RenderTexture renderTexture = new RenderTexture(
    (int)videoPlayerObject.clip.width, (int)videoPlayerObject.clip.height, 0);
    videoPlayerObject.targetTexture = renderTexture;
    rawImageObject.texture = renderTexture;
    videoPlayerObject.Play();
    }
     
  13. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    Do you mean you saw a black frame and then the video? Generally, RenderTexture starts filled with black colour. You can do this to clear the texture.
    Code (CSharp):
    1.     private void ClearRenderTexture()
    2.     {
    3.         RenderTexture rt = RenderTexture.active;
    4.         RenderTexture.active = videoPlayerObject.targetTexture;
    5.         GL.Clear(true, true, Color.clear);
    6.         RenderTexture.active = rt;
    7.     }