2017-08-25 84 views
0

URLからビデオ(バニービデオ)をダウンロードし、ビデオプレーヤーコンポーネントを使用して再生しようとしています。UnityのVideoPlayerを使用してURLからダウンロードしたビデオクリップを再生できません

私がここで問題になっているのは、エディタモードでビデオプレーヤーにダウンロード済みのクリップまたはURLを渡すと、問題なくビデオが再生されますが、VideoPlayerコンポーネントを自分のゲームオブジェクトに動的に追加して、ビデオプレーヤーには、決してビデオを再生しません。クリップオプションは、再生モードで常にVideoPlayerコンポーネントにnullを表示します。また、スクリプトを使って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フォルダに、あなたのバイトを保存することを参照してください。それは必要ではありません。したがって、Resources.Loadを使用してロードする必要はありません。

絶対ファイルパスを渡すだけです。私はiOSとMac OSでテストしました。できます。

関連する問題