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?