Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing sound inside javascript event

Tags:

javascript

I have an event where I need to play a mp3/wav sound every time a certain condition is verified. Is there a way to play it cross-browser, also even if I open android browser on a tablet?

Actually I am using a javascript plugin called ionSound but looks like it doesn't work under android's tabled..

Like:

jQuery.ajax({
    type: "GET",
    dataType: "json",
    url: "./getData.php?idx="+id,
    success: function(data) {
        if (data.length > 0) {
            // alert('Gotta play sound...');
            dataFound = true;
            $.ionSound({
                sounds: [ "woop_woop" ],
                path: "../../images/"
            });

            $.ionSound.play("woop_woop");
            // alert('Played sound right now...');
        } else {
            dataFound = false;
        }
    }
});

Thanks in advance to all!

Cheers, Luigi


I'm picking back this question again... I figured out on some platforms it doesn't really play sound... for example on another desktop PC with Chrome it doesn't play audio, neither on another tablet...I think it's due of browser's version... so is there a pure javascript way to just play an audio like a little mp3 or wav from code?..

Thanks in advance! Cheers Luigi

like image 852
Luigino Avatar asked Feb 14 '14 09:02

Luigino


1 Answers

Create an Audio object and play it! The link Audio takes in the constructor may be from any source.

var audio = new Audio('audio_file.mp3');
audio.play()
like image 58
poitroae Avatar answered Sep 30 '22 20:09

poitroae