views:

1153

answers:

4

Hi stackoverflow experts

I've been experimenting with the audio and local storage features of html5 of late and have run into something that has me stumped.

I'd like to be able to cache or store the source of the audio element locally to enable speedier and offline playback. The problem is I can't see how this is possible with the current implementation.

I have tried the following using webkit:

  1. Creating a manifest file to set up local caching but the audio file appears not to be a cacheable item maybe due to the way it is stream or something

  2. I have also attempted to use javascript to put an audio object into local storage but the size of the mp3 makes this impossible due to memory issues (i think).

  3. I have tried to use the data uri and base64 to use the html as a audio transport that can be cached but again the filesize makes this prohibitive. Also the audio element does not seem to like this in webkit (works fine in mozilla)

  4. I have tried several methods of putting the data into the local database store. Again suffering the same issues as the other cases.

I'd love to hear any other ideas anyone may have as to how I could achieve my goal of offline playback using caching/local storage in webkit.

+1  A: 

I guess it would be preferable to get the manifest approach working, since this feels like the most relevant mechanism for locally caching the file.

What happens if you alter the audio file's HTTP headers, e.g. Content-Type and Expires? Does the browser do something different if the file extension is changed?

Premasagar
Premasagar, thank for your answer but unfortunately neither of these approaches seems to have any effect :(
andrewdotcom
Hmmm. It seems like there has to be a way to do this. Please do post whatever solution you find, as this will be useful to know.
Premasagar
+1  A: 

Hi Andrew. I see you've had no luck so far.

You might want to take a look at JAI (JavaScript Audio Interface) ("the world's first javascript interface for web <audio>"). Or get in touch with Alastair MacDonald, who wrote it.

Failing that, the HTML5 Doctor may be able to assist.

Premasagar
Thanks for the tips - i'll head over to HTML5 Doctor and see what they've got.
andrewdotcom
A: 

Hi Andrew. Were you able to come up with anything that worked for you? I need to do something similar & haven't found a way to cache audio yet. I didn't see anything in the HTML5 specs, nor HTML5 Doctor (or related) sites.

Cheers

tchill
+2  A: 

I've been trying to do this myself, on the iOS (for iPhone / iPad) but it refuses to cache audio files in offline, even if in Cache Manifest.

It does not error, but instead simply pretends to have played the audio element if invoked via JavaScript without a control. If it's embedded with a control, it displays an alternate control that says "Cannot play audio file.". It works fine if the application is able to go online.

It seems not to cache the audio, playing another sound resource seems to cause it to clear the previous resource from memory - this is pretty worthless functionality even when online.

I have experimented with base64 encoding the audio as data URI's. This works in Safari on the desktop (at least for fairly short samples of around 20-30k that I've been using) but seems not to be supported at all on iOS - it silently does nothing, which is highly annoying.

I don't know about other vendors - Google Chrome used to not support data URI's for audio but perhaps they fixed it... - it seems like it's not possible for now though.

Update: Minor discrepancy with iPhone OS 3.x (tested with 3.1.2): If an audio element is specified in an offline web app but it doesn't have a control, it displays a non-interactive control with a non-animated spinner on it (which it definitely shouldn't do). I assume this is fixed in iOS 4.x (which should be out next week).

Iain Collins
Your tests with 20~30k files are meaningless since the iPhone doesn’t cache files larger than 15 KB. However, I [read](http://stackoverflow.com/questions/2956915/html5-offline-video-caching-in-mobile-safari/2957040#2957040) you also tested smaller (5~10k) files, and got similar results. Interesting, to say the least.
Mathias Bynens
Yes, I tested with a range including very small sound files "just in case" there was a special rule. The largest file I cache is 180 KB, wither another at 135 KB as well as some at 25-35 KB, these are all images specified in my cache manifest and all work offline (including with airplane mode turned on) on both my iPad and iPhone (3GS running 3.1.2) so I'm not sure where 15 KB limit comes from.
Iain Collins
Had a quick read up on the 15 KB limit - seems it refers to what the browser caches during normal browsing, but not to offline web apps with a cache manifest, at least as near as I can tell.
Iain Collins