-2

Unity introduced new functionality in 5.6.0b1 release and now it is possilbe to play videos on iPhone and Android.

I managed to make it work on iphone and Windows, but on Android it is stuck on "Preparing" the video.

This is the code I used

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 video To Play then prepare Audio to prevent Buffering
    // videoPlayer.clip = videoToPlay;
    videoPlayer.url = Application.streamingAssetsPath + "/TestVideo.MP4";
    videoPlayer.source = VideoSource.Url;
    VideoClip clip = new 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);

    videoPlayer.Prepare();

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

    Debug.Log("Done Preparing Video");

    //Play Video
    videoPlayer.Play();

    //Play Sound
    audioSource.Play();

    Debug.Log("Playing Video");
    while (videoPlayer.isPlaying)
    {
         yield return null;
    }

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

P.S.

In Unity documentation I found that when using android, I need to use WWW class to retrieve the files. The problem is that only MovieTexture is available and no VideoClip from WWW object. Is there a way to convert it?

1 Answers1

0

To your "P.S." question, you can retrieve the video by using WWW www = new WWW(url) instead of LoadFromCacheOrDownload(), then loading www.bytes to a bytes array, writing it to disk (File.WriteAllBytes()) and finally setting the VideoPlayer.url to the one you just wrote.

To the main question.. I'm facing the same problem, it keeps preparing (on android) and never end.. As a workaround, you can just skip it but then the video may run slowly.

  • "As a workaround, you can just skip it but then the video may run slowly." Have you done this? Did it work or are you just speculating? – Programmer Dec 16 '16 at 17:26
  • Yes I did. In my case the video did ran slowly. What I haven't done is try with a short or low quality video which may not run slowly like mine. – Jonathan Prust Vernizzi Dec 16 '16 at 17:55
  • 1
    That's a bug. I filed for about 5 bug reports about this new VideoPlayer on Android. I suggest you do so too so that these will be fixed in the next release. From your Editor, go to Help->Report a Bug... Make sure to tell them your android model number and Android version. As for the `WWW` you mentioned, that's not how VideoPlayer is supposed to work and that should not even be done. It is suppose to play any valid video you from the StreamingAssets folder. Again, that's a bug. I will update the duplicated post when there is a fix. – Programmer Dec 16 '16 at 19:48