Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过脚本的HTML音频控制_Html_Css_Audio_Web - Fatal编程技术网

通过脚本的HTML音频控制

通过脚本的HTML音频控制,html,css,audio,web,Html,Css,Audio,Web,我对HTML相当陌生,所以请容忍我。。。我正试图通过我网页上的脚本启动一个音频剪辑,但我很难弄清楚如何做到这一点。下面是我试图使用的HTML代码的子集: <body id="body"> <embed id="audio" src="song.wav" loop="true" hidden="true" autostart="true"> <script> ... if(state==1) {

我对HTML相当陌生,所以请容忍我。。。我正试图通过我网页上的脚本启动一个音频剪辑,但我很难弄清楚如何做到这一点。下面是我试图使用的HTML代码的子集:

<body id="body">
   <embed id="audio" src="song.wav" loop="true" hidden="true" autostart="true">
   <script>
      ...
      if(state==1)
      {
         document.getElementById("audio").autostart = "true"
      }
      else
      {
         document.getElementById("audio").autostart = "false"
      }
      ...
   </script>
</body>

...
如果(状态==1)
{
document.getElementById(“音频”).autostart=“true”
}
其他的
{
document.getElementById(“音频”).autostart=“false”
}
...

我的目标是根据脚本中变量的状态切换此音频。我上面所说的不起作用,但我感觉我就快到了。。。有什么想法吗?

试试这个。不要用双引号括住对和错

 if(state==1)
      {
         document.getElementById("audio").autostart = true;
      }
      else
      {
         document.getElementById("audio").autostart = false;
      }

应用true或false(不带引号)或赋值0或赋值1这很有意义,因为脚本的解释与html代码不同。不幸的是,这似乎不起作用,虽然。。。我在html代码中将autostart设置为“false”,在脚本中将autostart设置为true,但我的剪辑没有播放。也许有更好的方法解决这个问题吗?@DanSnyder您在浏览器控制台中看到任何错误吗?无论何时使用js或jquery,如果有什么东西不起作用,那就是您应该首先查找的地方。如果没有任何错误,请尝试简单的console.log或alert语句,以确保是否正在调用您的函数。谢谢您的指针!我没有看到任何错误,所以我登录到控制台,似乎正在解析我的条件语句。因此,我正在达到那个代码,它只是没有做我所期望的。也许这与使用嵌入标签与音频有关?我最终使用了不同的实现,但是我对结果不太满意。这在Chrome中有效,但在IE中不起作用,我不确定Safari:var snd=new Audio(“song.wav”);snd.play();有没有关于平台无关的解决方案的想法?