WebView - Does an HTML page in WebView have access to the system.CachesDirectory?

I wanted to know if it is possible for an HTML page in a WebView to access (embed/play) a video file that has been downloaded to the system.CachesDirectory?

In our app, users can either stream or download some larger video files.  I am able to play the streaming or downloaded videos fine in the app using media.playVideo().  However, I have some instances where I need to embed/play the video in an HTML page using code on the HTML page to embed the video.  

Would an html page in the system.ResourcesDirectory have access to a video in the system.CachesDirectory?

You might be able to use system.pathForFile() and tack a “file://” scheme on the beginning to get a URL to the local file. I don’t know if this will work, but it will be worth trying.

Rob

Thanks Rob.  I had started to go down this path before I posted, but the video was not playing and I was not getting any errors.  Turns out that my path was not complete and there was en error in my video embed code.

For anyone interested, I got this working.  It looks like it is possible for a webview local html file in the system.ResourcesDirectory to access a video downloaded to the system.CachesDirectory

My local HTML page is in the app in the system.ResourcesDirectory and when on the device has the path

/var/containers/Bundle/Application/####/Test.app

The downloaded video file is saved to the system.CachesDirectory that has the path

/var/mobile/Containers/Data/Application/####/Library/Caches/Caches

The #### above are long alpha-numeric sequences.

I count the number of folders deep the HTML page is in the system.ResourcesDirectory and create a URL that backs out of that location and appends the system.CachesDirectory.  It ends up being something like this

…/…/…/…/…/…/var/mobile/Containers/Data/Application/####/Library/Caches/Caches/

When opening the local HTML file in a webView, I use a url variable in order to pass the location that I created of the system.CachesDirectory to the HTML page.

When the HTML page opens it parses out the path from the URL variable so that it can be used for embedding/playing the videos that get downloaded to the system.CachesDirectory

I wasn’t sure this was all going to work and will need to test more with an app store build, but for now, the dev build is working as expected.

You might be able to use system.pathForFile() and tack a “file://” scheme on the beginning to get a URL to the local file. I don’t know if this will work, but it will be worth trying.

Rob

Thanks Rob.  I had started to go down this path before I posted, but the video was not playing and I was not getting any errors.  Turns out that my path was not complete and there was en error in my video embed code.

For anyone interested, I got this working.  It looks like it is possible for a webview local html file in the system.ResourcesDirectory to access a video downloaded to the system.CachesDirectory

My local HTML page is in the app in the system.ResourcesDirectory and when on the device has the path

/var/containers/Bundle/Application/####/Test.app

The downloaded video file is saved to the system.CachesDirectory that has the path

/var/mobile/Containers/Data/Application/####/Library/Caches/Caches

The #### above are long alpha-numeric sequences.

I count the number of folders deep the HTML page is in the system.ResourcesDirectory and create a URL that backs out of that location and appends the system.CachesDirectory.  It ends up being something like this

…/…/…/…/…/…/var/mobile/Containers/Data/Application/####/Library/Caches/Caches/

When opening the local HTML file in a webView, I use a url variable in order to pass the location that I created of the system.CachesDirectory to the HTML page.

When the HTML page opens it parses out the path from the URL variable so that it can be used for embedding/playing the videos that get downloaded to the system.CachesDirectory

I wasn’t sure this was all going to work and will need to test more with an app store build, but for now, the dev build is working as expected.