Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play sound when the current time equals to specific time

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

like image 831
Nasser Avatar asked Apr 25 '13 08:04

Nasser


2 Answers

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">
like image 84
Sankalp Mishra Avatar answered Nov 09 '22 19:11

Sankalp Mishra


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

like image 1
Okky Avatar answered Nov 09 '22 18:11

Okky