添加歌曲到Jquery JAudio播放列表而不刷新播放器

时间:2016-07-26 10:13:19

标签: javascript jquery html

我正在为娱乐网站制作一个自定义MP3播放器,即使在播放歌曲时我也需要不断添加歌曲。要做到这一点我使用这个功能。

var temp = [];
var tempObj = [];

function popUp(file,thumb,trackName,trackArtist,trackAlbum) {

    var validate = true;
    if (temp.length>0) {
        for (var x = 0; x < temp.length; x++) {
            if (temp[x]['trackName'] == trackName) {
                validate = false;
            }
        }
    }
    if (validate==true){

        // Save data to object
        tempObj = { file: file, thumb: thumb, trackName: trackName, trackArtist: trackArtist, trackAlbum: trackAlbum };
        temp.push(tempObj); // push object to existing array
        $("#player").jAudio({playlist: temp});

    }
}

问题是,要向玩家添加歌曲,我们需要运行#34; jAudio()&#34;功能。因为每次&#34; popUp()&#34;函数调用它调用&#34; jAudio()&#34;功能。如果有人有解决方案,请剪它..

这是JAudio API。

!function (t) {
    function i(i, a) {
        this.settings = t.extend(!0, r, a), this.$context = i, this.domAudio = this.$context.find("audio")[0], this.$domPlaylist = this.$context.find(".jAudio--playlist"), this.$domControls = this.$context.find(".jAudio--controls"), this.$domVolumeBar = this.$context.find(".jAudio--volume"), this.$domDetails = this.$context.find(".jAudio--details"), this.$domStatusBar = this.$context.find(".jAudio--status-bar"), this.$domProgressBar = this.$context.find(".jAudio--progress-bar-wrapper"), this.$domTime = this.$context.find(".jAudio--time"), this.$domElapsedTime = this.$context.find(".jAudio--time-elapsed"), this.$domTotalTime = this.$context.find(".jAudio--time-total"), this.$domThumb = this.$context.find(".jAudio--thumb"), this.currentState = "pause", this.currentTrack = this.settings.defaultTrack, this.timer = void 0, this.init()
    }

    function a(t, i) {
        for (var t = String(t); t.length < i;)t = "0" + t;
        return t
    }

    var e = "jAudio", r = {
        playlist: [],
        defaultAlbum: void 0,
        defaultArtist: void 0,
        defaultTrack: 0,
        autoPlay: !1,
        debug: !1
    };
    i.prototype = {
        init: function () {
            var t = this;
            t.renderPlaylist(), t.preLoadTrack(), t.highlightTrack(), t.updateTotalTime(), t.events(), t.debug(), t.domAudio.volume = .2
        }, play: function () {
            var t = this, i = t.$domControls.find("#btn-play");
            t.currentState = "play", t.domAudio.play(), clearInterval(t.timer), t.timer = setInterval(t.run.bind(t), 50), i.data("action", "pause"), i.attr("id", "btn-pause"), i.toggleClass("active")
        }, pause: function () {
            var t = this, i = t.$domControls.find("#btn-pause");
            t.domAudio.pause(), clearInterval(t.timer), t.currentState = "pause", i.data("action", "play"), i.attr("id", "btn-play"), i.toggleClass("active")
        }, stop: function () {
            var t = this;
            t.domAudio.pause(), t.domAudio.currentTime = 0, t.animateProgressBarPosition(), clearInterval(t.timer), t.updateElapsedTime(), t.currentState = "stop"
        }, prev: function () {
            var t, i = this;
            t = 0 === i.currentTrack ? i.settings.playlist.length - 1 : i.currentTrack - 1, i.changeTrack(t)
        }, next: function () {
            var t, i = this;
            t = i.currentTrack === i.settings.playlist.length - 1 ? 0 : i.currentTrack + 1, i.changeTrack(t)
        }, preLoadTrack: function () {
            var t = this;
            t.changeTrack(t.settings.defaultTrack), t.settings.autoPlay && t.play()
        }, changeTrack: function (t) {
            var i = this;
            i.currentTrack = t, i.domAudio.src = i.settings.playlist[t].file, i.highlightTrack(), i.updateThumb(), i.renderDetails(), "play" === i.currentState && i.play()
        }, events: function () {
            var i = this;
            i.$domControls.on("click", "button", function () {
                var a = t(this).data("action");
                switch (a) {
                    case"prev":
                        i.prev.call(i);
                        break;
                    case"next":
                        i.next.call(i);
                        break;
                    case"pause":
                        i.pause.call(i);
                        break;
                    case"stop":
                        i.stop.call(i);
                        break;
                    case"play":
                        i.play.call(i)
                }
            }), i.$domPlaylist.on("click", ".jAudio--playlist-item", function () {
                var a = t(this), e = (a.data("track"), a.index());
                i.currentTrack !== e && i.changeTrack(e)
            }), i.$domProgressBar.on("click", function (t) {
                i.updateProgressBar(t), i.updateElapsedTime()
            }), t(i.domAudio).on("loadedmetadata", function () {
                i.animateProgressBarPosition.call(i), i.updateElapsedTime.call(i), i.updateTotalTime.call(i)
            })
        }, getAudioSeconds: function (t) {
            var t = t % 60;
            return t = a(Math.floor(t), 2), t = 60 > t ? t : "00"
        }, getAudioMinutes: function (t) {
            var t = t / 60;
            return t = a(Math.floor(t), 2), t = 60 > t ? t : "00"
        }, highlightTrack: function () {
            var t = this, i = t.$domPlaylist.children(), a = "active";
            i.removeClass(a), i.eq(t.currentTrack).addClass(a)
        }, renderDetails: function () {
            var t = this, i = t.settings.playlist[t.currentTrack], a = (i.file, i.thumb, i.trackName), e = i.trackArtist, r = (i.trackAlbum, "");
            r += "<p>", r += "<span>" + a + "</span>", r += "<span>" + e + "</span>", r += "</p>", t.$domDetails.html(r)
        }, renderPlaylist: function () {
            var i = this, a = "";
            t.each(i.settings.playlist, function (t, i) {
                {
                    var e = i.file, r = i.thumb, o = i.trackName, s = i.trackArtist;
                    i.trackAlbum
                }
                trackDuration = "00:00", a += "<div class='jAudio--playlist-item' data-track='" + e + "'>", a += "<div class='jAudio--playlist-thumb'><img src='" + r + "'></div>", a += "<div class='jAudio--playlist-meta-text'>", a += "<h4>" + o + "</h4>", a += "<p>" + s + "</p>", a += "</div>", a += "</div>"
            }), i.$domPlaylist.html(a)
        }, run: function () {
            var t = this;
            t.animateProgressBarPosition(), t.updateElapsedTime(), t.domAudio.ended && t.next()
        }, animateProgressBarPosition: function () {
            var t = this, i = 100 * t.domAudio.currentTime / t.domAudio.duration + "%", a = {width: i};
            t.$domProgressBar.children().eq(0).css(a)
        }, updateProgressBar: function (t) {
            var i, a, e, r = this;
            t.offsetX && (i = t.offsetX), void 0 === i && t.layerX && (i = t.layerX), a = i / r.$domProgressBar.width(), e = r.domAudio.duration * a, r.domAudio.currentTime = e, r.animateProgressBarPosition()
        }, updateElapsedTime: function () {
            var t = this, i = t.domAudio.currentTime, a = t.getAudioMinutes(i), e = t.getAudioSeconds(i), r = a + ":" + e;
            t.$domElapsedTime.text(r)
        }, updateTotalTime: function () {
            var t = this, i = t.domAudio.duration, a = t.getAudioMinutes(i), e = t.getAudioSeconds(i), r = a + ":" + e;
            t.$domTotalTime.text(r)
        }, updateThumb: function () {
            var t = this, i = t.settings.playlist[t.currentTrack].thumb, a = {"background-image": "url(" + i + ")"};
            t.$domThumb.css(a)
        }, debug: function () {
            var t = this;
            t.settings.debug && console.log(t)
        }
    }, t.fn[e] = function (a) {
        var e = function () {
            return new i(t(this), a)
        };
        t(this).each(e)
    }
}(jQuery);

// initialize
(function () {


}());

1 个答案:

答案 0 :(得分:0)

这是旧的,但如果你愿意有点讨厌,我有一个工作的解决方法。请原谅我讨厌的方法,但最终还是有效的。

我们将在这里使用窗口作为跨功能的全局范围。

init: function()
{
  var self = this;
  self.renderPlaylist();
  self.preLoadTrack();
  self.highlightTrack();
  self.updateTotalTime();
  self.events();
  self.debug();
  self.domAudio.volume = 0.05;
  window.jAudioCore = self; // This line returns the self
},

现在,您可以随时更改播放列表,只需更改 window.playlist 全局变量,代码中的

    renderPlaylist: function()
{
  var self = this,
      template = "";


  $.each(window.playlist, function(i, a) //added window.playlist as the parameter

您的 renderPlaylist 功能应该如上所示。

要在此处测试一个简单的代码供您使用:

(function(){
window.playlist = [
      {
        file: "http://192.168.1.99:5000/mpeg",
        thumb: "https://i.ytimg.com/vi/Kd57YHWqrsI/mqdefault.jpg",
        trackName: "Carnival Of Rust",
        trackArtist: "Poets Of The Fall",
        trackAlbum: "Single",
        trackDuration:"04:06",
      },
      {
        file: "http://127.0.0.1:5000/mpeg",
        thumb: "https://i.ytimg.com/vi/Kd57YHWqrsI/mqdefault.jpg",
        trackName: "Carnival Of Rust",
        trackArtist: "Poets Of The Fall",
        trackAlbum: "Single",
        trackDuration:"04:06",
      }
  ];

  var t = {
    playlist: [], // this is no longer needed, you can remove it from the source code later
    debug:true
  }
  $(".jAudio").jAudio(t);
    setTimeout(function(){
    window.playlist.push({
        file: "http://127.0.0.1:5000/mpeg",
        thumb: "https://i.ytimg.com/vi/Kd57YHWqrsI/mqdefault.jpg",
        trackName: "Carnival Of Rust",
        trackArtist: "Poets Of The Fall",
        trackAlbum: "Single",
        trackDuration:"04:06",
      });
       window.jAudioCore.renderPlaylist(); // renders the playlists
       window.jAudioCore.preLoadTrack(); // will preload the current track
       window.jAudioCore.highlightTrack(); // will highlight in the css

    },3000);

})();

正如我所说的那样,它最终会动态运行而不会刷新。可能存在一些潜在的错误,我无法对此进行太多测试。干杯。