4 $.jPlayer.timeFormat.showHour = true;
6 $(".jp-jplayer").each(function() {
7 console.log('starting player')
9 var $root = $self.parent();
10 var $currentMedia = null
11 var currentDuration = 0;
13 var totalDurationLeft = 0;
17 var setMedia = function(elem, time=0) {
18 console.log('setMedia', elem, time);
21 media['mp3'] = elem.attr('data-mp3');
22 media['oga'] = elem.attr('data-ogg');
23 media['id'] = elem.attr('data-media-id');
25 $(".c-player__head", $root).html(
26 $(".attribution", elem).html())
28 $(".c-player__info", $root).html(
29 $(".title", elem).html()
31 $(".c-media__caption .content", $root).html($(".project-description", elem).html());
32 $(".c-media__caption .license", $root).html($(".license", elem).html());
33 $(".c-media__caption .project-logo", $root).html($(".project-icon", elem).html());
35 player.jPlayer("setMedia", media);
36 player.jPlayer("option", "playbackRate", speed);
37 player.jPlayer("pause", time);
40 $(".play-prev", $root).prop("disabled", !elem.prev().length);
41 $(".play-next", $root).prop("disabled", !elem.next().length);
43 let du = elem.data('duration');
45 elem.nextAll().each(function() {
46 du += $(this).data('duration');
48 totalDurationLeft = du;
55 swfPath: "/static/js/contrib/jplayer/",
56 solution: "html,flash",
58 cssSelectorAncestor: "#" + $self.attr("data-player"),
59 useStateClassSkin: true,
65 let selectItem = $('.c-select li');
66 selectItem.on('click', function() {
67 let speedStr = $(this).data('speed');
68 speed = parseFloat(speedStr);
69 player.jPlayer("option", "playbackRate", speed);
70 localStorage['audiobook-speed'] = speedStr;
73 $('.play-next', $root).click(function() {
74 let p = $currentMedia.next();
76 setMedia(p).jPlayer("play");
79 $('.play-prev', $root).click(function() {
80 let p = $currentMedia.prev();
82 setMedia(p).jPlayer("play");
86 $('.jp-playlist li', $root).click(function() {
87 setMedia($(this)).jPlayer("play");
88 $('.c-player__chapters').removeClass('is-active');
91 var initialElem = $('.jp-playlist li', $root).first();
93 if (true || Modernizr.localstorage) {
95 let speedStr = localStorage['audiobook-speed'];
97 speed = parseFloat(speedStr);
98 $(".speed .is-active").removeClass("is-active");
99 $(".speed [data-speed='" + speedStr + "']").addClass("is-active");
104 audiobooks = JSON.parse(localStorage["audiobook-history"]);
108 last = audiobooks[$root.attr("data-book-slug")]
109 // Fallback for book id;
111 last = audiobooks[$root.attr("data-book-id")]
115 initialElem = $('[data-media-id="' + last[1] + '"]', $root).first();
116 initialTime = last[2];
119 setMedia(initialElem, initialTime);
122 timeupdate: function(event) {
123 t = event.jPlayer.status.currentTime;
124 ttl = (totalDurationLeft - t) / speed;
125 ttl = $.jPlayer.convertTime(ttl);
126 $(".total-time-left").text('Czas do końca: ' + ttl);
128 $(".time-left").text('– ' + $.jPlayer.convertTime(
133 if (Math.abs(t - lastUpdate) > 3) {
135 audiobooks = JSON.parse(localStorage["audiobook-history"]);
139 if (t && event.jPlayer.status.duration - t > 10) {
140 console.log('writer');
141 audiobooks[$root.attr("data-book-slug")] = [
143 event.jPlayer.status.media.id,
144 event.jPlayer.status.currentTime
147 delete audiobooks[$root.attr("data-book-slug")];
149 // Remove old book id, if present.
150 delete audiobooks[$root.attr("data-book-id")];
151 localStorage["audiobook-history"] = JSON.stringify(audiobooks);
157 ended: function(event) {
158 let p = $currentMedia.next();
160 setMedia(p).jPlayer("play");