【问题标题】:.play(); not working for html5 audio。玩();不适用于 html5 音频
【发布时间】:2013-10-10 17:43:08
【问题描述】:

我一直在学习我的 javascript 技能,但我遇到了一个问题,即单击此处时没有播放音频是我的代码。

<div class="player_container">
<button class="play_pause_button" type="button" title="Play" onclick="play_pause"></button>
        <audio class="audio_player" preload="auto">
           <source src="audio/test.mp3" type="audio/mpeg">
        Your browser does not support the audio element.
    </audio>
</div>
    <script type="text/javascript">
    var content = document.getElementsByClassName('audio_player'),
    play_pause = document.getElementsByClassName('play_pause_button'),
    progress = document.getElementsByClassName('player_progress');
    function play_pause() {
        return content.play();
    }
    </script>

【问题讨论】:

    标签: javascript html audio


    【解决方案1】:

    document.getElementsByClassName('audio_player') 返回一个 HTMLCollection 对象,而不是单个节点。在向它发送命令之前,您需要指定集合中的哪个对象。

    return content.play(); 更改为return content[0].play(); 应该可以解决您的问题。

    【讨论】:

    • @user2498606 在 Firefox 中测试完整的脚本,您的函数名称中的下划线似乎有问题。去掉下划线,把onclick="play_pause"改成onclick="playpause()"
    猜你喜欢
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多