无法通过WWW加载电影。

7
我想通过URL加载视频,但一直出现同样的错误。我使用的是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;

我已经做了一段时间,这在许多文件和我的两个系统上都发生了。


1
我已经能够使用Unity 5.2和5.1播放视频。这是最新版本出现的问题。 - Alex
1
你试过等久一点吗?我不知道为什么现在出现这个错误,可能是Unity的bug,但看起来它并不会停止函数的执行。如果你再等一会儿,电影就会正常开始,并且没有声音问题(确保pixelInset被设置为屏幕上可见的部分)。 - Yuri Nudelman
对我来说,它确实停止了执行。在之前的版本上运行时,我有音频并调整了设置以查看视频。 - Alex
你尝试在访问www.movie之前产生yielding www对象了吗?产生yielding www对象将会使得协程停止直到请求的资源下载完成。我不知道电影流的行为会如何,但是我建议你可以试一试。 - Thomas Hilbert
2个回答

2

我找到了一个解决方案!我在Unity 5.2.X和5.3.X中测试了这段代码。

我不知道为什么,但Unity 3D要求首先等待下载完成,使用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 ();
    }
}

我尝试逐字逐句地使用您的代码,但仍然在loadedTexture = www.movie;这一行出现相同的错误 - 错误信息为:Error: Cannot create FMOD::Sound instance for resource (null), (An invalid parameter was passed to this function. ) UnityEngine.WWW:get_movie()。您是否将任何组件附加到与此脚本相同的对象上,或者还有其他我可能遗漏的东西? - Ben Hayward
在Unity 5.3.1中,这个错误实际上是一个警告。如果你编译你的项目,它将完美地工作。也许这只是Unity 3D编辑器的一个bug。 - Mario Madureira Fontes
另一个重要的事情是,您必须将音频源和网格渲染器附加到您正在附加此代码的对象上。 - Mario Madureira Fontes

0

我不会提供使用WWW电影的解决方案,但有一个替代方案可能会对你或其他人有所帮助。 在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 文件系统上下载和写入视频的协程。一旦完成,您就可以加载和播放它。 希望能有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接