There are several ways to play sound in a web page. Why you need sound in a web page. The most common type of feedback is sound. The computer clicks when you select things, beeps when there are errors, and makes other noises to alert you to situations. But Web pages don’t usually have this type of feedback.

As I said above that there are several ways to play sound in web page. But the most common problem is the “Browser specific solutions”. Some will run on Internet explorer, while others will run on Mozilla Firefox and some on Google Chrome.

I have provided a solution here that will run on almost all those browsers that support Flash Player plug-in and that means nearly 99%.

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
    width="40"
    height="40"
    id="audio1"
    align="middle">
    <embed src="wavplayer.swf?h=1&w=1"
        bgcolor="#ffffff"
        width="40"
        height="40"
        allowScriptAccess="always"
        type="application/x-shockwave-flash"
        pluginspage="http://www.macromedia.com/go/getflashplayer"
    />
</object>

<script>
function getPlayer(pid) {
	var obj = document.getElementById(pid);
	if (obj.doPlay) return obj;
	for(i=0; i<obj.childNodes.length; i++) {
		var child = obj.childNodes[i];
		if (child.tagName == "EMBED") return child;
	}
}
function doPlay(fname) {
	var player=getPlayer("audio1");
	player.play(fname);
}
function doStop() {
	var player=getPlayer("audio1");
	player.doStop();
}
</script>

<form>
<input type="button" value="Play Sound" onClick="doPlay('duck.wav')">
<input type="button" value="Stop Sound" onClick="doStop()">
</form>

You can also download complete running code from the link below.

Download Source code zip

Tagged with: ProgrammingSource Code
 

3 Responses to Playing Sound on a webpage using Javascript and Flash Player

  1. Bart says:

    Nice … if it would actually work. Downloaded the example and it does exactly nothing :)

  2. […] Following code might help you to play sound in a web page using javascript only. You can see further details at http://sourcecodemania.com/playing-sound-javascript-flash-player/ […]

  3. Vinay says:

    I am getting error at player.play(fname); in function doPlay(fname). Please help.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.