7

我正在使用 UbaPlayer,它是一个带有 Flash 后备的 jQuery HTML5 音频播放器。目前,它只是在被提示的特定歌曲完成后停止。

我希望它自动播放按时间顺序列出的下一个“音频按钮”类。

@Winner_joiner 已设法编辑第 190 行以移至下一首歌曲,<ul>但在最后一首歌曲<ul>完成后,它转到 的第一个实例<ul class=controls>,而我希望它继续到<ul>.

我有一些歌曲之间的内容,所以我必须打破<ul>并为其他歌曲重新开始。

这是我的 HTML:

<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="src/jquery.ubaplayer.js"></script>
<script>
jQuery(document).ready(function() { 
   jQuery.noConflict();
   jQuery(function(){
            jQuery("#ubaPlayer").ubaPlayer({
            codecs: [{name:"MP3", codec: 'audio/mpeg;'}]
            });

    });
    jQuery('a[rel=vidbox]').click(function () {

        if (jQuery("#ubaPlayer").ubaPlayer("playing") === true) {
            jQuery("#ubaPlayer").ubaPlayer("pause");
            }
        return false;
    });
})
</script>
</head>

<body>
<div id="ubaPlayer"></div>
    <ul class="controls">
        <li><a class="audioButton" href="mp3/dontthinktwice.mp3">
        Don't Think Twice (Bob Dylan)</a></li>
        <li><a class="audioButton" href="mp3/livingforthecity.mp3">
        Living for the City (Stevie Wonder)</a></li>
    </ul>

    <!-- More content and stuff here-->

    <ul class="controls">
        <li><a class="audioButton" href="mp3/anothersong.mp3">
        Another Song</a></li>
        <li><a class="audioButton" href="mp3/anothersong2.mp3">
        Another Song 2</a></li>
    </ul>
</body>

音频播放器 JavaScript:

(function($) {
var defaults = {
    audioButtonClass: "audioButton",
    autoPlay: null,
    codecs: [{
        name: "OGG",
        codec: 'audio/ogg; codecs="vorbis"'
    }, {
        name: "MP3",
        codec: 'audio/mpeg'
    }],
    continuous: false,
    extension: null,
    flashAudioPlayerPath: "libs/swf/player.swf",
    flashExtension: ".mp3",
    flashObjectID: "audioPlayer",
    loadingClass: "loading",
    loop: false,
    playerContainer: "player",
    playingClass: "playing",
    swfobjectPath: "libs/swfobject/swfobject.js",
    volume: 1
},
    currentTrack, isPlaying = false,
    isFlash = false,
    audio, $buttons, $tgt, $el, playTrack, resumeTrack, pauseTrack, methods = {
        play: function(element) {
            $tgt = element;
            currentTrack = _methods.getFileNameWithoutExtension($tgt.attr("href"));
            isPlaying = true;
            $tgt.addClass(defaults.loadingClass);
            $buttons.removeClass(defaults.playingClass);

            if (isFlash) {
                if (audio) {
                    _methods.removeListeners(window);
                }
                audio = document.getElementById(defaults.flashObjectID);
                _methods.addListeners(window);
                audio.playFlash(currentTrack + defaults.extension);
            } else {
                if (audio) {
                    audio.pause();
                    _methods.removeListeners(audio);
                }
                audio = new Audio("");
                _methods.addListeners(audio);
                audio.id = "audio";
                audio.loop = defaults.loop ? "loop" : "";
                audio.volume = defaults.volume;
                audio.src = currentTrack + defaults.extension;
                audio.play();
            }
        },

        pause: function() {
            if (isFlash) {
                audio.pauseFlash();
            } else {
                audio.pause();
            }

            $tgt.removeClass(defaults.playingClass);
            isPlaying = false;
        },

        resume: function() {
            if (isFlash) {
                audio.playFlash();
            } else {
                audio.play();
            }
            $tgt.addClass(defaults.playingClass);
            isPlaying = true;
        },

        playing: function() {
            return isPlaying;
        }
    },

    _methods = {
        init: function(options) {
            var types;

            //set defaults
            $.extend(defaults, options);
            $el = this;

            //listen for clicks on the controls
            $(".controls").bind("click", function(event) {
                _methods.updateTrackState(event);
                return false;
            });
            $buttons = $("." + defaults.audioButtonClass);

            types = defaults.codecs;
            for (var i = 0, ilen = types.length; i < ilen; i++) {
                var type = types[i];
                if (_methods.canPlay(type)) {
                    defaults.extension = [".", type.name.toLowerCase()].join("");
                    break;
                }
            }

            if (!defaults.extension || isFlash) {
                isFlash = true;
                defaults.extension = defaults.flashExtension;
            }

            if (isFlash) {
                $el.html("<div id='" + defaults.playerContainer + "'/>");
                $.getScript(defaults.swfobjectPath, function() {
                    swfobject.embedSWF(defaults.flashAudioPlayerPath, defaults.playerContainer, "0", "0", "9.0.0", "swf/expressInstall.swf", false, false, {
                        id: defaults.flashObjectID
                    }, _methods.swfLoaded);
                });
            } else {
                if (defaults.autoPlay) {
                    methods.play(defaults.autoPlay);
                }
            }
        },

        updateTrackState: function(evt) {
            $tgt = $(evt.target);
            if (!$tgt.hasClass("audioButton")) {
                return;
            }
            if (!audio || (audio && currentTrack !== _methods.getFileNameWithoutExtension($tgt.attr("href")))) {
                methods.play($tgt);
            } else if (!isPlaying) {
                methods.resume();
            } else {
                methods.pause();
            }
        },

        addListeners: function(elem) {
            $(elem).bind({
                "canplay": _methods.onLoaded,
                "error": _methods.onError,
                "ended": _methods.onEnded
            });
        },

        removeListeners: function(elem) {
            $(elem).unbind({
                "canplay": _methods.onLoaded,
                "error": _methods.onError,
                "ended": _methods.onEnded
            });
        },

        onLoaded: function() {
            $buttons.removeClass(defaults.loadingClass);
            $tgt.addClass(defaults.playingClass);

            audio.play();
        },

        onError: function() {
            $buttons.removeClass(defaults.loadingClass);
            if (isFlash) {
                _methods.removeListeners(window);
            } else {
                _methods.removeListeners(audio);
            }
        },

        onEnded: function() {
            isPlaying = false;
            $tgt.removeClass(defaults.playingClass);
            currentTrack = "";
            if (isFlash) {
                _methods.removeListeners(window);
            } else {
                _methods.removeListeners(audio);
            }

            if (defaults.continuous) {
                var $next = $tgt.next().length ? $tgt.next() : $(defaults.audioButtonClass).eq(0);
                methods.play($next);
            }

        },

        canPlay: function(type) {
            if (!document.createElement("audio").canPlayType) {
                return false;
            } else {
                return document.createElement("audio").canPlayType(type.codec).match(/maybe|probably/i) ? true : false;
            }
        },

        swfLoaded: function() {
            if (defaults.autoPlay) {
                setTimeout(function() {
                    methods.play(defaults.autoPlay);
                }, 500);
            }
        },

        getFileNameWithoutExtension: function(fileName) {
            //this function take a full file name and returns an extensionless file name
            //ex. entering foo.mp3 returns foo
            //ex. entering foo returns foo (no change)
            var fileNamePieces = fileName.split('.');
            fileNamePieces.pop();
            return fileNamePieces.join(".");
        }
    };

$.fn.ubaPlayer = function(method) {
    if (methods[method]) {
        return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
    } else if (typeof method === "object" || !method) {
        return _methods.init.apply(this, arguments);
    } else {
        $.error("Method " + method + " does not exist on jQuery.ubaPlayer");
    }
};})(jQuery);
4

1 回答 1

3

这似乎是 lib 中的一个错误(或未记录的功能)。

当音频按钮是直接兄弟时才有效(在 jquery.ubaplayer.js 的第 190 行中证明)

var $next = $tgt.next().length ? $tgt.next() : $(defaults.audioButtonClass).eq(0);
// a other unrelated bug in the same line is ... $(defaults.audioButtonClass) should be $("." + defaults.audioButtonClass) or so

换句话说,如果您的 HTML 看起来像这样(连续:true):

 <div class="controls"> <!-- THIS IS NOW AN DIV NOT UL-->
     <!-- NO LI'S since the links have to be direkt siblings-->
        <a class="audioButton" href="mp3/dontthinktwice.mp3">
        Don't Think Twice (Bob Dylan)</a>
        <a class="audioButton" href="mp3/livingforthecity.mp3">
        Living for the City (Stevie Wonder)</a>
 </div>

它可以工作(使用 chrome 25+)

我希望这有帮助。

更新1:

如果要使用HTML,则必须将文件 jquery.ubaplayer.js 中的第 190 行更新为

var $next = $tgt.parent().next().length>0 && $tgt.parent().next().children().length>0?$tgt.parent().next().children().eq(0):$("."+defaults.audioButtonClass).eq(0);
// Code could/should be cleaned up a bit

此代码获取 a-Tag = li-Tag 的父级,检查它是否有兄弟姐妹,如果有,则获取正确的音频 href。

警告我刚刚检查了代码是否运行,对于您的问题,我不知道它是否会在其他使用场景中引起问题。(顺便说一句。我只会更改 HTML 并将 LIB 保持原样,因此如果有更新,则不会出现中断更改)

更新 2:

使用自动播放调用 Ubaplayer 的代码

 jQuery("#ubaPlayer").ubaPlayer({
        codecs: [{name:"MP3", codec: 'audio/mpeg;'}],
        autoPlay:$(".audioButton").eq(0)
 });
于 2013-03-14T14:54:13.197 回答