Rajasekar Gunasekaran
Rajasekar Gunasekaran

Reputation: 1827

How to play a sound, when i click a button or link in a browser,

how to play a sound file, when i click a link or button in my web page , I also need place more than one link or button each one have different sound file.

actually i did this using BGSOUND tag it works only in IE,

Upvotes: 3

Views: 12033

Answers (2)

shybovycha
shybovycha

Reputation: 12235

Step 1: download the 'standalone' version of audio player

Step 2: include player's JS to your page:

<script type="text/javascript" src="audio-player.js"></script> 

Step 3: setup player and implement functions to embed player for each file:

<script type="text/javascript">  
    AudioPlayer.setup("player.swf", {  
        width: 290
    });

    function moo() {
        AudioPlayer.embed("player", {soundFile: "moo.mp3", autostart: 'yes'});  
    }

    function foo() {
        AudioPlayer.embed("player", {soundFile: "foo.mp3", autostart: 'yes'});  
    }
</script>  

Step 4: create some element with id = player (or whatever you've specified):

<div id="player"></div><br />

Step 5: provide 'switchers' for your audio files:

<a href="#" onClick="moo();">play file 1</a><br />
<a href="#" onClick="foo();">play file 2</a>

You should end with something like this:

<html>  
    <head>  
        <title>Your website</title>  
        <script type="text/javascript" src="audio-player.js"></script>  
        <script type="text/javascript">  
            AudioPlayer.setup("player.swf", {  
                width: 290
            });

            function moo() {
                AudioPlayer.embed("player", {soundFile: "moo.mp3", autostart: 'yes'});  
            }

            function foo() {
                AudioPlayer.embed("player", {soundFile: "foo.mp3", autostart: 'yes'});  
            }
        </script>    
    </head>  
    <body>  
        <div id="player"></div><br />
        <a href="#" onClick="moo();">play file 1</a><br />
        <a href="#" onClick="foo();">play file 2</a>
    </body>  
</html>  

P.S.: here are some docs on player's options you could pass with .embed() or .setup() functions

Upvotes: 6

Marty
Marty

Reputation: 2963

Why cant you just link to the sound file on the web app?

eg, something like this:

<a href="/pathtosound/sound.mp3">Click here to play a sound</a>

Upvotes: 0

Related Questions