Thursday, June 23, 2022
HomeGame DevelopmentObtain a video to PersistentDataPath in Unity whereas additionally taking part in...

Obtain a video to PersistentDataPath in Unity whereas additionally taking part in it because it downloads?


I’m making an attempt to combine a video operate which is able to playback quick looping movies (much like YouTube Shorts or TikTok). I due to this fact need the movies to obtain to PersistentDataPath and be performed from there (quite than instantly off the online) in order that they aren’t frequently consuming knowledge/Web assets each time you re-access them.

I’d additionally just like the movies to have the ability to play again whereas downloading quite than ready till completion. That is the half I am having hassle with.

Code resembling the next works properly for downloading the video after which taking part in it again:

IEnumerator StartNewVideo(){

        fileName = "VideoName.mp4";
        fileURL = "https://www.web site.com/" + fileName;

        UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(fileURL); 

        yield return www.SendWebRequest();

        if (www.outcome == UnityEngine.Networking.UnityWebRequest.Outcome.ConnectionError || www.outcome == UnityEngine.Networking.UnityWebRequest.Outcome.ProtocolError) {
                Debug.Log(www.error);
            }
            else {
                System.IO.File.WriteAllBytes(Software.persistentDataPath + "/VideoCache/" + fileName, www.downloadHandler.knowledge);
            }
        }

        videoPlayer.url = Software.persistentDataPath + "/VideoCache/" + fileName;
        videoPlayer.Put together();
        yield return new WaitUntil(() => videoPlayer.isPrepared);
        videoPlayer.Play();

        www.Dispose();
}

The difficulty is I can’t determine a working methodology to playback whereas the downloading is in progress.

I attempted crudely one thing like this:

        UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(fileURL); 
            www.SendWebRequest();
            whereas (www.outcome == UnityEngine.Networking.UnityWebRequest.Outcome.InProgress) {
                if (www.downloadHandler.knowledge != null) {
                    System.IO.File.WriteAllBytes(Software.persistentDataPath + "/VideoCache/" + fileName, www.downloadHandler.knowledge);
                    if (www.downloadProgress > 0.05f) {
                        if (!videoPlayer.isPrepared) {
                            videoPlayer.url = Software.persistentDataPath + "/VideoCache/" + fileName;
                            videoPlayer.Put together();
                            yield return new WaitUntil(() => videoPlayer.isPrepared);
                            videoPlayer.Play();

                        }
                    }
                    
                }
                yield return null;

            }

The thought is to start out making ready and taking part in again the video as quickly as at the least 5% has been downloaded. Nonetheless, as quickly because it makes an attempt to start out playback it provides a IOException: Sharing Violation on path presumably as a result of it may’t write knowledge to the file and playback video from it on the identical time.

There may be additionally probably an inefficiency right here as a result of I’m working System.IO.File.WriteAllBytes each time quite than simply including to the top of the file as new knowledge turns into out there.

Is there any resolution that may make this work? Any concepts? Or is that this principally going to be inconceivable? Thanks.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments