Forum Moderators: open
I am creating a website where when the user performs certain actions (like clicking things), a sound plays. In IE, I am using BGSOUND. However, the only way I can get the BGSound to dynamically play is by setting the "src" attribute to the sound file (i.e. bgsound.src="http://whatever/soundfile.mp3"). When I do this, it goes back to the server to get the file each time. So, there is a delay. If my user clicks on a few objects quickly, they may miss some sounds because they click on the next object before the sound linked to the first object finishes downloading.
Is there a way to dynamically play sounds without having the browser always go back to the server to retrieve the sound files? I know with images, you can preload them, so they reside in browser cache, so you don't have to keep going back to the server to get them. Can you do that with sounds?
Thanks.
You create a file that processes the mp3s to the client end and in that file you output the mp3 along with a 304 header for a predefined period of time.
You need to setup a header to specify the resource type, otherwise the client may not know what to do with the resource (as it would be something like .php).
eg:
bgsound.src="http://whatever/all_sounds.php?sound=soundfile.mp3"
then the all_sounds.php will output the headers along with the specific mp3 file that is passed as a parameter (or you could use some database and pass an index only). This should also work whether you use flash or any other type of resource.
Flash is the most graceful solution here, you wait until all data's loaded and then display the interface. Embedded files won't cut it.
Yeah, from what I've heard, Flash would be the best/most reliable method. Not just from a caching point of view, but for cross-browser support and timing as well. The flash would contain just the sounds. JS would interface with your Flash object. (I think someone has made available an object which handles a lot of this for you...?)