-1

My app can download video content from firebase, save it to the Application.dataPath, and then play it in the editor. However, when I build it to iOS, it does not find the file. I have also tried using Application. persistentData but it does seem to be there. Any helpful pointers?

// _____ forgive my ugly code :/ ______ // ____ i have deleted crappy comments etc, tidied up, but i think this gives an idea of th tricks i'm using ...

    public void FUKN_download_viddy()
    {
    FirebaseStorage storage = FirebaseStorage.DefaultInstance;
    StorageReference path_reference = storage.GetReference("videoname.mp4");

    pathToSaveTo =  "file://" +
    Application.dataPath +
    "/Resources/Adam/Year 1 Semester 1/"+
    "videoname.mp4";

    path_reference.GetFileAsync(pathToSaveTo).ContinueWith(task => {
    if (!task.IsFaulted && !task.IsCanceled)
    {
    Debug.Log("File downloaded.");
    }
    });
    }

    // ____________




    // _________

    public void FUKN_play_viddy()
    {
    string viddyplayerpath = "Adam/Year 1 Semester 1/" + "videofile";

    var viddyKlip = Resources.Load<VideoClip>(viddyplayerpath);

    VideoPlayer videoPlayer = GetComponent<VideoPlayer>();
    videoPlayer.clip = viddyKlip;    // index offset
    videoPlayer.Play();


    }
    // _________


1 Answers1

0

I think that your issue is that Resources.Load pulls in elements included in the Assets/Resources directory - not your filesystem (see the doc page).

You want to use VideoPlayer.src as noted here.

ex:

public class TestVideo: MonoBehaviour {
    [SerializeField] private VideoPlayer _videoPlayer;

    public Play() {
        _videoPlayer.src = "file://" + Application.persistentDataPath + "/Resources/Adam/Year 1 Semester 1/" + "videoname.mp4";
        _videoPlayer.Play();
    }
}

Note that I used persistentDataPath, dataPath will be read-only on iOS and Android.

I hope that helps!

--Patrick

Patrick Martin
  • 2,993
  • 1
  • 13
  • 11
  • Thanks Patrick, oh wow, you are the guy from the firebase tutorial videos! Famous! I have been studying your videos, and you comment here.. but still can't get it to work. I want my users to be able to download very short video clips to their iOS or Android devices. They don't need to upload anything. (these will be uploaded thru the console by me) They will be able to delete videos if they want. When you say persistentDataPath & dataPath are read-only on iOS and Android, does that mean I can't download from firebase & save to these to the device..? – Adam Stone Jul 10 '20 at 01:34
  • My code works when in the Unity Editor, however after building to iOS, the files do not seem to download. Here are the bits of my code that I suspect I am doing wrong: – Adam Stone Jul 10 '20 at 01:36
  • ` pathToSaveTo = Application.persistentDataPath + "/" + "Y1_002_election.mp4"; FirebaseStorage storage = FirebaseStorage.DefaultInstance; StorageReference path_reference = storage.GetReference("Y1_002_election.mp4"); path_reference.GetFileAsync( pathToSaveTo ).ContinueWith(task => { if (!task.IsFaulted && !task.IsCanceled) { print( "downloaded"); } else { print " NOT downloaded"; } ` – Adam Stone Jul 10 '20 at 01:36
  • // _________ if (File.Exists(pathToSaveTo) == true) // on iOS this always seems false { ViddyPlayer.url = pathToSaveTo; ViddyPlayer.Play(); display4.text = " playing viddy"; } else { display4.text = " computer sez no"; // this is displayed } // _________ – Adam Stone Jul 10 '20 at 01:36
  • sorry, i cant figger out how to do the code formatting here in stack overflow... – Adam Stone Jul 10 '20 at 01:42
  • Multiline code samples in the comments section doesn't seem to work. I went and tried converting it to a Gist for you. I like to use gist.github.com: https://gist.github.com/patm1987/ad42d2edcc2d9342b74eeff47b245ed3 – Patrick Martin Jul 10 '20 at 15:28
  • I don't have easy access to an iOS device right now or I'd test it myself. What is `Application.persistentDataPath` for you? If it isn't preceded with `file://` does it work if you add this? Barring that, if you can share any error logs from the XCode output that would help. If you think it's possibly Firebase related - say rules for example - it might help to turn the log level ( https://firebase.google.com/docs/reference/unity/class/firebase/firebase-app#class_firebase_1_1_firebase_app_1aa95be32e8c8d56783fcb16234fbe2b63 ) to something like `LogLevel.Verbose`. – Patrick Martin Jul 10 '20 at 15:38
  • I have been trying both persistentDataPath & dataPath ,,, but they are both read-only . . . what should i use to save files to my iOs or Android? – Adam Stone Jul 13 '20 at 00:42
  • `dataPath` will often be read only, but `persistentDataPath` is intended to be written to ( https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html ). It would help if you shared any error you were getting? There's a chance, for example, that you need to create the directories leading up to where you want to write out the file (ex: https://stackoverflow.com/questions/3201598/how-do-i-create-a-file-and-any-folders-if-the-folders-dont-exist ). – Patrick Martin Jul 13 '20 at 16:00
  • Also search for the string `protected virtual string PathToPersistentDataPathUriString(string filename)` in https://github.com/firebase/quickstart-unity/blob/master/storage/testapp/Assets/Firebase/Sample/Storage/UIHandler.cs to see an example for how to construct a file URI to save to. – Patrick Martin Jul 13 '20 at 16:03
  • 1
    Patrick! Thanks for your help & patience! I figgered it out !!! I made a youtube video, i hope it will help somebody :) https://youtu.be/SbVYYsBs14w – Adam Stone Jul 17 '20 at 05:16