【问题标题】:Unable to load movie via WWW无法通过 WWW 加载电影
【发布时间】:2015-12-18 22:14:19
【问题描述】:

我正在尝试通过 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;) 我已经为此工作了一段时间,它发生在许多文件和我的两个系统上。

【问题讨论】:

  • 我已经能够使用 Unity 5.2 和 5.1 播放视频。这是有这个问题的最新版本。
  • 您是否尝试过再等一会?我不知道为什么现在出现这个错误,可能是 Unity 错误,但看起来它并没有停止函数执行。如果您稍等片刻,电影将正常开始,并且不会出现声音问题(确保将pixelInset 设置为屏幕的某个可见部分)。
  • 对我来说,它确实会停止执行。当它在以前的版本上工作时,我有音频和调整设置来观看视频。
  • 您是否在访问 www.movi​​e 之前尝试生成 www 对象?产生一个 www 对象将使协程停止,直到请求的资产下载完成。我不知道电影流的行为会如何,但我会尝试一下。

标签: unity3d fmod


【解决方案1】:

我找到了解决办法!我用统一的 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 ();
    }
}

【讨论】:

  • 我尝试逐字使用您的代码,但在loadedTexture = www.movie; 行上仍然出现相同的错误 - 错误读取:错误:无法为资源创建 FMOD::Sound 实例( null), (向此函数传递了一个无效参数。) UnityEngine.WWW:get_movie()。您是否将任何组件附加到与此脚本相同的对象上,或者我可能缺少的任何其他内容?
  • 在 Unity 5.3.1 中这个错误实际上是一个警告。如果您编译您的项目将完美运行。也许这只是 Unity 3D EDITOR 的一个 bug。
  • 另一个重要的事情是,您必须将音频源和网格渲染附加到要附加此代码的对象。
【解决方案2】:

我不会为 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 的文件系统上下载和写入视频的协程。完成后,您可以加载并播放它。 希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 2016-06-28
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多