Javascript:如何暂停/停止当前播放的音频?

Javascript:如何暂停/停止当前播放的音频?,javascript,jquery,audio,mp3,html5-audio,Javascript,Jquery,Audio,Mp3,Html5 Audio,我有以下代码在我的网站上播放音频。问题在于播放音频时不会停止或暂停播放新的音频。所有的文件都在一起玩得很疯狂 $().ready(function () { PlayMp3('sounds/file1.mp3'); PlayMp3('sounds/file2.mp3'); PlayMp3('sounds/file3.mp3'); PlayMp3('sounds/file4.mp3'); }); function PlayMp3(file) {

我有以下代码在我的网站上播放音频。问题在于播放音频时不会停止或暂停播放新的音频。所有的文件都在一起玩得很疯狂

$().ready(function () {
    PlayMp3('sounds/file1.mp3');
    PlayMp3('sounds/file2.mp3');
    PlayMp3('sounds/file3.mp3');
    PlayMp3('sounds/file4.mp3');
});
    function PlayMp3(file) {
            var isiPad = navigator.userAgent.match(/iPad/i) != null;
            var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
            var isiPod = navigator.userAgent.match(/iPod/i) != null;
            pauseAllAudio();

            if (navigator.userAgent.indexOf("Firefox") != -1) {
                file = file.replace("mp3", "ogg");            
                $('#content').append('<audio src="' + file + '" controls preload="auto" autobuffer autoplay="autoplay" style="display:none"></audio>');
            }
            else if (isiPad || isiPhone || isiPod) {           
                var snd = new Audio(file);
                snd.load();
                snd.play();            
            }
            else {
                $('#content').append('<embed height="0" width="0" src="' + file + '">');
            }


        }

        function pauseAllAudio() {
            $('video, audio').each(function () {
                $(this)[0].pause();
            });
            var allAudioEls = $('audio');
            allAudioEls.each(function () {
                var a = $(this).get(0);
                a.pause();
            });

            $('audio').remove();
            if (snd != undefined) {
                snd.pause();
            }
        }
$().ready(函数(){
播放mp3(“声音/文件1.mp3”);
播放mp3(“声音/文件2.mp3”);
播放mp3(“声音/文件3.mp3”);
播放mp3(“声音/文件4.mp3”);
});
功能播放MP3(文件){
var isiPad=navigator.userAgent.match(/iPad/i)!=null;
var isiPhone=navigator.userAgent.match(/iPhone/i)!=null;
var isiPod=navigator.userAgent.match(/iPod/i)!=null;
pausealaudio();
if(navigator.userAgent.indexOf(“Firefox”)!=-1){
file=file.replace(“mp3”、“ogg”);
$(“#内容”)。追加(“”);
}
else如果(isiPad | | isiPhone | | isiPod){
var snd=新音频(文件);
snd.load();
snd.play();
}
否则{
$(“#内容”)。追加(“”);
}
}
函数pauseAllAudio(){
$('video,audio')。每个(函数(){
$(此)[0]。暂停();
});
var allAudioEls=$('audio');
Allaudiols.each(函数(){
var a=$(this.get)(0);
a、 暂停();
});
$('audio').remove();
如果(snd!=未定义){
暂停();
}
}
这不是一个重复的问题。我在这里尝试过类似问题的所有解决方案,但没有一个对我有效。谢谢。

零钱

$('video, audio').each(function () {
    $(this).get(0).stop();
});
说它是否有效

更新#1 检查以下各项是否正常工作:

  • $(this)[0]。暂停()
  • $(this.get(0.pause()
  • 零钱

    $('video, audio').each(function () {
        $(this).get(0).stop();
    });
    
    说它是否有效

    更新#1 检查以下各项是否正常工作:

  • $(this)[0]。暂停()
  • $(this.get(0.pause()

  • 多亏了普拉文·库马尔。没有他,我不可能发现删除“嵌入”的问题。添加
    $('embed').remove()后
    函数pauseAllAudio()
    。它解决了除Firefox以外的所有浏览器的问题。firefox不喜欢mp3或
    标签。我必须做更多的研究,使它只适用于那个浏览器

    jPlayer看起来很棒,它解决了我的问题。解决办法如下

     function PlayMp3(file) {
        $('embed').remove();
        if (navigator.userAgent.indexOf("Firefox") != -1) {
                file = file.replace("mp3", "ogg");
                $('#jquery_jplayer').jPlayer("pauseOthers");
                $('#jquery_jplayer').jPlayer("setMedia", { oga: file });
                $('#jquery_jplayer').jPlayer("play");
        }
        else {
            $('#content').append('<embed height="0" width="0" src="' + file + '">');
    }
    
    功能播放MP3(文件){
    $('embed').remove();
    if(navigator.userAgent.indexOf(“Firefox”)!=-1){
    file=file.replace(“mp3”、“ogg”);
    $('jqueryjplayer').jplayer(“pauseOthers”);
    $('jqueryjplayer').jplayer(“setMedia”,{oga:file});
    $('jqueryjplayer').jplayer(“play”);
    }
    否则{
    $(“#内容”)。追加(“”);
    }
    

    我不得不放弃HTML 5
    标记。:(

    多亏了Praveen Kumar。没有他,我不可能发现删除“嵌入”的问题。在添加
    $('embed').remove()之后;
    函数pauseAllAudio()
    。它解决了除Firefox之外的所有浏览器的问题。Firefox不喜欢mp3或
    标签。我必须做更多的研究,使其仅适用于该浏览器

    jPlayer看起来很棒,它解决了我的问题

     function PlayMp3(file) {
        $('embed').remove();
        if (navigator.userAgent.indexOf("Firefox") != -1) {
                file = file.replace("mp3", "ogg");
                $('#jquery_jplayer').jPlayer("pauseOthers");
                $('#jquery_jplayer').jPlayer("setMedia", { oga: file });
                $('#jquery_jplayer').jPlayer("play");
        }
        else {
            $('#content').append('<embed height="0" width="0" src="' + file + '">');
    }
    
    功能播放MP3(文件){
    $('embed').remove();
    if(navigator.userAgent.indexOf(“Firefox”)!=-1){
    file=file.replace(“mp3”、“ogg”);
    $('jqueryjplayer').jplayer(“pauseOthers”);
    $('jqueryjplayer').jplayer(“setMedia”,{oga:file});
    $('jqueryjplayer').jplayer(“play”);
    }
    否则{
    $(“#内容”)。追加(“”);
    }
    

    我不得不放弃HTML 5
    标记。:(

    它不起作用。我现在甚至得到了脚本错误“PM error:TypeError:$(this).get(0).stop不是函数源文件:“@jonat更新了答案。检查两个条件!:)我已经尝试了两个更新的答案。它们现在都没有出现脚本错误,但也没有暂停:(我已经用我正在使用的网站更新了我的问题。你可以很快地尝试这个测试,并会看到声音一起播放得很糟糕。好的。我会检查并让你知道。很好。我在其他浏览器上使用过。我添加了$('embed')。remove();它在chrome中工作。需要修复才能在Firefox中工作。它不工作。我现在甚至收到了脚本错误“PM error:TypeError:$(this)。get(0)。stop不是函数源文件:“@JonasT更新了答案。检查两个条件!:)我已经尝试了两个更新的答案。它们现在都没有出现脚本错误,但也没有暂停:(我已经用我正在使用的网站更新了我的问题。你可以很快地尝试这个测试,并会看到声音一起播放得很糟糕。好的。我会检查并让你知道。好家伙。我曾用于其他浏览器。我添加了$('embed').remove();它在chrome中工作。需要修复才能在Firefox中工作。