tags:

views:

649

answers:

3

I'm trying to get jQuery to play a sound on element hover/click. (It's like a flash website without flash)

I've tried the methods recommended in Cross-platform, cross-browser way to play sound from Javascript?, the jQuery Sound plugin and a couple of other tutorials without any success. I'm assuming this is because they haven't been updated since 2008.

Does anyone have any recommendations?

+2  A: 

Don't need any of those. The HTML tag, combined with JavaScript, will do the trick.

<audio id="soundHandle" style="display: none;"></audio>
<script>
  soundHandle = document.getElementById('soundHandle');
  soundHandle.src = '/blah/blah.mp3';
  soundHandle.play();
</script>
Delan Azabani
That's a short bit of code. I'm more comfortable with jQuery, but how would I use java to attach this to a mouse over event?
Erin
@Erin: Javascript != Java, and this is pure Javascript. Note, however, that this is not cross-browser at all as it uses the HTML5 `<audio>` element (not working on IE at all, different browsers supporting different formats/codecs, etc.). See: http://html5doctor.com/native-audio-in-the-browser/.
Max Shawabkeh
Why is this downvoted? This **should** be the way to go. Yes, it's not cross-browser (yet), but it will be pretty soon and **it is the standard**. Actually, the only browser **not** currently supporting it is Internet Explorer. Source: http://caniuse.com/#feat=audio
Felix
No, there are more browsers that do not support it, especially in the mobile sector. Plus the question emphasizes cross-browser and cross-platform compatibility. When the solution works for less then 50% of the market it doesn't satisfy this requirement.
Jakub Hampl
And you think more mobile devices support Flash than `<audio>`? I doubt that's true, and even if it is, the relevant devices (that is, current smartphones -- iPhone, Android) support it. So, percentage-wise, I think more mobile devices support `<audio>` than Flash. Even if I'm wrong (if you have figures please correct me), I will be right in the near future. Also, that "less than 50%" is about to change pretty soon, too (IE is down to 60% from 95% a few years ago). If you want to be future-proof, use `<audio>`.
Felix
No but IE does not support it. iPhones do support the tag, but not in a reasonably scriptable manner (they will play the sound on a dedicated screen) thus again useless for this purpose. The truth is that sound (as in a user-interface element, not playing an mp3 song) is currently out of today's cross-browser compatible techniques. Also the percentage doesn't have to be 50% to not be cross-browser compliant, you should be aiming for something like 5%.
Jakub Hampl
A: 

try this http://johnny-ray.com/swagg-player/ it using the flash to play a sound or this one http://www.schillmania.com/projects/soundmanager2/

FDisk
some ass hole down-voted :D
FDisk
A: 

Actually it is impossible to do in a truly cross-browser compliant way as for example iPhones will play sound only on a dedicated player "page". However some of the plugins in the other answers should do the job for most (desktop) browsers.

Jakub Hampl
Why the downvote?
Jakub Hampl