简体   繁体   English

AJAX并播放一次声音

[英]AJAX and playing a sound once

Alright, so here's the issue. 好了,这就是问题所在。 We have a website which displays status updates throughout our various facilities (they all have LCDs with a fullscreen IE page I point it to). 我们有一个网站,其中显示了我们各种设施的状态更新(它们都有LCD屏,我指向它是全屏IE页面)。 Right now I go to my management page with a WYSIWYG-editor, type in the messages/updates, which saves it to a .txt file (alert.txt) and it updates the page on the screens via AJAX. 现在,我进入带有所见即所得编辑器的管理页面,输入消息/更新,将其保存到.txt文件(alert.txt),并通过AJAX更新屏幕上的页面。 The page on the screens checks for updates at a set interval of 5 seconds. 屏幕上的页面每隔5秒检查一次更新。

What I want to do now is play a sound to alert when an update is given (the screen only displays critical network errors and such) 我现在要做的是在发出更新时播放声音以发出警报(屏幕仅显示严重的网络错误等)

I use 我用

<body onload="window.setTimeout('loadXMLDoc()',1000);">

which calls to this: 调用此:

<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
  document.getElementById("changes").innerHTML=xmlhttp.responseText;
  }
}
xmlhttp.open("GET","alerts.txt",true);
xmlhttp.send();
}
</script>

I've tried adding the sound clip via an embed HTML code (created a Flash object that plays it once upon loading the Flash object) to the text file, but this causes it to play once every iteration. 我尝试通过嵌入HTML代码(创建一个在加载Flash对象时播放一次的Flash对象)将声音剪辑添加到文本文件中,但这会导致它每次迭代播放一次。 Even if there are no changes. 即使没有变化。

Halp?

Instead of adding the flash into the alerts.txt create a new DOM object once the changes div has been updated : changes div更新后,不要将闪存添加到alerts.txt创建一个新的DOM对象:

if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
  document.getElementById("changes").innerHTML=xmlhttp.responseText;
  // Add Flash object to DOM
   var emb = document.createElement("embed");
   emb.setAttribute("type","application/x-shockwave-flash");   
   emb.setAttribute("autostart","true"); 
   emb.setAttribute("data","<your url>");
  }
}

You will need to change the URL to match the URL of your sound flash file and add any attributes you need to set 您将需要更改URL以使其与声音Flash文件的URL相匹配,并添加需要设置的所有属性

There is a problem with this approach in that the object will get created each time .... so perhaps you could change remove the element first then re-add it. 这种方法存在一个问题,即每次都会创建对象。因此,也许您可​​以先更改删除元素,然后重新添加它。

Also check out this answer -> Playing dynamically embedded sound object via Javascript 还请查看此答案-> 通过Javascript播放动态嵌入的声音对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM