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 swfPath: "/static/js/contrib/jplayer/",
18 solution: "html,flash",
20 cssSelectorAncestor: "#" + $self.attr("data-player"),
21 useStateClassSkin: true,
27 var setMedia = function(elem, time=0) {
28 console.log('setMedia', elem, time);
31 media['mp3'] = elem.attr('data-mp3');
32 media['oga'] = elem.attr('data-ogg');
33 media['id'] = elem.attr('data-media-id');
35 $(".c-player__head", $root).html(
36 $(".attribution", elem).html())
38 $(".c-player__info", $root).html(
39 $(".title", elem).html()
41 $(".c-media__caption .content", $root).html($(".project-description", elem).html());
42 $(".c-media__caption .license", $root).html($(".license", elem).html());
43 $(".c-media__caption .project-logo", $root).html($(".project-icon", elem).html());
45 player.jPlayer("setMedia", media);
46 $("audio")[0].playbackRate = speed;
47 player.jPlayer("pause", time);
50 $(".play-prev", $root).prop("disabled", !elem.prev().length);
51 $(".play-next", $root).prop("disabled", !elem.next().length);
53 let du = elem.data('duration');
55 elem.nextAll().each(function() {
56 du += $(this).data('duration');
58 totalDurationLeft = du;
63 let selectItem = $('.c-select li');
64 selectItem.on('click', function() {
65 let speedStr = $(this).data('speed');
66 speed = parseFloat(speedStr);
67 $("audio")[0].playbackRate = speed;
68 localStorage['audiobook-speed'] = speedStr;
71 $('.play-next', $root).click(function() {
72 let p = $currentMedia.next();
74 setMedia(p).jPlayer("play");
77 $('.play-prev', $root).click(function() {
78 let p = $currentMedia.prev();
80 setMedia(p).jPlayer("play");
84 $('.jp-playlist li', $root).click(function() {
85 setMedia($(this)).jPlayer("play");
86 $('.c-player__chapters').removeClass('is-active');
89 var initialElem = $('.jp-playlist li', $root).first();
91 if (true || Modernizr.localstorage) {
93 let speedStr = localStorage['audiobook-speed'];
95 speed = parseFloat(speedStr);
96 $(".speed .is-active").removeClass("is-active");
97 $(".speed [data-speed='" + speedStr + "']").addClass("is-active");
102 audiobooks = JSON.parse(localStorage["audiobook-history"]);
106 last = audiobooks[$root.attr("data-book-slug")]
107 // Fallback for book id;
109 last = audiobooks[$root.attr("data-book-id")]
113 initialElem = $('[data-media-id="' + last[1] + '"]', $root).first();
114 initialTime = last[2];
117 setMedia(initialElem, initialTime);
120 timeupdate: function(event) {
121 t = event.jPlayer.status.currentTime;
122 ttl = (totalDurationLeft - t) / speed;
123 ttl = $.jPlayer.convertTime(ttl);
124 $(".total-time-left").text('Czas do końca: ' + ttl);
126 $(".time-left").text('– ' + $.jPlayer.convertTime(
131 if (Math.abs(t - lastUpdate) > 3) {
133 audiobooks = JSON.parse(localStorage["audiobook-history"]);
137 if (t && event.jPlayer.status.duration - t > 10) {
138 console.log('writer');
139 audiobooks[$root.attr("data-book-slug")] = [
141 event.jPlayer.status.media.id,
142 event.jPlayer.status.currentTime
145 delete audiobooks[$root.attr("data-book-slug")];
147 // Remove old book id, if present.
148 delete audiobooks[$root.attr("data-book-id")];
149 localStorage["audiobook-history"] = JSON.stringify(audiobooks);