【问题标题】:playing audio when button clicked and then stops and new audio plays when different button clicked单击按钮时播放音频,然后在单击不同按钮时停止播放新音频
【发布时间】:2014-02-21 10:25:59
【问题描述】:

当用户单击按钮时,它会播放音频,但当他们单击另一个按钮时,音频会停止,然后它会播放该按钮的音频,依此类推。在页面加载的那一刻,我也有自动播放的背景音乐,我试图弄清楚如何在按下新按钮时停止音乐播放,然后音乐播放为当前它们重叠。任何帮助将不胜感激。

关于加载背景音乐代码:

<audio autoplay="autoplay" class="bondAudio">
     <source src="audio/Bond.mp3" type="audio/mpeg">
     Your browser does not support the audio element.
</audio>

按钮点击音频播放代码:

<script>
    $(document).ready(function() {
        var audioElement = document.createElement('audio');
        audioElement.setAttribute('src', 'audio/Dr-No.mp3');
        audioElement.setAttribute('play', 'play');
        //audioElement.load()

        $.get();

        audioElement.addEventListener("load", function() {
            audioElement.play();
        }, true);

        $('#blockLink1').click(function() {
            audioElement.play();
        });
    });
</script>  

【问题讨论】:

标签: javascript jquery html audio


【解决方案1】:

你有 jQuery 为什么还要用纯 js 创建元素?

<script>
    $(document).ready(function() {
        var audioElement = $('<audio></audio>');

        audioElement.attr('src', 'audio/Dr-No.mp3');
        audioElement.attr('play', 'play');
        //audioElement.load()

        $.get(); // ?

        audioElement.on("load", function() {
            audioElement.get(0).play();
        }, true);

        $('#blockLink1').click(function() {
            $('audio').each(function() {
                this.stop();
            });
            audioElement.get(0).play();
        });
    });
</script>  

【讨论】:

  • 我对 jQuery 和整个开发都很陌生,所以我的编码不会那么好。谢谢回答。有什么应该进入音频标签的,因为我尝试了代码但音频没有播放
  • 因为 html5 视频和音频标签可以有 s 作为孩子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多