使用JQuery

时间:2017-02-16 08:08:57

标签: javascript jquery audio audiojs

Hiii Everyone,

这是我的音频播放的示例代码。它将在10秒后缓冲并开始播放,之后它将不再播放我尝试的场景中的相同音频。

<!DOCTYPE html>
    <html lang="en">
        <head>
        <meta charset="utf-8">
        <title>audio.js</title>
        <script src="./audiojs/audio.min.js"></script>
        <link rel="stylesheet" href="./includes/index.css" media="screen">
        <style>
            .play-pause {
                display: none;
            }
           .audiojs {
                width: 100%;
            }

        </style>
    </head>

    <body>
        <header>
            <h1>audio.js</h1>
        </header>

        <audio src="http://kolber.github.io/audiojs/demos/mp3/juicy.mp3" id="player"></audio>
        <label id="audio_stats"></label>
        <script>
            var element = document.getElementById("player");
            var settings = {
                autoplay: false,
                loop: false,
                preload: true,
                swfLocation: 'audiojs/audiojs.swf',
                trackEnded: function (e) {
                    document.getElementById("audio_stats").innerText = "Completed...";
                }
            }

            audiojs.events.ready(function () {
                var a = audiojs.create(element, settings);
                var count = 11;
                var counter = setInterval(timer, 1000);

                function timer() {
                    count = count - 1;
                    if (count <= 0) {
                        clearInterval(counter);
                        a.play();
                        document.getElementById("audio_stats").innerText = "Playing...";
                        return;
                    }
                    document.getElementById("audio_stats").innerText = "Will Start in " + count + " sec.";
                }
            });
        </script>
    </body>
    </html>

这是参考https://kolber.github.io/audiojs/在我的网站中,我有近17个音频。每个div都包含单个音频。第一个div是带有id“播放器(意味着第一个音频)”的音频,类似于第二个div“player1(意味着第二个音频)”音频将在那里。对于第一个div我将有一个带有“下一个问题”的按钮同样适用于所有17个div将有上一个和下一个按钮。问题是所有17个音频都被缓冲并同时播放而“播放器(意味着第一个音频)”将在页面打开后启动缓冲区。当我点击下一个按钮时,第二个音频应该启动缓冲区。如果我按下前一个按钮“播放器(意味着第一音频)”不应再播放,因为只有一次它应该播放。同样它适用于所有音频。如果有人给我解决这个问题。它会更有帮助。我长期在这个问题上苦苦挣扎。请帮助我。谢谢你。

1 个答案:

答案 0 :(得分:1)

根据您的要求检查以下代码。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>audio.js</title>
    <script src="./audiojs/audio.min.js"></script>
    <link rel="stylesheet" href="./includes/index.css" media="screen">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <style>
        .play-pause {
            display: none;
        }

        .scrubber {
            display: none;
        }

        .audiojs {
            width: 110px;
        }

        .audiojs .time {
            border-left: none;
        }
    </style>
</head>

<body>
    <header>
        <h1>audio.js</h1>
    </header>

    <audio src="http://kolber.github.io/audiojs/demos/mp3/juicy.mp3" id="player"></audio>
    <label id="audio_stats"></label>
    <br/>
    <button id="next" disabled>Next</button>

    <ol style="display:none;">
        <li class=""><a href="#" data-src="http://kolber.github.io/audiojs/demos/mp3/01-dead-wrong-intro.mp3">dead wrong intro</a></li>
        <li class=""><a href="#" data-src="http://kolber.github.io/audiojs/demos/mp3/02-juicy-r.mp3">juicy-r</a></li>
        <li class=""><a href="#" data-src="http://kolber.github.io/audiojs/demos/mp3/03-its-all-about-the-crystalizabeths.mp3">it's all about the crystalizabeths</a></li>
        <li class=""><a href="#" data-src="http://kolber.github.io/audiojs/demos/mp3/04-islands-is-the-limit.mp3">islands is the limit</a></li>
        <li class=""><a href="#" data-src="http://kolber.github.io/audiojs/demos/mp3/05-one-more-chance-for-a-heart-to-skip-a-beat.mp3">one more chance for a heart to skip a beat</a></li>
        <li><a href="#" data-src="http://kolber.github.io/audiojs/demos/mp3/06-suicidal-fantasy.mp3">suicidal fantasy</a></li>
        <li><a href="#" data-src="http://kolber.github.io/audiojs/demos/mp3/07-everyday-shelter.mp3">everyday shelter</a></li>
        <li><a href="#" data-src="http://kolber.github.io/audiojs/demos/mp3/08-basic-hypnosis.mp3">basic hypnosis</a></li>
        <li><a href="#" data-src="http://kolber.github.io/audiojs/demos/mp3/09-infinite-victory.mp3">infinite victory</a></li>
        <li><a href="#" data-src="http://kolber.github.io/audiojs/demos/mp3/10-the-curious-incident-of-big-poppa-in-the-nighttime.mp3">the curious incident of big poppa in the nighttime</a></li>
        <li><a href="#" data-src="http://kolber.github.io/audiojs/demos/mp3/11-mo-stars-mo-problems.mp3">mo stars mo problems</a></li>
    </ol>

    <script>
        var element = document.getElementById("player");
        var settings = {
            autoplay: false,
            loop: false,
            preload: true,
            swfLocation: 'audiojs/audiojs.swf',
            trackEnded: function(e) {
                document.getElementById("audio_stats").innerText = "Track Ended...";
                var next = $('ol li.playing').next();
                if (!next.length) next = $('ol li').first();
                next.addClass('playing').siblings().removeClass('playing');
                audio.load($('a', next).attr('data-src'));
                audio.play();
            }
        }

        audiojs.events.ready(function() {
            var a = audiojs.createAll(settings);
            var count = 11;
            var counter = setInterval(timer, 1000);

            function timer() {
                count = count - 1;
                if (count <= 0) {
                    clearInterval(counter);
                    audio.play();
                    document.getElementById("audio_stats").innerText = "Playing..." + audio.mp3;
                    $('#next').removeAttr('disabled');
                    return;
                }
                document.getElementById("audio_stats").innerText = "Will Start in " + count + " sec.";
            }

            // Load in the first track
            var audio = a[0];
            first = $('ol a').attr('data-src');
            $('ol li').first().addClass('playing');
            audio.load(first);

            // Load in a track on click
            $('ol li').click(function(e) {
                e.preventDefault();
                $(this).addClass('playing').siblings().removeClass('playing');
                audio.load($('a', this).attr('data-src'));
                audio.play();
            });
            // Keyboard shortcuts
            $(document).keydown(function(e) {
                var unicode = e.charCode ? e.charCode : e.keyCode;
                // right arrow
                if (unicode == 39) {
                    var next = $('li.playing').next();
                    if (!next.length) next = $('ol li').first();
                    next.click();
                    // back arrow
                } else if (unicode == 37) {
                    //var prev = $('li.playing').prev();
                    //if (!prev.length) prev = $('ol li').last();
                    //prev.click();
                    // spacebar
                } else if (unicode == 32) {
                    //audio.playPause();
                }
            });

            $("#next").click(function() {
                var next = $('li.playing').next();
                if (!next.length) next = $('ol li').first();
                next.click();
                document.getElementById("audio_stats").innerText = "Playing..." + audio.mp3;
            });

        });
    </script>
</body>

</html>