IRON.audioPlayer = (function ($) { "use strict"; var autoplayEnable; function initPlayer(player) { var audioPlayer = player; audioPlayer.id = audioPlayer.data("id"); audioPlayer.hide_progressbar = audioPlayer.data("hide-progressbar") ? true : false; this.audioPlayer = player; var waveContainer = this.audioPlayer.find(".player .wave").attr("id"); var playlist = audioPlayer.find(".playlist"); this.playlist = playlist; this.autoplayEnable = audioPlayer.data("autoplay"); audioPlayer.list = {}; audioPlayer.list.tracks = []; playlist.find(".sr-playlist-item").each(function () { var $track = $(this); var trackData = { peakFile: $track.data("peakfile"), mp3: $track.data("audiopath"), sourcePostID: $track.data("post-id"), id: $track.data("trackid"), // Its the media attachment ID if set. track_pos: $track.data("track-pos"), isPreview: $track.data("is-preview"), peak_allow_frontend: $track.data("peakfile-allow"), }; audioPlayer.list.tracks.push(trackData); }); $(audioPlayer).css("opacity", 1); fetch(this.audioPlayer.attr('data-url-playlist')) // Call the fetch function passing the playlist.json as audioplayer.list parameters .then(response => { if (!response.ok) { throw new Error('Network error: ' + response.status); } return response.json(); }) .then(data => { this.audioPlayer.list = data; }) .catch(error => { console.error('There was a problem retrieving the data: ', error); }); this.$audio_el = $("#" + waveContainer).find(".sonaar_media_element")[0]; fakeWaveUpdate(this.$audio_el, audioPlayer, playlist); $(audioPlayer).find(".wave").css("opacity", "1"); setPlaylist(playlist, this.$audio_el, audioPlayer); var trackNumber = playlist.find("li").index(); var track = playlist.find("li").eq(trackNumber); if(track.data('tracktime')){ audioPlayer.find('.totalTime').text('-' + track.data('tracktime')); } setCurrentTrack(playlist.find("li").eq(0), playlist.find("li").index(), audioPlayer, this.$audio_el); setControl(this.$audio_el, audioPlayer, playlist); if(track.data('tracktime')){ audioPlayer.find('.totalTime').text('-' + track.data('tracktime')); } sr_playerCTAresponsive(); } var triggerPlay = function ($audio_el, audioPlayer) { $audio_el.play(); togglePlaying(audioPlayer, $audio_el); }; function setCurrentTrack(track, index, audioPlayer, $audio_el) { audioPlayer.currentTrack = index; var albumArt = audioPlayer.find(".album .album-art"); var album = audioPlayer.find(".album"); var trackTitle = audioPlayer.find(".track-title"); var trackTime = audioPlayer.find(".track-time"); var trackArtist = audioPlayer.find(".sr_it-artists-value"); var albumTitle = audioPlayer.find(".sr_it-playlist-title, .album-title"); var albumReleaseDate = audioPlayer.find(".srp_subtitle"); if (audioPlayer.data('hide-artwork') != '1' && audioPlayer.data('hide-artwork') != 'true') { if (track.data("albumart")) { audioPlayer.removeClass('sonaar-no-artwork'); if (albumArt.find("img").length) { albumArt.find("img").attr("src", track.data("albumart")); } else { albumArt.css("background-image", "url(" + track.data("albumart") + ")"); } } else { audioPlayer.addClass('sonaar-no-artwork'); } } audioPlayer.data("currentTrack", index); trackTitle.html(track.data("tracktitle")); trackTime.text(track.data("tracktime")); trackArtist.text(track.data("trackartists")); albumReleaseDate.text(track.data("releasedate")); if (audioPlayer.data("playlist_title").length) { albumTitle.text(audioPlayer.data("playlist_title")); } else { albumTitle.text(track.data("albumtitle")); } if (audioPlayer.data('playertemplate') == 'skin_boxed_tracklist' && audioPlayer.find('.srp_track_cta').length) { audioPlayer.find('.srp_track_cta .song-store-list-container').remove(); audioPlayer.find('.srp_track_cta').append(track.find('.song-store-list-container').clone()); } audioPlayer.find(".player").removeClass("hide"); audioPlayer.find(".wave").removeClass("reveal"); if (!track.data("showloading")) { audioPlayer.find(".player").addClass("hide"); } else { audioPlayer.find(".progressLoading").css("opacity", "0.75"); } IRON.createFakeWave(audioPlayer); setTime(audioPlayer, $audio_el); hideEmptyAttribut(track.data("releasedate"), audioPlayer.find(".srp_subtitle")); } function setPlaylist(playlist, $audio_el, audioPlayer) { let playlistTimeDuration = 0; playlist.find("li").each(function () { setSingleTrack($(this), $(this).index(), $audio_el, audioPlayer); if ($(this).data('relatedtrack') != '1') { //Count playlist time duration if ($(this).data('tracktime')) { convertTime($(this).data('tracktime')); playlistTimeDuration = playlistTimeDuration + convertTime($(this).data('tracktime')); } else { // Hide playlist time duration if one track doesnt have time (streaming) audioPlayer.find('.srp_playlist_duration').hide(); } } }); //Output playlist time duration playlistTimeDuration = Math.round(playlistTimeDuration / 60) * 60; //Round to minutes playlistTimeDuration = moment.duration(playlistTimeDuration, "seconds"); let durationOutput = (playlistTimeDuration.hours() > 0) ? playlistTimeDuration.hours() + ' ' + audioPlayer.find('.srp_playlist_duration').data('hours-label') + ' ' : ''; durationOutput = durationOutput + playlistTimeDuration.minutes() + ' ' + audioPlayer.find('.srp_playlist_duration').data('minutes-label'); audioPlayer.find('.srp_playlist_duration').html(durationOutput); } function setTime(audioPlayer, $audio_el) { $($audio_el).on("timeupdate", function () { var currentTime = $audio_el.currentTime; var time = moment.duration(currentTime, "seconds"); if (time.hours() >= 12 || time.hours() <= 0) { audioPlayer.find(".currentTime").html(moment(time.minutes() + ":" + time.seconds(), "m:s").format("mm:ss")); } else { audioPlayer.find(".currentTime").html(moment(time.hours() + ":" + time.minutes() + ":" + time.seconds(), "h:m:s").format("h:mm:ss")); } if ($audio_el.duration !== Infinity) { var timeLeft = moment.duration($audio_el.duration - $audio_el.currentTime, "seconds"); if(timeLeft.milliseconds() > 0){ if (timeLeft.hours() >= 12 || timeLeft.hours() <= 0) { audioPlayer.find(".totalTime").html("-" + moment(timeLeft.minutes() + ":" + timeLeft.seconds(), "m:s").format("mm:ss")); } else { audioPlayer.find(".totalTime").html("-" + moment(timeLeft.hours() + ":" + timeLeft.minutes() + ":" + timeLeft.seconds(), "h:m:s").format("h:mm:ss")); } }else{ audioPlayer.find(".totalTime").html(""); } } else { audioPlayer.find(".totalTime").html(""); } }); } function setControl($audio_el, audioPlayer, playlist) { // var ctrl = audioPlayer.find('.control'); audioPlayer.unbind('click'); audioPlayer.on("click", ".play, .album .album-art", function (event) { togglePause(); if (!audioPlayer.hasClass("audio-playing")) { if($($audio_el).attr('src') != ''){ play(audioPlayer, $audio_el); triggerPlay($audio_el, audioPlayer); } else { playlist.find("li").eq(0).find("a.audio-track").click(); } } else { togglePause(); } togglePlaying(audioPlayer, $audio_el); event.preventDefault(); }); audioPlayer.on("click", ".previous", function (event) { previous(audioPlayer, $audio_el, playlist); event.preventDefault(); }); audioPlayer.on("click", ".next", function (event) { next(audioPlayer, $audio_el, playlist); event.preventDefault(); }); audioPlayer.on('mouseenter', '.sr-playlist-item .song-store-list-menu', function () { openStoreListContainer(this); }); audioPlayer.on('mouseleave', '.sr-playlist-item .song-store-list-container', function () { closeStoreListContainer(this); }); } function setSingleTrack(singleTrack, eq, $audio_el, audioPlayer) { singleTrack.find(".audio-track").remove(); var tracknumber = eq + 1; var trackplay = $("", { class: "track-number", html: '' + tracknumber + '', }); $(""); $("", { class: "audio-track", click: function (event) { if ($(this).parents('.sr-playlist-item').attr("data-audiopath").length == 0) { return; } if (ifTrackIsPlaying($audio_el) && singleTrack.hasClass("current")) { togglePause(); togglePlaying(audioPlayer, $audio_el); } else if (singleTrack.hasClass("current")) { play(audioPlayer, $audio_el); } else { togglePause(); setCurrentTrack(singleTrack, eq, audioPlayer, $audio_el); setAudio(singleTrack.data("audiopath"), $audio_el, audioPlayer); audioPlayer.find(".playlist li").removeClass("current"); singleTrack.addClass("current"); triggerPlay($audio_el, audioPlayer); togglePlaying(audioPlayer, $audio_el); } IRON.init_generatePeaks(audioPlayer); event.preventDefault(); }, }) .appendTo(singleTrack) .prepend(trackplay) .append('