1

私のウェブサイトでオーディオを再生するための次のコードがあります。問題は、オーディオの再生が停止または一時停止して新しいオーディオを再生しないことです。すべてのファイルが狂ったように一緒に再生されています。

$().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();
            }
        }

これは重複した質問ではありません。私はここで同様の質問からすべての解決策を試しましたが、どの解決策も私にはうまくいきません。ありがとう。

4

2 に答える 2

1

小さな変化。

$('video, audio').each(function () {
    $(this).get(0).stop();
});

それが機能しているかどうかを言います。

アップデート#1

以下が機能しているかどうかを確認します。

  1. $(this)[0].pause();
  2. $(this).get(0).pause();
于 2012-10-13T02:46:19.553 に答える
0

プラヴィーンクマールに感謝します。彼がいなければ、「埋め込み」を削除する問題を見つけることができませんでした。に追加$('embed').remove();した後function pauseAllAudio()。Firefox以外のすべてのブラウザで解決します。<embed>その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 + '">');
}

<audio>HTML5タグをあきらめなければなりませんでした。:(

于 2012-10-13T10:55:58.550 に答える