首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么我的JavaScript代码不播放音乐?

为什么我的JavaScript代码不播放音乐?
EN

Stack Overflow用户
提问于 2019-06-13 04:09:02
回答 1查看 41关注 0票数 0

我想让我网站上的按钮(主页、关于、博客)在有人点击时播放一些声音或音乐。现在我试了又试,但是找不到javascript代码中的错误。

代码语言:javascript
复制
<body onload="htmlIsReady()">
<script type="text/javascript">


var audio;

var audioLink;


window.onmessage = function(event)  {
if (event.data.data) {
//Passes the audio url to the new variable
    audioLink = event.data.data;
//Created a new audio object with the received audio url        
audio = new Audio('URL of AUDIO-FILE');         
                }
            };


function htmlIsReady() {
window.parent.postMessage("html_is_ready", "*");  
 }  

//Funcion that plays the audio
function playAudio() {
 audio.play.play();   
 }

</script>

 // Button that executes the function that plays the audio everytime it's 
 clicked 
 <button class="button" onclick="playAudio()">Play</button>

 </body>
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56569681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档