Passed
Push — develop ( f34e9f...e9b353 )
by Nikolay
15:23 queued 10s
created

sites/admin-cabinet/assets/js/src/SoundFiles/sound-file-modify-player.js   A

Complexity

Total Complexity 13
Complexity/F 1.63

Size

Lines of Code 82
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 51
c 0
b 0
f 0
dl 0
loc 82
rs 10
mnd 5
bc 5
fnc 8
bpm 0.625
cpm 1.625
noi 1
1
/*
2
 * Copyright (C) MIKO LLC - All Rights Reserved
3
 * Unauthorized copying of this file, via any medium is strictly prohibited
4
 * Proprietary and confidential
5
 * Written by Nikolay Beketov, 10 2020
6
 *
7
 */
8
9
const sndPlayer = {
10
	slider: document.getElementById('audio-player'),
11
	duration: 0, // Duration of audio clip
12
	$pButton: $('#play-button'), // play button
13
	$slider: $('#play-slider'),
14
	$playerSegment: $('#audio-player-segment'),
15
	initialize() {
16
		// play button event listenter
17
		sndPlayer.$pButton.on('click', (e) => {
18
			e.preventDefault();
19
			sndPlayer.play();
20
		});
21
		// timeupdate event listener
22
		sndPlayer.slider.addEventListener('timeupdate', sndPlayer.cbTimeUpdate, false);
23
24
		// Gets audio file duration
25
		sndPlayer.slider.addEventListener('canplaythrough', sndPlayer.cbCanPlayThrough, false);
26
27
		sndPlayer.$slider.range({
28
			min: 0,
29
			max: 100,
30
			start: 0,
31
			onChange: sndPlayer.cbOnSliderChange,
32
		});
33
	},
34
	UpdateSource(newSource) {
35
		sndPlayer.slider.getElementsByTagName('source')[0].src = newSource;
36
		sndPlayer.slider.pause();
37
		sndPlayer.slider.load();
38
		sndPlayer.slider.oncanplaythrough = sndPlayer.cbCanPlayThrough;
39
	},
40
	cbCanPlayThrough() {
41
		sndPlayer.duration = sndPlayer.slider.duration;
42
		console.log(`New duration ${sndPlayer.slider.readyState}`);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
43
		if (sndPlayer.duration > 0) {
44
			sndPlayer.$slider.range('set value', 0);
45
			sndPlayer.$playerSegment.show();
46
		} else {
47
			sndPlayer.$playerSegment.hide();
48
		}
49
	},
50
51
	cbOnSliderChange(newVal, meta) {
52
		if (meta.triggeredByUser && Number.isFinite(sndPlayer.slider.duration)) {
53
			sndPlayer.slider.removeEventListener('timeupdate', sndPlayer.cbTimeUpdate, false);
54
			sndPlayer.slider.currentTime = (sndPlayer.slider.duration * newVal) / 100;
55
			sndPlayer.slider.addEventListener('timeupdate', sndPlayer.cbTimeUpdate, false);
56
		}
57
	},
58
	// timeUpdate
59
	// Synchronizes playhead position with current point in audio
60
	cbTimeUpdate() {
61
		if (Number.isFinite(sndPlayer.slider.duration)) {
62
			const percent = sndPlayer.slider.currentTime / sndPlayer.slider.duration;
63
			const rangePosition = Math.round((percent) * 100);
64
			sndPlayer.$slider.range('set value', rangePosition);
65
			if (sndPlayer.slider.currentTime === sndPlayer.duration) {
66
				sndPlayer.$pButton.html('<i class="icon play"></i>');
67
			}
68
		}
69
	},
70
71
	// Play and Pause
72
	play() {
73
		// start music
74
		if (sndPlayer.slider.paused && sndPlayer.slider.duration) {
75
			sndPlayer.slider.play();
76
			// remove play, add pause
77
			sndPlayer.$pButton.html('<i class="icon pause"></i>');
78
		} else { // pause music
79
			sndPlayer.slider.pause();
80
			// remove pause, add play
81
			sndPlayer.$pButton.html('<i class="icon play"></i>');
82
		}
83
	},
84
85
};
86
87
88
$(document).ready(() => {
89
	sndPlayer.initialize();
90
});
91