2013-04-25 119 views
5

在我的网页播放声音,我想播放声音时的当前时间等于,例如,下午5:00。如何在当前时间等于特定的时间

我所做的是:

<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> 

我寻找时播放的声音特定的功能,我发现document.write不工作,没有任何意义。

任何建议,我应该怎么办

+1

我觉得你应该先阅读类似的东西http://www.html5rocks.com/en/tutorials/webaudio/intro/ – thinklinux 2013-04-25 08:17:47

+0

http://stackoverflow.com/questions/ 8489710 /如何在播放声音在jquery - 当点击一个按钮 将对您有用...... \ http://jsbin.com/ilijif/ 2这是点击播放,你可以去上一次同样的事情匹配 – 2013-04-25 08:18:25

+1

您将需要反复检查,如果当前的时间是玩音乐的时候声音。使用setInterval来检查时间。 – Abbas 2013-04-25 08:22:04

回答

2

在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> 

不要忘记在HTML中添加此

<embed src="notify.mp3" autostart="false" width="0" height="0" id="sound1" 
enablejavascript="true"> 
+0

[这](http://stackoverflow.com/a/38571639/2218697)播放声音如果**浏览器是不活动的或最小化**由用户。希望有助于某人。 – stom 2016-07-25 15:29:02

1

尝试这样的事情。

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

使用该元件

标签定义为外部(非HTML)内容的容器。下面的代码片段应发挥嵌入在网页中的MP3文件:

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

或者尝试

尝试使用的功能发挥()这个修订版

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

0

我假设你要创建一个报警状功能。这是我会做:

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(); 
} 
相关问题