7

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.

Alex
  • 535
  • 6
  • 16
  • 1
    I've been able to play video using Unity 5.2 and 5.1. It's the latest version with this issue. – Alex Dec 19 '15 at 01:12
  • 1
    Did you try just to wait a little longer? I don't know why this error appears now, possibly Unity bug, but looks like it does not stop function execution. If you wait a little longer, the movie will start normally and without sound problems (make sure `pixelInset` is set to some visible portion of the screen). – Yuri Nudelman Dec 19 '15 at 07:08
  • For me, it does stop execution. When it was working on the previous versions, I had audio and tweaked settings to see the video. – Alex Dec 19 '15 at 21:53
  • Did you try yielding the www object before accessing www.movie? Yielding a www object will make the Coroutine stop until the download of the requested asset has been completed. How the behaviour with movie streams will be I do not know, but I would try it. – Thomas Hilbert Dec 23 '15 at 16:55

2 Answers2

2

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 ();
    }
}
  • I tried using your code word-for-word, and I still get the same error appearing on the line `loadedTexture = www.movie;` - the error reading: Error: Cannot create FMOD::Sound instance for resource (null), (An invalid parameter was passed to this function. ) UnityEngine.WWW:get_movie(). Do you have any components attached to the same object as this script, or anything else I might be missing? – Ben Hayward Feb 01 '16 at 13:04
  • In the Unity 5.3.1 This error in fact is a WARNING. If you compile your project will works perfectly. Maybe this is only a Unity 3D EDITOR bugs. – Mario Madureira Fontes Feb 02 '16 at 14:50
  • An another important thing is, you must attach an Audio Source and Mesh Render to the object you are attaching this code. – Mario Madureira Fontes Feb 02 '16 at 14:58
0

I won't provide a solution with the WWW.movie, but an alternative solution that may help ou or anyone else. On IPhone, we didn't find a solution to stream video from a server, we decided to download the video before reading it, here's how:

string docPath = Application.persistentDataPath + "/" + id + ".mp4";
if (!System.IO.File.Exists(docPath))
{
  WWW www = new WWW(videouUrl);
  while(!www.isDone)
  {
    yield return new WaitForSeconds(1);
    Loading.Instance.Message = "Downloading video : " + (int)(www.progress * 100) + "%";
    if (!string.IsNullOrEmpty(www.error))
      Debug.Log(www.error);
  }
  byte[] data = www.bytes;
  System.IO.File.WriteAllBytes(docPath, data);
}

mediaPlayer.Load(docPath);
onVideoReady();

mediaPlayer.Play();

This is the coroutine used to download and write the video on the iphone's file system. Once it's done, you can load and play it. Hope it helps.

Stud
  • 521
  • 4
  • 8