Caching remote assets

Hi good people!

Thanks for an excellent framework. I have a question about Assets that I fail to understand by reading the documentation: Is it possible to cache an asset specified by remote URI (an mp3) so that you can listen to it with Expo Audio framework even when you’re in flight mode?

I find no examples anywhere of how to create an Asset with a remote url and then download it. Please help!

Hey @simpan83,

You’ll be able to cache remote assets after an initial connected fetch, but you won’t be able to do so before first-load (as in bundling the assets in the binary). https://docs.expo.io/versions/v34.0.0/guides/preloading-and-caching-assets/

Cheers,
Adam

Hi @adamjnav,

Thanks for your reply. I still fail to understand how to do this initial connected fetch. The link you provided explains how to preload an image using the Image.prefetch(url) method, but not how to do it for any other file type.

I’m building a music player in which mp3s are retrieved by scannable codes containing remote URLs. After this code is scanned, I want to provide functionality to fetch the mp3 and store it locally, so that the audio is playable even if the user goes on an airplane. Much like Spotify’s download feature. How would I turn a remote URL (like “https://mywebsite.com/audio/MySong.mp3”) into a downloaded and stored Asset?

Thanks for taking the time to help me!

Regards,
Simon

You’ll likely want to make use of the FileSystem module, specifically the downloadAsync method. https://docs.expo.io/versions/v34.0.0/sdk/filesystem/#filesystemdownloadasyncuri-fileuri-options which will allow you to download the contents of a remote file to the app’s file system on the device.

1 Like

Thank you so much for your help! Keep up the good work!

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.