1/* 2 AudioPlayer jQuery Plugin 3 A plugin for custom audio players 4 version 1.1, Nov 11th, 2016 5 by Ingi á Steinamørk 6 7 The MIT License (MIT) 8 9 Copyright (c) 2016 Ingi á Steinamørk 10 11 Permission is hereby granted, free of charge, to any person obtaining a copy of 12 this software and associated documentation files (the "Software"), to deal in 13 the Software without restriction, including without limitation the rights to 14 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 15 the Software, and to permit persons to whom the Software is furnished to do so, 16 subject to the following conditions: 17 18 The above copyright notice and this permission notice shall be included in all 19 copies or substantial portions of the Software. 20 21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 23 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 24 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 25 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 26 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27 */ 28 29var AudioPlayer = { 30 debug: false, 31 $element: null, 32 $button: null, 33 $timeline: null, 34 $progress: null, 35 $playhead: null, 36 $time: null, 37 onplayhead: false, 38 lastx: null, 39 loading: false, 40 41 init: function (audio_element, audio_element_source, audio_button, audio_timeline, audio_progress, audio_playhead, audio_time) { 42 AudioPlayer.debug && console.log('[AudioPlayer] init'); 43 audio_element = typeof audio_element === 'undefined' ? '#audio_element' : audio_element; 44 audio_element_source = typeof audio_element_source === 'undefined' ? '#audio_element_source' : audio_element_source; 45 audio_button = typeof audio_button === 'undefined' ? '#audio_button' : audio_button; 46 audio_timeline = typeof audio_timeline === 'undefined' ? '#audio_timeline' : audio_timeline; 47 audio_progress = typeof audio_progress === 'undefined' ? '#audio_progress' : audio_progress; 48 audio_playhead = typeof audio_playhead === 'undefined' ? '#audio_playhead' : audio_playhead; 49 audio_time = typeof audio_time === 'undefined' ? '#audio_time' : audio_time; 50 // Setup references 51 AudioPlayer.$element = $(audio_element); 52 AudioPlayer.$element_source = $(audio_element_source); 53 AudioPlayer.$button = $(audio_button); 54 AudioPlayer.$timeline = $(audio_timeline); 55 AudioPlayer.$progress = $(audio_progress); 56 AudioPlayer.$playhead = $(audio_playhead); 57 AudioPlayer.$time = $(audio_time); 58 // Add initial classes 59 AudioPlayer.$button.addClass('audioplayer--play'); 60 // Bind events 61 AudioPlayer.$button.on('click', AudioPlayer.playAudio); 62 AudioPlayer.$element.on('timeupdate', AudioPlayer.audioTimeUpdate); 63 AudioPlayer.$timeline.on('click', function (e) { 64 AudioPlayer.$element[0].currentTime = AudioPlayer.$element[0].duration * AudioPlayer.clickPercent(e); 65 AudioPlayer.movePlayhead(e); 66 }); 67 AudioPlayer.$playhead.on('mousedown touchstart', AudioPlayer.mouseDown);
68 $(window).on('mouseup touchend', AudioPlayer.mouseUp); 69 AudioPlayer.$element.on('canplaythrough progress', function (e) { 70 AudioPlayer.audioTimeUpdate(e); 71 }); 72 AudioPlayer.$element.on('ended', function (e) { 73 if (AudioPlayer.$element.get(0).hasAttribute('data-source1')) { 74 AudioPlayer.$element_source.attr("src", AudioPlayer.$element.attr('data-source1')).detach().appendTo(AudioPlayer.$element); 75 if (AudioPlayer.$element.get(0).hasAttribute('data-source2')) { 76 AudioPlayer.$element.attr('data-source1', AudioPlayer.$element.attr('data-source2')); 77 AudioPlayer.$element.removeAttr('data-source2'); 78 } else { 79 AudioPlayer.$element.removeAttr('data-source1'); 80 } 81 AudioPlayer.loading = true; 82 AudioPlayer.$element.get(0).load(); 83 } 84 }); 85 AudioPlayer.$element.on('canplay', function (e) { 86 if (AudioPlayer.loading) { 87 AudioPlayer.loading = false; 88 AudioPlayer.playAudio(); 89 } 90 }); 91 AudioPlayer.debug && console.log('[AudioPlayer] init done'); 92 }, 93 94 playAudio: function () { 95 AudioPlayer.debug && console.log('[AudioPlayer] playAudio'); 96 if (AudioPlayer.$element[0].paused) { 97 AudioPlayer.$element[0].play(); 98 AudioPlayer.$button.removeClass('audioplayer--play').addClass('audioplayer--pause'); 99 } else { 100 AudioPlayer.$element[0].pause(); 101 AudioPlayer.$button.removeClass('audioplayer--pause').addClass('audioplayer--play'); 102 } 103 }, 104 105 audioTimeUpdate: function () { 106 var position_percent = AudioPlayer.$element[0].currentTime / AudioPlayer.$element[0].duration; 107 AudioPlayer.$playhead.css({ 108 'left': (position_percent * 100) + '%', 109 'margin-left': (position_percent * -AudioPlayer.$playhead.outerWidth()) + 'px' 110 }); 111 AudioPlayer.$progress.css({ 112 // 'right': 'calc(' + ((1 - position_percent) * 100) + '% + ' + ((AudioPlayer.$playhead.outerWidth() * position_percent) - (AudioPlayer.$playhead.outerWidth()/2)) + 'px)' 113 'right': ((1 - position_percent) * 100) + '%' 114 }); 115 var duration = AudioPlayer.$element[0].duration; 116 AudioPlayer.$time.find('.seconds_elapsed').text(duration * position_percent); 117 AudioPlayer.$time.find('.seconds_left').text(duration - (duration * position_percent)); 118 AudioPlayer.$time.find('.seconds_total').text(duration); 119 AudioPlayer.$time.find('.time_elapsed').text(AudioPlayer.formatTime(duration * position_percent)); 120 AudioPlayer.$time.find('.time_left').text(AudioPlayer.formatTime(duration - (duration * position_percent))); 121 AudioPlayer.$time.find('.time_total').text(AudioPlayer.formatTime(duration)); 122 if (AudioPlayer.$element[0].currentTime === AudioPlayer.$element[0].duration) { 123 AudioPlayer.$button.removeClass('audioplayer--pause').addClass('audioplayer--play'); 124 } 125 }, 126 127 clickPercent: function (e) { 128 if (e.type !== 'touchend') { 129 AudioPlayer.lastx = e.pageX; 130 } 131 var offset_left = AudioPlayer.$timeline.offset().left; 132 var offset_right = offset_left + AudioPlayer.$timeline.outerWidth(); 133 return (AudioPlayer.lastx - offset_left) / (offset_right - offset_left); 134 }, 135 136 mouseDown: function () { 137 AudioPlayer.onplayhead = true; 138 $(window).on('mousemove touchmove', AudioPlayer.movePlayhead); 139 AudioPlayer.$element.off('timeupdate', AudioPlayer.audioTimeUpdate); 140 }, 141 142 mouseUp: function (e) { 143 if (AudioPlayer.onplayhead === true) { 144 AudioPlayer.movePlayhead(e); 145 $(window).off('mousemove touchmove', AudioPlayer.movePlayhead); 146 AudioPlayer.$element[0].currentTime = AudioPlayer.$element[0].duration * AudioPlayer.clickPercent(e); 147 AudioPlayer.$element.on('timeupdate', AudioPlayer.audioTimeUpdate); 148 } 149 AudioPlayer.onplayhead = false; 150 }, 151 152 movePlayhead: function (e) { 153 if (e.type === 'touchmove') { 154 AudioPlayer.lastx = e.originalEvent.touches[0].pageX ? e.originalEvent.touches[0].pageX : AudioPlayer.lastx; 155 } else if (e.type === 'touchend') { 156 } else { 157 AudioPlayer.lastx = e.pageX; 158 } 159 var offset_left = AudioPlayer.$timeline.offset().left; 160 var offset_right = offset_left + AudioPlayer.$timeline.outerWidth(); 161 var position_percent = (AudioPlayer.lastx - offset_left) / (offset_right - offset_left); 162 if (position_percent < 0) position_percent = 0; 163 if (position_percent > 1) position_percent = 1; 164 AudioPlayer.$playhead.css({ 165 'left': (position_percent * 100) + '%', 166 'margin-left': (position_percent * -AudioPlayer.$playhead.outerWidth()) + 'px' 167 }); 168 AudioPlayer.$progress.css({ 169 // 'right': 'calc(' + ((1 - position_percent) * 100) + '% + ' + ((AudioPlayer.$playhead.outerWidth() * position_percent) - (AudioPlayer.$playhead.outerWidth())) + 'px)', 170 'right': ((1 - position_percent) * 100) + '%' 171 }); 172 var duration = AudioPlayer.$element[0].duration; 173 AudioPlayer.$time.find('.seconds_elapsed').text(duration * position_percent); 174 AudioPlayer.$time.find('.seconds_left').text(duration - (duration * position_percent)); 175 AudioPlayer.$time.find('.seconds_total').text(duration); 176 AudioPlayer.$time.find('.time_elapsed').text(AudioPlayer.formatTime(
176duration * position_percent)); 177 AudioPlayer.$time.find('.time_left').text(AudioPlayer.formatTime(duration - (duration * position_percent))); 178 AudioPlayer.$time.find('.time_total').text(AudioPlayer.formatTime(duration)); 179 }, 180 181 formatTime: function (seconds_input, include_hours) { 182 include_hours = typeof include_hours === 'undefined' ? (seconds_input >= 3600) : include_hours; 183 seconds_input = parseInt(seconds_input, 10); 184 var hours = Math.floor(seconds_input / 3600); 185 var minutes = Math.floor((seconds_input - (hours * 3600)) / 60); 186 var seconds = seconds_input - (hours * 3600) - (minutes * 60); 187 if (!include_hours) { 188 minutes = minutes + (hours * 60); 189 hours = 0; 190 } 191 hours = (hours < 10 ? hours : hours); 192 minutes = (minutes < 10 ? '0' + minutes : minutes); 193 seconds = (seconds < 10 ? '0' + seconds : seconds); 194 if (include_hours) { 195 return hours + ':' + minutes + ':' + seconds; 196 } else { 197 return minutes + ':' + seconds; 198 } 199 } 200 201 202};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.