Using New Unity Videoplayer and Videoclip API to Play Video

Using new Unity VideoPlayer and VideoClip API to play video

Found the problem. Below is the FIXED code that plays Video and Audio:

//Raw Image to Show Video Images [Assign from the Editor]
public RawImage image;
//Video To Play [Assign from the Editor]
public VideoClip videoToPlay;

private VideoPlayer videoPlayer;
private VideoSource videoSource;

//Audio
private AudioSource audioSource;

// Use this for initialization
void Start()
{
Application.runInBackground = true;
StartCoroutine(playVideo());
}

IEnumerator playVideo()
{
//Add VideoPlayer to the GameObject
videoPlayer = gameObject.AddComponent<VideoPlayer>();

//Add AudioSource
audioSource = gameObject.AddComponent<AudioSource>();

//Disable Play on Awake for both Video and Audio
videoPlayer.playOnAwake = false;
audioSource.playOnAwake = false;

//We want to play from video clip not from url
videoPlayer.source = VideoSource.VideoClip;

//Set Audio Output to AudioSource
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

//Assign the Audio from Video to AudioSource to be played
videoPlayer.EnableAudioTrack(0, true);
videoPlayer.SetTargetAudioSource(0, audioSource);

//Set video To Play then prepare Audio to prevent Buffering
videoPlayer.clip = videoToPlay;
videoPlayer.Prepare();

//Wait until video is prepared
while (!videoPlayer.isPrepared)
{
Debug.Log("Preparing Video");
yield return null;
}

Debug.Log("Done Preparing Video");

//Assign the Texture from Video to RawImage to be displayed
image.texture = videoPlayer.texture;

//Play Video
videoPlayer.Play();

//Play Sound
audioSource.Play();

Debug.Log("Playing Video");
while (videoPlayer.isPlaying)
{
Debug.LogWarning("Video Time: " + Mathf.FloorToInt((float)videoPlayer.time));
yield return null;
}

Debug.Log("Done Playing Video");
}

Why Audio was not playing:

//Set Audio Output to AudioSource
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

//Assign the Audio from Video to AudioSource to be played
videoPlayer.EnableAudioTrack(0, true);
videoPlayer.SetTargetAudioSource(0, audioSource);

must be called before videoPlayer.Prepare(); not after it. This is took hours of experiment to find this this was the problem I was having.


Stuck at "Preparing Video"?

Wait 5 seconds after videoPlayer.Prepare(); is called then exit the while loop.

Replace:

while (!videoPlayer.isPrepared)
{
Debug.Log("Preparing Video");
yield return null;
}

with:

//Wait until video is prepared
WaitForSeconds waitTime = new WaitForSeconds(5);
while (!videoPlayer.isPrepared)
{
Debug.Log("Preparing Video");
//Prepare/Wait for 5 sceonds only
yield return waitTime;
//Break out of the while loop after 5 seconds wait
break;
}

This should work but you may experience buffering when the video starts playing. While using this temporary fix, my suggestion is to file for bug with the title of "videoPlayer.isPrepared always true" because this is a bug.

Some people also fixed it by changing:

videoPlayer.playOnAwake = false; 
audioSource.playOnAwake = false;

to

videoPlayer.playOnAwake = true; 
audioSource.playOnAwake = true;

Play Video From URL:

Replace:

//We want to play from video clip not from url
videoPlayer.source = VideoSource.VideoClip;

with:

//We want to play from url
videoPlayer.source = VideoSource.Url;
videoPlayer.url = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";

then Remove:

public VideoClip videoToPlay; and videoPlayer.clip = videoToPlay; as these are not needed anymore.

Play Video From StreamingAssets folder:

string url = "file://" + Application.streamingAssetsPath + "/" + "VideoName.mp4";

if !UNITY_EDITOR && UNITY_ANDROID
url = Application.streamingAssetsPath + "/" + "VideoName.mp4";
#endif

//We want to play from url
videoPlayer.source = VideoSource.Url;
videoPlayer.url = url;

All supported video formats:

  • ogv
  • vp8
  • webm
  • mov
  • dv
  • mp4
  • m4v
  • mpg
  • mpeg

Extra supported video formats on Windows:

  • avi
  • asf
  • wmf

Some of these formats don't work on some platforms. See this post for more information on supported video formats.

UnityEngine.Videoplayer not rendering video on IOS Devices

So the problem was, H264 and H265 Codecs are not supported for some reasons in IOS, You have to convert all your videos to VP8 Codec in unity, and it will render fine in ios devices.
Change this codec on your video in unity

And Voila, it should work fine now.

Unity: Assigning a new clip to video player changes ALL video player clips in my current scene

I fixed the problem by myself:

The reason for the behaviour was not, that all the clips got reassigned. (All video players still had their individual video resource)
But instead, I had one single render texture assigned to the video players. Of course that wont work, as all video players will render their frames onto this one render texture.

The solution instantiates a custom render texture based on the size of the video panel for each individual video player:

        public virtual void SetVideoResource(string fileName)
{
videoName = fileName;
textPanel.SetActive(false);
videoPanel.SetActive(true);
float parentWidth = videoPanel.GetComponent<RectTransform>().rect.width;
float parentHeight = videoPanel.GetComponent<RectTransform>().rect.height;
videoPlayer = videoPanel.GetComponentInChildren<VideoPlayer>();
CustomRenderTexture targetTexture = new CustomRenderTexture((int)(parentWidth*0.90f), (int)(parentHeight * 0.90f), RenderTextureFormat.ARGB32);
videoPanel.GetComponentInChildren<RawImage>().texture = targetTexture;
videoPlayer.targetTexture = targetTexture;
videoPlayer.clip = (VideoClip)Resources.Load("Videos/" + fileName);
}



Related Topics



Leave a reply



Submit