【转】unity www读取本地视频文件和外部视频文件 播放视频动画和视频声音

时间:2024-03-12 09:26:38
using System.Runtime.InteropServices;
using UnityEngine;
using System.Collections;
using Assets;

public class PlayVideo : MonoBehaviour
{
    public string url;
    public MovieTexture movieTexture;
    public WWW www;
    void Start()
    {
        url = "file:///D:/video/sample.ogg";//本地视频
      //  url = "http://www.unity3d.com/webplayers/Movie/sample.ogg ";//外部视频
        StartCoroutine(DownLoadMovie());
     
    }

    private IEnumerator DownLoadMovie()
    {
        www = new WWW(url);
        Debug.Log(Time.time);  
        movieTexture = www.movie;
        audio.clip = movieTexture.audioClip;
        renderer.material.mainTexture = movieTexture;
        movieTexture.loop = true;
        yield return www;
        

    }

    void Update()
    {

          Debug.Log("movieTexture.isReadyToPlay" + "     " + movieTexture.isReadyToPlay);
          Debug.Log("www.isDone" + "     " + www.isDone);
          Debug.Log("movieTexture.duration" + "     " + movieTexture.duration);
          Debug.Log("movieTexture.isPlaying" +"      "+ movieTexture.isPlaying);
    }

    void OnGUI()
    {
        if (GUILayout.Button("播放/继续"))
        {
            //播放/继续播放视频  
            if (!movieTexture.isPlaying)
            {
                movieTexture.Play();
                audio.Play();
            }
        }

        if (GUILayout.Button("暂停播放"))
        {
            //暂停播放  
            movieTexture.Pause();
            audio.Pause();
        }

        if (GUILayout.Button("停止播放"))
        {
            //停止播放  
            movieTexture.Stop();
            audio.Stop();
        }  
    }  
}