Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Local Storage of audio element source - is it possible?

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.

like image 515
andrewdotcom Avatar asked Oct 23 '09 08:10

andrewdotcom


People also ask

How do you store audio in HTML?

HTML | <audio> src Attribute The HTML <audio> src attribute is used to specify the URL of the audio files. We should use the Mp3 file for play the audio in the Internet Explorer and Safari Browsers. The source element can be used to add audio files to a webpage to work in all the Browsers.

What is the correct HTML5 element for playing audio files?

The HTML <audio> element is used to play an audio file on a web page.

What is true with local storage in HTML5?

Q 15 - Which of the following is true about Local Storage in HTML5? A - HTML5 introduces the localStorage attribute which would be used to access a page's local storage area without no time limit.


2 Answers

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).

like image 163
Iain Collins Avatar answered Sep 22 '22 19:09

Iain Collins


So it's been a while since I asked this question and I thought i'd give some info about how we solved it. Basically we encoded the data into PNG's using a similar technique to this:

http://audioscene.org/scene-files/yury/pngencoding/sample.html

Then cached the image on the mobile device using html5 local storage and accessed it as needed. The PNG's were pretty big but this worked for us.

like image 35
andrewdotcom Avatar answered Sep 21 '22 19:09

andrewdotcom