2017-08-25 173 views
0

我想從網址下載視頻(用於測試的兔子視頻),然後使用視頻播放器組件播放它。無法播放使用VideoPlayer在Unity下載的視頻剪輯

我這裏的問題是,如果我通過一個預先下載的片段或URL在編輯模式下的視頻播放器可播放影片沒有任何問題,但是當我嘗試動態添加VideoPlayer組件到我的遊戲對象,並指定視頻剪輯到視頻播放器,它從不播放視頻。在播放模式下,剪輯選項在VideoPlayer組件中始終顯示爲空。當通過腳本完成時,添加url也不起作用。

這是我的腳本,下載視頻並將其分配給錄像機。

public class DownloadScript : MonoBehaviour 
{ 



    private WWW Url; 

    private VideoPlayer Player; 
    public VideoClip clip; 

    public float test; 
    // Use this for initialization 
    void Start() 
    { 
     this.gameObject.AddComponent<VideoPlayer>(); 
     Player = this.gameObject.GetComponent<VideoPlayer>(); 
     _Download(); 
     /*Player.source=VideoSource.VideoClip; 
     Player.clip = clip; 
     Player.Prepare();*/ 


     // Player.url = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"; 


    } 

    // Update is called once per frame 
    void Update() { 

     if (!Player.isPlaying && Player.isPrepared) 
     { 
      Player.Play(); 

     } 
    } 
    private IEnumerator WaitForDownload(string sURL) 
    { 
     Url = new WWW(sURL); 
     //WWW www = new WWW(sURL); 
     Debug.Log(Url.bytesDownloaded); 
     yield return Url; 
     if (Url.isDone) 
     { 
      Debug.Log("Player ready"); 
      Invoke("PlayVideo",20f); 
     } 
     else 
     { 
      Debug.Log(Url.bytesDownloaded); 
     } 

     yield return Url; 

    } 
    private void _Download() 
    { 
     try 
     { 
      StartCoroutine(WaitForDownload("http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4")); 
     // return Url.bytes; 
     } 
     catch(Exception e) 
     { 
      Debug.Log(e.ToString()); 
     } 


    } 

    public void PlayVideo() 
    { 
     Debug.Log("url download complete"); 
     File.WriteAllBytes(Application.dataPath + "/Resources"+"/bunny.mp4", Url.bytes); 


     clip =(VideoClip) Resources.Load("bunny.mp4"); 
     Player.source=VideoSource.VideoClip; 
     Player.clip = clip; 
     test = 5.0f; 
     // Player.url = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"; 
     if (Player.isPrepared) 
     { 
      Player.Play(); 
     } 
    } 
} 

這是檢查器面板在運行時的屏幕截圖。

Inspector

我在腳本中添加了一個測試變量來驗證如果有什麼在運行時獲得或沒有更新。因此,檢查器面板中的測試變量的值會更新,但視頻剪輯值不會更新,這就是我的視頻無法播放的原因。

任何解決方案?

回答

0

所以我有同樣的問題。剛剛解決。

CODE

private void Start() 
{ 
    StartCoroutine(this.loadVideoFromThisURL(videoUrl)); 
} 

[SerializeField] 
internal UnityEngine.Video.VideoPlayer myVideoPlayer; 
string videoUrl ="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"; 
private IEnumerator loadVideoFromThisURL(string _url) 
{ 
    UnityWebRequest _videoRequest = UnityWebRequest.Get (_url); 

    yield return _videoRequest.SendWebRequest(); 

    if (_videoRequest.isDone == false || _videoRequest.error != null) 
    { Debug.Log ("Request = " + _videoRequest.error);} 

    Debug.Log ("Video Done - " + _videoRequest.isDone); 

    byte[] _videoBytes = _videoRequest.downloadHandler.data; 

    string _pathToFile = Path.Combine (Application.persistentDataPath, "movie.mp4"); 
    File.WriteAllBytes (_pathToFile, _videoBytes); 
    Debug.Log (_pathToFile); 
    StartCoroutine(this.playThisURLInVideo (_pathToFile)); 
    yield return null; 
} 


private IEnumerator playThisURLInVideo(string _url) 
{ 
    myVideoPlayer.source = UnityEngine.Video.VideoSource.Url; 
    myVideoPlayer.url = _url; 
    myVideoPlayer.Prepare(); 

    while (myVideoPlayer.isPrepared == false) 
    { yield return null;} 

    Debug.Log ("Video should play"); 
    myVideoPlayer.Play(); 
} 

SOLUTION

從你的代碼,我看到你的字節保存到資源文件夾。這不是必需的。因此,您不必使用Resources.Load加載它。

只傳入絕對文件路徑。我已經在iOS和Mac OS上測試過。有用。