无法通过WWW加载电影

时间:2015-12-18 22:14:19

标签: unity3d fmod

我试图通过网址加载视频,但我一直遇到同样的错误。我使用Unity 5.3和http://docs.unity3d.com/ScriptReference/WWW-movie.html中的示例代码(经过大量修改,因为当前示例没有编译)。

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();

    }

}

我在新场景中将此脚本添加为主摄像头,我收到此错误:

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)

(第20行是MovieTexture movieTexture = www.movie;) 我已经在这方面工作了一段时间,它发生在许多文件和我的两个系统上。

2 个答案:

答案 0 :(得分:2)

我找到了解决方案!我使用unity 5.2.X和5.3.X测试代码。

我不知道为什么但是Unity 3d要求首先等待unitl下载完成isDone == true并且纹理副本等到isReadyToPlay == true

电影文件必须是OGG视频格式,某些MP4文件不起作用。

好。检查代码:

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 ();
    }
}

答案 1 :(得分:0)

我无法通过WWW.movi​​e提供解决方案,但是可以帮助您或其他任何人的替代解决方案。 在iPhone上,我们没有找到从服务器流式传输视频的解决方案,我们决定在阅读之前下载视频,具体如何:

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();

这是用于在iphone文件系统上下载和编写视频的协程。完成后,您可以加载并播放它。 希望它有所帮助。