June 4, 2012

Auto play and stop html5 audio with jquery

Question by Łukasz Borawski

i have a little problem.

I have a facebook like box window on overlay. This box hide when user click like – obviously. I wanna to use audio element when this window is visible and stop audio when this window will be hide.
So this is my html and jquery. Please help me.

<audio id="audio_player" loop="loop">
    <source src="fileadmin/templates/main/crowd.mp3" type='audio/mpeg; codecs="mp3"'>
    <source src="fileadmin/templates/main/crowd.ogg" type='audio/ogg; codecs="vorbis"'>

function audio_player (){
    if (
            $('audio').each(function() {
                var song = $(this);
    else if (
        $('fb_like_box').css('display','none')) {
            $('audio').each(function() {
                var song = $(this);
    else {}

Answer by Starx

Your code syntactically wrong.

First of all, the following syntax is for assigning value.


It assigns the property block to element $('#fb_like_box');

If you want to check it, as that, you have use something like this:

if($('#fb_like_box').css('display') == 'block') {
    // then do something

A good way to do what you are attempting is:

if ($('#fb_like_box').is(':visible')) {

    $('audio').each(function() {

} else {

    $('audio').each(function() {

May 11, 2012

Javascript method pause() only works with audio element

Question by Panini Venkataraman

So I have a function like this:

function music(song) {
var audio = new Audio("audio/" + song + ".ogg");

And the problem I’m having is that audio.play() works perfectly, but if I try audio.pause() later (after defining the same variable), it doesn’t work. However, if I do this:

function music() {
var audio = document.getElementById("songname");

Then I can define the variable and later use audio.pause() to pause the track. The problem is that I want to do this within javascript because I have a lot of tracks and I don’t want to create audio elements for each one.

Why doesn’t the first piece of code allow me to pause the audio later? Is there any way I can let it pause the audio later? If not, are there any alternatives that involve playing the audio from javascript by passing a parameter which is the song title? I don’t want to have to include the src in the html.

I suspect that jQuery might have an easy solution for this but form what I’ve researched I couldn’t find anything.

Answer by Starx

I just tested a snippet and it works as such

function music(song) {

    var audio = new Audio("audio/" + song + ".ogg");
    setTimeout(function() { audio.pause(); }, 5000);

April 11, 2012

How to play a notification sound on websites?

Question by valmar

When a certain event occurs, I want my website to play a short notification sound to the user.

The sound should not auto-start (instantly) when the website is opened.
Instead, it should be played on demand via JavaScript (when that certain event occurs).

It is important that this also works on older browsers (IE6 and such).

So, basically there are two questions:

  1. What codec should I use?
  2. What’s best practice to embed the audio file? (<embed> vs. <object> vs. Flash vs. <audio>)

Answer by Starx

How about the yahoo’s media player
Just embed yahoo’s library

<script type="text/javascript" src="http://mediaplayer.yahoo.com/js"></script> 

And use it like

<a id="beep" href="song.mp3">Play Song</a>

To autostart

$(function() { $("#beep").click(); });
December 29, 2011

how to play wav stream in safari vai javascript

Question by Jeffrey Hsu

I’ve tried Audio / embed / Object, but fail at all of them.

play wave file is ok, but cannot play stream.

<audio><source src="sample.wav" /></audio> OK

<audio><source src="http://mydomain/stream" /></audio> fail

<object data="sample.wav"></object> OK

<object data="http://mydomain/stream"></object> fail

jPlayer also cannot work!

I found a clue:
If when I play it in firefox before, then safari works. It seems firefox download it so the safari can play it in quicktime’s cache.
<embed src="" height="45" width="170" type="audio/x-wav"/>

Answer by Starx

You should use html5audioplayer. It slags a bit, but works in the end….


Please fill the form - I will response as fast as I can!