-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.js
92 lines (78 loc) · 2.88 KB
/
audio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import {getUrlParam, setUrlParam} from './utilities.js';
export function getAudioWidget(saints){
const saintIndex = +getUrlParam('track',0)
// data
const saint = saints[saintIndex];
const saintLen = saints.length;
const audioUrl = saint.url;
if(!audioUrl) $('#wait-layer').hide();
// elements
const $container = $$('div').addClass('audio-widget')
const $title = $$('h3').text(saint.title).addClass("feast__title");
const $player = $$('div').addClass("player")
//.attr("crossorigin","anonymous")
const $playerAudio = $$('audio').attr('src',audioUrl).addClass('player__audio')
$playerAudio[0].load();
const $playerControls = $$('div').addClass('player-controls')
const $prevTrackButton = $$('button').text('<').addClass('player-controls__button player-controls__button_track_prev');
if(saintLen < 2) $prevTrackButton.hide();
const $nextTrackButton = $$('button').text('>').addClass('player-controls__button player-controls__button_track_next');
if(saintLen < 2) $nextTrackButton.hide();
const $trackControls = $$('div').addClass('track_controls')
const $playIcon = $$('div').addClass('icon_play').attr('title','play');
const $pauseIcon = $$('div').addClass('icon_pause').attr('title','pause');
const $trackPlayButton = $$('button').html($playIcon).addClass('track-controls__button track-controls__button_play ')
const $trackPlayButtonSpinner = $$('div').addClass('spinner track-controls__button-spinner_play')
//functions
function togglePlay() {
const method = $playerAudio[0].paused ? 'play' : 'pause';
$playerAudio[0][method]();
}
function updateButton() {
const icon = $playerAudio[0].paused ? $playIcon : $pauseIcon;
$trackPlayButton.html(icon);
}
function resetTime(){
if($playerAudio[0].currentTime >= saint.end){
$playerAudio[0].currentTime = saint.start
$playerAudio[0].pause();
}
}
function nextTrack(){
changeTrack((saintIndex+1) % saintLen);
}
function prevTrack(){
changeTrack((saintIndex || saintLen) - 1);
}
function changeTrack(track){
setUrlParam('track',track);
$container.replaceWith(getAudioWidget(saints).render());
}
// events
$trackPlayButton.click(togglePlay);
$playerAudio[0].addEventListener('play', updateButton);
$playerAudio[0].addEventListener('pause', updateButton);
$playerAudio[0].addEventListener('timeupdate',resetTime);
$prevTrackButton.click(prevTrack)
$nextTrackButton.click(nextTrack)
$playerAudio[0].addEventListener('canplaythrough',function(e){
$playerAudio[0].currentTime = saint.start;
$trackPlayButtonSpinner.replaceWith($trackPlayButton);
},{once: true});
// render
function render(){
return $container
.append($title)
.append($player
.append($playerAudio)
.append($playerControls
.append($prevTrackButton)
.append($trackControls
.append($trackPlayButtonSpinner)
)
.append($nextTrackButton)
)
)
}
return {render}
}