0

I have a hybrid app that plays mp3, but I don't want to set all the mp3s inside the assets folder, I want the user to select mp3 that he/she likes to download.

So the question is... Can I use a previously downloaded mp3 inside webview, natively in the app data folder?

Something like this in file:///android_assets/index.html:

<html><body>
<audio src="file:///Android/data/com.packagename/previouslydownloaded.mp3">
</body></html>

Thanks a lot.

P R
  • 105
  • 9
juanram0n
  • 356
  • 3
  • 22

1 Answers1

0

Since Android Lollipop you can use files from anywhere because WebView has gained full support for content:// scheme. Older Android versions may also work (for example, MediaPlayer gained Uri support for playback in API 14). Just create a ContentProvider and refer to associated content uris directly in web page.

<html><body>
<audio src="content://com.example.authority/file/path.mp3">
</body></html>

Both files from secondary external storage (accessible only via Storage Access Framework) and your app private files, exposed via FileProvider or something like that, should be accessible via content://.

Note, that writing ContentProviders is slightly tricky business, you may want to look at following question and all it's answers: Is it possible to use content:// as a source for an <audio> element in a WebView

user1643723
  • 4,109
  • 1
  • 24
  • 48