开发者

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 开发者_StackOverflowi did this using BGSOUND tag it works only in IE,


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


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜