Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.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和JavaScript在点击按钮时播放音频文件?_Javascript_Html - Fatal编程技术网

如何使用HTML和JavaScript在点击按钮时播放音频文件?

如何使用HTML和JavaScript在点击按钮时播放音频文件?,javascript,html,Javascript,Html,此代码仅返回音量图标和链接属性,不播放音频文件: 函数playSound(){ var sound=document.getElementById(“音频”); 声音。播放(); } 我的名字是 巴西亚特祖拜尔 如果您为的src属性分配了正确的URL,它将起作用,例如 函数playSound(){ var sound=document.getElementById(“音频”); 声音。播放(); } 我的名字是 巴西亚特祖拜尔 您的浏览器不支持音频元素。 试试这个,它可能对你有用 <

此代码仅返回音量图标和链接属性,不播放音频文件:

函数playSound(){
var sound=document.getElementById(“音频”);
声音。播放();
}

我的名字是
巴西亚特祖拜尔

如果您为的
src
属性分配了正确的URL,它将起作用,例如

函数playSound(){
var sound=document.getElementById(“音频”);
声音。播放();
}

我的名字是
巴西亚特祖拜尔
您的浏览器不支持
音频
元素。
试试这个,它可能对你有用

<!DOCTYPE html>
<html>
<body>
<audio id="myAudio">
  <source src="horse.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>
<p>Click the buttons to play or pause the audio.</p>
<button onclick="playAudio()" type="button">Play Audio</button>
<button onclick="pauseAudio()" type="button">Pause Audio</button>
<script>
    var x = document.getElementById("myAudio"); 

    function playAudio() { 
      x.play(); 
    } 

    function pauseAudio() { 
     x.pause(); 
    }
</script> 
</body>
</html>

您的浏览器不支持音频元素。
单击按钮播放或暂停音频

播放音频 暂停音频 var x=document.getElementById(“myAudio”); 函数playAudio(){ x、 play(); } 函数pauseAudio(){ x、 暂停(); }
错误是什么?如果有正确的音频,它工作得很好。是的,它工作得很好。但是我想要音频当我点击扬声器图标时,你的代码与“controls=“@BasiyathTZubair”一起工作。这是一样的,我已经删除了
controls
属性。