0

I have been looking at the WWW.movie documentation and i can seam to get it working.

https://docs.unity3d.com/ScriptReference/WWW-movie.html

The code below is attached to a cube containing both the GUI Texture and Audio Source Components. if anyone can help me to get this working i would be very greatful.

I am using unity 5.5.1 and am creating a VR application.

using UnityEngine;
using System.Collections;

public class TouchMovie1 : MonoBehaviour {
 public string url = "file://C:/Users/blobbymatt/VRLibrary/Videos/video.ogv";

 // Use this for initialization
 void Start () {

     StartCoroutine(loadAndPlay ());
 }



 // Update is called once per frame
 void Update () {

 }

 IEnumerator loadAndPlay() {
     // Start download
     var www = new WWW(url);

     // Make sure the movie is ready to start before we start playing
     var movieTexture = www.movie;
     while (!movieTexture.isReadyToPlay) { 
     yield return null;
         Debug.Log("Loading");
     }



     var gt = GetComponent< GUITexture > ();

     // Initialize gui texture to be 1:1 resolution centered on screen
     gt.texture = movieTexture;


     // Assign clip to audio source
     // Sync playback with audio
     var aud = GetComponent< AudioSource > ();
     aud.clip = movieTexture.audioClip;

     // Play both movie & sound
     movieTexture.Play();
     aud.Play();
     yield return null;
 }

}

blobbymatt
  • 317
  • 1
  • 2
  • 17

1 Answers1

2

You shouldn't be using GUITexture because it obsolete. If you want to display video, do it on the RawImage component with GetComponent<RawImage>().texture = yourMovieTexture;.

If you want to do it on a 3D Model then do it on the MeshRenderer component with GetComponent<MeshRenderer>().material.mainTexture = yourMovieTexture.

If using Unity 5.6 and above:

Replace

www.movie;

with

www.GetMovieTexture();

There is a new API to play video in Unity 5.6. You can see an example here.

With the RawImage component, you can do it with the code below:

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class TouchMovie1 : MonoBehaviour
{
    public string url = "http://techslides.com/demos/sample-videos/small.ogv";
    public RawImage videDisplay;

    // Use this for initialization
    void Start()
    {
        StartCoroutine(loadAndPlay());
    }

    // Update is called once per frame
    void Update()
    {

    }

    IEnumerator loadAndPlay()
    {
        // Start download
        var www = new WWW(url);

        // Make sure the movie is ready to start before we start playing
        var movieTexture = www.movie;

        //Assign the Texture to the RawImage
        videDisplay.texture = movieTexture;

        while (!movieTexture.isReadyToPlay)
        {
            yield return null;
            Debug.Log("Loading");
        }

        // Assign clip to audio source
        // Sync playback with audio
        var aud = GetComponent<AudioSource>();
        aud.clip = movieTexture.audioClip;

        // Play both movie & sound
        movieTexture.Play();
        aud.Play();
        yield return null;
    }
}
Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • 1
    Thank-you so much!!!! i spent the better part of a day trying to work this out :D – blobbymatt Feb 05 '17 at 23:18
  • You are welcome. I even forgot to completely remove `GUITexture` from the code. Just did that. – Programmer Feb 05 '17 at 23:26
  • www.movie says OBSOLETE in the Docs. https://docs.unity3d.com/ScriptReference/WWW-movie.html – omarojo Sep 14 '17 at 22:39
  • 1
    @omarojo Don't use this. `MovieTexture` will be deprecated soon. See the link in my answer that shows new way to play video in Unity. This answer will remain here for those using old Unity version. – Programmer Sep 14 '17 at 22:46
  • You are right, I didnt read properly. On the other hand I found this component : https://docs.unity3d.com/Manual/class-VideoPlayer.html but it doesnt work with my sample videos, it crashes unity entirely: https://upload.wikimedia.org/wikipedia/commons/6/65/Examplevideo.ogv or https://www.w3schools.com/html/mov_bbb.mp4 – omarojo Sep 14 '17 at 23:52
  • *"On the other hand I found this component : docs.unity3d.com/Manual/class-VideoPlayer.html"* I don't think you read right again because that VideoPlayer "component" is what I linked in my answer. Take a moment and read that. If you have a problem with it, you can create new question with your code and mention your Unity version + platform it is crashing on – Programmer Sep 14 '17 at 23:54
  • VideoPlayer has a huge problem on Android. The VideoPlayer.prepare() runs on the main thread! Therefore, it will freeze your app while preparing video. Ugh! I will stick to MovieTexture until that is resolved. – TatiOverflow Sep 21 '17 at 03:22
  • @TatiOverflow Note that you did not answer/do or address anything I said in my [last](https://stackoverflow.com/questions/41144054/using-new-unity-videoplayer-and-videoclip-api-to-play-video?noredirect=1#comment79629011_41144054) comment. Good luck with MovieTexture which doesn't even work on mobile devices. Please, do not continue to spam my [other](https://stackoverflow.com/questions/41144054/using-new-unity-videoplayer-and-videoclip-api-to-play-video?noredirect=1#comment79629011_41144054) Video questions/answers. If you have a question, create a new one. – Programmer Sep 21 '17 at 04:52