Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Resolved Download Video from URL Array List with UnityWebRequest

Discussion in 'Multiplayer' started by ozlgktg, May 16, 2020.

  1. ozlgktg

    ozlgktg

    Joined:
    Jan 15, 2020
    Posts:
    21
    Dear Unity Members,

    I use UnityWebrequest function for downloading video and picture. And it works good.

    For example: I have two URLs on my array list(VideoURLs). And I have video1 and video2 URLs in this array list. OK! This codes download all videos perfectly! But downloaded videos names are always video2(the last variable of array list)... This code download video1(with named video2), and video2 is overwrited on video1(because video1 name was video2). More description is below...

    I have added my URLs to here:
    Code (CSharp):
    1. public string[] VideoURLs;
    And when I click a button running this void Download() function:
    Code (CSharp):
    1. public void Download()
    2.     {
    3.         int URLCount = VideoURLs.Length;
    4.  
    5.         for (counter = 0; counter < URLCount; counter++)
    6.         {
    7.             URLVideo = VideoURLs[counter];
    8.             StartCoroutine(DownloadVideo());
    9.         }
    10.     }
    And the IEnumerator DownloadVideo() coroutine function is this:
    Code (CSharp):
    1. public IEnumerator DownloadVideo()
    2.     {
    3.        UnityWebRequest wwwVideo = UnityWebRequest.Get(URLVideo);
    4.         yield return wwwVideo.SendWebRequest(); //After this line coroutine will not continue and go back to  Download() Function. Why?
    5.  
    6.         if (wwwVideo.isNetworkError || wwwVideo.isHttpError)
    7.         {
    8.         Debug.Log(wwwVideo.error);
    9.         }
    10.     else
    11.         {
    12.             File.WriteAllBytes(Application.persistentDataPath + "Video"+ counter + ".mp4" , wwwVideo.downloadHandler.data);
    13. //Writing videos... But its writing only last array counter name... And all videos are writing overwriting with same name(with last counter value)
    14.         }  
    15.     }
    Thank you all for help and assistance...
     
    Last edited: May 17, 2020
    james8hicks likes this.
  2. ozlgktg

    ozlgktg

    Joined:
    Jan 15, 2020
    Posts:
    21
  3. ozlgktg

    ozlgktg

    Joined:
    Jan 15, 2020
    Posts:
    21
    Dear Unity Members,

    HERE IS THE SOLUTION:
    My Download() function is a standart void function. But DownloadVideo() function type is IEnumerator. So; While the IEnumerator DownloadVideo() function is still running, void Download() function is not waiting for end of IEnumerator DownloadVideo() function.

    When I understand that, I have changed the void Download() to IEnumerator Download(). And I called the IEnumerator DownloadVideo() function with yield return StartCoroutine(DownloadVideo()); // this line mean is: wait for DownloadVideo() function is finished!

    The last version of void Download() function is here: (" With new type: IEnumerator Download()" )
    Code (CSharp):
    1. public IEnumerator Download()
    2.     {
    3.         int URLCount = VideoURLs.Length;
    4.         for (counter = 0; counter < URLCount; counter++)
    5.         {
    6.             URLVideo = VideoURLs[counter];
    7.           yield return StartCoroutine(DownloadVideo());
    8.           // this "yield return" mean is: wait for DownloadVideo() function is finished! I mean, "for loop" is froozen now...
    9.         }
    10.     }
    Thank you and best wishes!