I am building a web application for playing some audio files offline. I have read and tested the Cache part of the Service Worker API, and I can easily add all my audio files to a named cache. However, the browser does not seem to use it when I add a <audio src='/audio/file.mp3'>
to the DOM and play()
it, even if that exact URL is added to a cache.
It makes sense in some way that the cache is not automatically used, as then it would not be any point in naming the cache.
I am using create-react-app, and as I have not ejected, I'm not controlling the Service Worker. As far as I understand, though, it is not recommended to cache larger files up front anyway, so I'm created a function in the UI to let the user decide when to cache all files.
My question is: How can I put a cached audio file into the audio element?
EDIT: I thought the problem should be understandable from my question, but was asked to add code, so here is what I've tried.
The markup has an audio element and two buttons. First button loads an mp3 url into a cache, second button tries two different ways of setting this as a source for the audio element:
- Insert blob from cached response
- Set cached request url as audio src
None of them works, of course, that's why I write this. :)
<div>
<audio src="/audio/shape_of_you.mp3" controls/>
</div>
<div>
<button onclick="addToCache()">Add to cache</button>
<button onclick="playCachedAudio()">Play cached audio</button>
</div>
<script>
function addToCache() {
caches.open('myCache').then((cache) => {
cache.add('/audio/rise.mp3').then(() => {
console.log('cached');
});
});
}
function playCachedAudio() {
caches.open('myCache').then((cache) => {
cache.match('/audio/rise.mp3').then((cachedAudio) => {
cachedAudio.blob().then(cont => {
let audioEl = $('audio');
audioEl.attr('src', cont);
// ...fails with the following error in console:
// HTTP “Content-Type” of “text/html” is not supported. Load of media resource http://localhost:3000/[object%20Blob] failed.
// Cannot play media. No decoders for requested formats: text/html
audioEl.attr('src', '/audio/rise.mp3');
// Works online, but not offline (resource is not found)
});
});
});
}
</script>
I am looking for either
a way to add the blob as audio content
or
another way to programatically force browser to guarantee a list of audio files to be cached for offline use.