【问题标题】:Audio Not Playing - JavaScript音频未播放 - JavaScript
【发布时间】:2013-06-25 22:20:07
【问题描述】:

我正在尝试设置客户的网站以在页面加载时播放音频文件(根据他的要求,这不是我的首选功能)。我使用了一些简单的 JavaScript,但页面加载时没有播放音频。任何想法为什么会发生这种情况?

<head>
<!--other metadata up here-->
    <script>
function EvalSound(soundobj) {
      var thissound = document.getElementById(soundobj);
      thissound.Play();
    }
    </script>

    <embed src="scripts/audio.wav" autostart=true width=1 height=1 id="audio"
    enablejavascript="true">
</head>
<body>
  <div id="container" onload="EvalSound('audio')">
<!--rest of page>
</body>

【问题讨论】:

    标签: javascript html audio-streaming


    【解决方案1】:

    HTML5解决方案,(支持IE9+和其他所有合适的浏览器)

    <audio src="audio.mp3" preload="auto" controls></audio>
    
    <script>
        var getAudio = document.getElementsByTagName("audio")[0];
        getAudio.play();
    </script>
    

    自动播放音频而无法阻止它是一个可怕的想法。

    【讨论】:

    • 在 HTML 中放置这个的最佳位置是什么?
    • audio标签放在body的某处,然后Javascript放在底部,靠近关闭的body标签。
    【解决方案2】:

    Div 没有加载事件。你应该把它贴在身体上。

    【讨论】:

    • 我已经尝试过这样做,并且还使用 onload 事件添加了一个空表单,但无济于事。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 2013-09-20
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多