6

In my web page, I would like to play sound when the current time equals to , for example, 5:00 pm.

What I did is:

<script type='text/css'>
var d = new Date();
var m = d.getMinutes();
var h = d.getHours();
if (h == 17 && m == 00){
document.write = "<embed src =\'notify.mp3\' hidden=\'true\' autostart=\'true\' loop=\'true\'></embed>";
}
</script>

I looked for a specific function that plays the sound and I found document.write which doesn't work and doesn't make sense.

Any suggestion what I should do

Nasser
  • 2,118
  • 6
  • 33
  • 56
  • 1
    I think you should first read something like that http://www.html5rocks.com/en/tutorials/webaudio/intro/ – thinklinux Apr 25 '13 at 08:17
  • http://stackoverflow.com/questions/8489710/how-can-i-play-sound-in-jquery-when-click-a-button Will be useful for you...... \ http://jsbin.com/ilijif/2 this is for click to play the sound you can go for the same thing on time matching – Ram Singh Apr 25 '13 at 08:18
  • 2
    You will need to check repeatedly if the current time is the time to play the music. Use setInterval to check the time. – Abbas Apr 25 '13 at 08:22

3 Answers3

4

Try this in JS:

 <script>
var d = new Date();
var m = d.getMinutes();
var h = d.getHours();
if (h == 17 && m == 00){
      var sound = document.getElementById(sound1);
      sound.Play();
}
</script>

Dont forget to add this in HTML

<embed src="notify.mp3" autostart="false" width="0" height="0" id="sound1"
enablejavascript="true">
Engineer
  • 5,911
  • 4
  • 31
  • 58
  • [This](http://stackoverflow.com/a/38571639/2218697) plays sounds if **browser is inactive or minimized** by the user. Hope helps someone. – Shaiju T Jul 25 '16 at 15:29
1

Try something like this.

document.getElementById("dummy").innerHTML= "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";

Using The Element

The tag defines a container for external (non-HTML) content. The following code fragment should play an MP3 file embedded in a web page:

Example

<embed height="50" width="100" src="horse.mp3">

Or try

Try using this revised version of the function play()

function play() 
{
  var embed=document.createElement('object');
  embed.setAttribute('type','audio/wav');
  embed.setAttribute('data', 'c:\test.wav');
  embed.setAttribute('autostart', true);
  document.getElementsByTagName('body')[0].appendChild(embed);
}

Refence: http://www.w3schools.com/html/html_sounds.asp

http://webdesign.about.com/od/sound/a/play_sound_oncl.htm

Playing sound with JavaScript

Community
  • 1
  • 1
Okky
  • 10,338
  • 15
  • 75
  • 122
0

I am assuming you are trying to create an alarm-like functionality. Here's what I would do:

var delay = 60000; // in milliseconds, check every minute
var intervalId = setInterval("playWhenReady()", delay);

function playWhenReady()
{
    var d = new Date();
    var h = d.getHours();
    var m = d.getMinutes(); 

    if (h === 17 && m === 00)
    {
        playSound('notify.mp3');
        clearInterval(intervalId);              
    }
}

function playSound(soundFile)
{
    var audioElement = document.createElement('audio');
    audioElement.setAttribute('src', soundFile);
    audioElement.play();
}
Abbas
  • 6,720
  • 4
  • 35
  • 49