Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to load movie via WWW

Tags:

unity3d

fmod

I'm trying to load a video via url, but I keep getting the same error. I'm using Unity 5.3 and the example code from http://docs.unity3d.com/ScriptReference/WWW-movie.html (heavily modified because the current example doesn't compile).

using UnityEngine;
using System.Collections;

// Make sure we have gui texture and audio source
[RequireComponent (typeof(GUITexture))]
[RequireComponent (typeof(AudioSource))]
public class TestMovie : MonoBehaviour {

    string url = "http://www.unity3d.com/webplayers/Movie/sample.ogg";
    WWW www;

    void Start () {
        // Start download
        www = new WWW(url);

        StartCoroutine(PlayMovie());
    }

    IEnumerator PlayMovie(){
        MovieTexture movieTexture = www.movie;

        // Make sure the movie is ready to start before we start playing
        while (!movieTexture.isReadyToPlay){
            yield return 0;
        }

        GUITexture gt = gameObject.GetComponent<GUITexture>();

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

        transform.localScale = Vector3.zero;
        transform.position = new Vector3 (0.5f,0.5f,0f);
//      gt.pixelInset.xMin = -movieTexture.width / 2;
//      gt.pixelInset.xMax = movieTexture.width / 2;
//      gt.pixelInset.yMin = -movieTexture.height / 2;
//      gt.pixelInset.yMax = movieTexture.height / 2;

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

        // Play both movie & sound
        movieTexture.Play();
        aud.Play();

    }

}

I added this as a script to the Main Camera in a new scene, and I get this error:

Error: Cannot create FMOD::Sound instance for resource (null), (An invalid parameter was passed to this function. )
UnityEngine.WWW:get_movie()
<PlayMovie>c__Iterator4:MoveNext() (at Assets/TestMovie.cs:20)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
TestMovie:Start() (at Assets/TestMovie.cs:16)

(Line 20 is MovieTexture movieTexture = www.movie;) I've been working on this for a while now, it's happened on many files and both of my systems.

like image 969
Alex Avatar asked Dec 18 '15 22:12

Alex


1 Answers

I found a solution! I tested the code with unity 5.2.X and 5.3.X.

I dont know why but Unity 3d requires first wait unitl the download is done with isDone == true and after the copy of the texture wait until the isReadyToPlay == true.

The movie file must be OGG Video format some MP4 files doesn't work.

Well. Check the code:

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

public class MovieTextureStream : MonoBehaviour {
    public Text progressGUI;
    public MeshRenderer targetRender = null;
    public AudioSource targetAudio = null;
    public string URLString = "http://unity3d.com/files/docs/sample.ogg";
    MovieTexture loadedTexture;

    IEnumerator Start() {

        if(targetRender ==null) targetRender = GetComponent<MeshRenderer> ();
        if(targetAudio ==null) targetAudio = GetComponent<AudioSource> ();

        WWW www = new WWW (URLString);
        while (www.isDone == false) {
            if(progressGUI !=null) progressGUI.text = "Progresso do video: " + (int)(100.0f * www.progress) + "%";
            yield return 0;
        }

        loadedTexture = www.movie;
        while (loadedTexture.isReadyToPlay == false) {
            yield return 0;
        }
        targetRender.material.mainTexture = loadedTexture;
        targetAudio.clip = loadedTexture.audioClip;
        targetAudio.Play ();
        loadedTexture.Play ();
    }
}
like image 187
Mario Madureira Fontes Avatar answered Oct 24 '22 05:10

Mario Madureira Fontes