mmaynar1
mmaynar1

Reputation: 356

How can I play a notification wav file in the browser?

I want play a success/failure sound depending upon the result of a user's action. The problem I'm having is that a visible Windows Media Player dialog is being launched to play the sound. Any solutions for playing the sound without launching the dialog? I need to be able to run in IE8 and above.

Current code:

HTML

<a id="goodSoundId" style="display: none">good</a>
<a id="badSoundId" style="display: none">bad</a>

JS

$( document ).ready( function()
{
  $('#goodSoundId' ).attr('href',"sound/alert/" + page.goodSound + ".WAV");
  $('#badSoundId' ).attr('href',"sound/alert/" + page.badSound + ".WAV");
}

var object =
{
  playGoodSound: function()
  {
     $('a#goodSoundId')[0].click();
  },
  playBadSound: function()
  {
     $('a#badSoundId')[0].click();
  }
}

Upvotes: 1

Views: 267

Answers (1)

starsky135
starsky135

Reputation: 7

You can simply put a JavaScript play inside a function and call it when required, like this:

function PlayDing(){
    var audio1 = new Audio('correct.mp3');
    audio1.play();
}

function PlayCross(){
    var audio2 = new Audio('incorrect.mp3');
    audio2.play();
}

Upvotes: 1

Related Questions