views:

123

answers:

2
+1  Q: 

Audio on mouseover

It it working partially, but how do I get the below to work with multiple audio files:

    <!DOCTYPE HTML>
    <html>
    <body>

    <script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"&gt;&lt;/script&gt;

    <script type="text/javascript">
    $(function(){
        var birdhover     = $('.birds');
            var birdaudio = birdhover.find('audio')[0];

        birdhover.hover(function(){
           birdaudio.play();
        }, function(){
           birdaudio.stop();
        });
    });
    </script>

    <div class="birds" style="width:30px;height:30px;background-image:url(image1.gif');cursor:pointer;">
       <audio src="sound1.ogg" preload="auto"></audio>
    </div> 

    <div class="birds" style="width:30px;height:30px;background-image:url('/home/imran/Desktop/images/vowel_short_2.gif');cursor:pointer;">
(image2.gif');cursor:pointer;">
       <audio src="sound2.ogg" preload="auto"></audio>
    </div> 

    </body>
    </html>

Both images play the same sound.

+2  A: 

updated for multiple sound files
This is what I used before: http://plugins.jquery.com/project/sound

Usage:

$("#sound").sound({swf: url});
$("#sound").load(url);
$("#sound").play();
$("#sound").pause();
$("#sound").stop();
$("#sound").volume(0-100);

(taken from jQuery plugins page)

for mouseover:

$('#selector').mouseover(function() {
   $("#sound").play('path/to/wav/or/mp3');
});

$('#second-selector').mouseover(function() {
   $("#sound2").play('path/to/second/wav/or/mp3');
});
Neurofluxation
+2  A: 

if you don't mind using HTML5:

HTML

<div class="birds">
   <audio src="/sounds/bird.mp3" preload="auto"></audio>
</div> 

Javascript

$(function(){
    var birdhover     = $('.birds');
        var birdaudio = birdhover.find('audio')[0];

    birdhover.hover(function(){
       birdaudio.play();
    }, function(){
       birdaudio.stop();
    });
});

http://www.w3schools.com/html5/tag_audio.asp

jAndy
+1 for HTML5 ...
Marko
I just tried this and it just produces a blank screen? Am I supposed to place an image between the audio tags?
oshirowanen
I am using chromium 5.x.
oshirowanen
I have updated the original question to show you what I currently have.
oshirowanen
Hi, got it to work, didn't realise you were using jquery. I have uploaded the original question, as I do not understand how to do the same thing with multiple audio files.
oshirowanen
@oshirowanen: I created an example for you http://www.jsfiddle.net/hSR7f/4/
jAndy
@ jAndy: That example just has an add_to function. Nothing relevant to playing audio files that I can see.
oshirowanen
@oshirowanen: ups, http://www.jsfiddle.net/hSR7f/6/
jAndy
@jAndy,It's not working for me on your example page, even when I copy that script into my own webpage, it will not work
oshirowanen