Posts tagged play

Play a sound via JavaScript

3

Working in a project recently I found myself in the need of playing a sort of "alert" sound with JavaScript. It turns out there are not so many good options out there. The first thing that came to my mind was to use an embed and the Play and Stop JavaScript controls.

Well that only works in IE and some versions of Netscape so let's just forget about all those tags (embed, bgsound... etc)

So in my search for an easy solution I came across a very neat jQuery plugin that does exactly that job, the jQuery Sound Plugin by Jorn Zaefferer. I have been completely unable to find the plugin Website, so I will post it's code myself here. This plugin is a port from scriptaculous' version by Jules Gravinese.

How to use:

Using this to play a sound couldn't be easier, let's see an example with no parameters except for the file we want to play:

$.sound.play('files/sample.mp3')

This way the mp3 file sample will start playing. If we prefer the sound to start playing stopping the previous one, we can use tracks, this way we can play one sound per track, and every time we set a new sound for a certain track it stops the current sound there:

$.sound.play('files/sample.mp3',{
   track: "track1"
});

Other options are:

// timeout: Specify for how long the sound can play in milliseconds
$.sound.play('files/sample.mp3', {
   timeout: 4000
});

We can store a sound reference in an object so that we can later on stop it:

var sound = $.sound.play('files/sample.mp3');
sound.remove();

And finally we have the option to enable/disable sounds at all:

$.sound.enabled = false;
$.sound.enabled = true

The plugin code:

(function($) {
$.sound = {
	tracks: {},
	enabled: true,
	template: function(src) {
		return '<embed style="height:0" loop="false" src="' + src + '" autostart="true" hidden="true"/>';
	},
	play: function(url, options){
		if (!this.enabled)
			return;
		var settings = $.extend({
			url: url,
			timeout: 2000
		}, options);
		if(settings.track){
			if (this.tracks[settings.track]) {
				var current = this.tracks[settings.track];
				current.Stop && current.Stop();
				current.remove();
			}
		}
		var element = $.browser.msie
		  	? $('<bgsound/>').attr({
		        src: settings.url,
				loop: 1,
				autostart: true
		      })
		  	: $(this.template(settings.url));
		element.appendTo("body");
		if (settings.track) {
			this.tracks[settings.track] = element;
		}
		setTimeout(function() {
			element.remove();
		}, options.timeout)
		return element;
	}
};
})(jQuery);

I will probably minify this code before I upload it to the production server, so if I don't forget I will also post here the minified version,

Hope you found it useful,
Alex

II Play with the food :)

2

Funny food column, to put to the right I guess...Hi all,
Well, regarding the amazing views of the first entry "Play with the food", I've decided to do some sort of follow up of it, but with a video I saw:

This video is about what happens to MacDonalds food when it is left in a jar for weeks :D

The fun thing is that fries stay the same :)

If any of you guys ever decide to try this, please post a link in the comments, I would love to post the video here! :)
I may even try myself, never know...

Play with the food! :)

6

Funny eggs?

It's weekend, and I was a little bored, so I was looking around in the web and I came across this!:

eggcellent picture!

I just love the faces of those eggs... :)

Now a little story, by Orange and banana... emo style

emo oranges

I just love how she got "undressed" lol very very imaginative!

And the final pic, just a bunch of special effects, by JibJab:

JibJab eggs

Check out the II Play with the food article!

Go to Top