-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedirom-audio-player.js
More file actions
544 lines (424 loc) · 16.7 KB
/
edirom-audio-player.js
File metadata and controls
544 lines (424 loc) · 16.7 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/**
* Represents the EdiromAudioPlayer custom element.
* @class
* @extends HTMLElement
*/
class EdiromAudioPlayer extends HTMLElement {
/**
* Creates an instance of EdiromAudioPlayer.
* @constructor
*/
constructor() {
super();
/** attach shadow root with mode "open" */
this.attachShadow({ mode: 'open' });
//Define a FontFace
const font = new FontFace("Material Symbols Outlined", "url(https://fonts.gstatic.com/s/materialsymbolsoutlined/v192/kJF1BvYX7BgnkSrUwT8OhrdQw4oELdPIeeII9v6oDMzByHX9rA6RzaxHMPdY43zj-jCxv3fzvRNU22ZXGJpEpjC_1v-p_4MrImHCIJIZrDCvHOej.woff2)", {
style: "normal",
weight: "100 700"
});
// wait for font
font.load().then((loaded_face) => {
document.fonts.add(loaded_face)
}).catch((error) => { });
}
/**
* Returns the list of observed attributes for the EdiromAudioPlayer custom element.
* @static
* @returns {Array<string>} The list of observed attributes.
*/
static get observedAttributes() {
return ['track', 'tracks', 'height', 'width', 'state', 'start', 'end', 'playbackrate', 'progressbar', 'playlist'];
}
/**
* Invoked when the custom element is connected from the document's DOM.
*/
connectedCallback() {
// set properties from attributes
this.props = Object.fromEntries(
Array.from(this.attributes).map(a => [a.name, a.value])
);
// initial rendering
if(!this.props.tracks) {
this.render();
} else {
const tracks = JSON.parse(this.props.tracks);
const track = this.props.track;
this.render();
}
}
/**
* Invoked when the custom element is disconnected from the document's DOM.
*/
disconnectedCallback() { }
/**
* Invoked when the custom element is moved to a new document.
*/
adoptedCallback() { }
/**
* Invoked when one of the custom element's attributes is added, removed, or changed.
* @param {string} property - The name of the attribute that was changed.
* @param {*} oldValue - The previous value of the attribute.
* @param {*} newValue - The new value of the attribute.
*/
attributeChangedCallback(property, oldValue, newValue) {
// handle property change
this.set(property, newValue);
}
/**
* Renders the EdiromAudioPlayer custom element with tracks content.
*/
render() {
// get properties and prepare content
const tracks = this.props.tracks ? JSON.parse(this.props.tracks) : [];
const { track, height, width } = this.props;
// prepare tracks content
const tracksHTML = tracks.map((thisTrack, idx) => `<div class="track-button track-toggler${idx == track ? ' current' : ''}" data-trackidx="${idx}">
<div class="track-title">${thisTrack.title}</div>
<div class="track-subtitle">${thisTrack.composer} - ${thisTrack.work}</div>
</div>
`).join('');
// append content
this.shadowRoot.innerHTML = `
<style>
.mso {
font-family: 'Material Symbols Outlined';
font-weight: normal;
font-style: normal;
font-size: 24px;
line-height: 1;
letter-spacing: normal;
text-transform: none;
display: inline-block;
white-space: nowrap;
word-wrap: normal;
direction: ltr;
-moz-font-feature-settings: 'liga';
-moz-osx-font-smoothing: grayscale;
}
</style>
<style>
#player {
height: ${height ?? '100%'};
width: ${width ?? '100%'};
container: player / inline-size;
}
#player.hidden{
display: none;
}
#controls {
display: inline-block;
align-items: center;
gap: 10px;
}
#controls button {
display: inline-block;
border: none;
background: none;
}
#controls #replayButton,
#controls #fast_forwardButton,
#controls #fast_rewindButton {
display: none;
}
#timeInfo {
display: inline-block;
margin-top: 10px;
}
#timeInfo input {
width: 100px;
}
#timeInfo span {
font-size: 0.85rem;
font-family: 'Roboto', sans-serif;
text-align: center;
}
#timer {
display: inline-block;
margin-top: 4px;
position: absolute;
margin-left: 5px;
}
.track-button {
display: block;
margin: 10px 0;
padding: 6px 16px;
font-size: 0.875rem;
font-weight: 500;
line-height: 1.75;
letter-spacing: 0.02857em;
color: rgba(0, 0, 0, 0.87);
border: none;
border-radius: 4px;
background-color: #e6e6e6;
transition: background-color 0.3s;
position: relative;
cursor: pointer;
}
.track-button:hover, .track-button.current {
background-color: #d5d5d5;
}
.track-button.current {
font-weight: 700;
}
.track-button:active {
background-color:rgb(153, 153, 153);
}
.track-button:focus {
outline: none;
box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.12);
}
/* height-dependent rules */
/* width-dependent rules */
@container player (width > 380px){
#controls #replayButton,
#controls #fast_forwardButton,
#controls #fast_rewindButton {
display: inline-block;
}
}
</style>
<div id="player" class="" style="">
<div id="controls">
<audio id="audioPlayer" controls style="display: none;">
<source src="${tracks[track].src}" type="${tracks[track].type}">
Your browser does not support the audio element.
</source>
</audio>
<button id="skip_previousButton" title="skip_previous" class="track-toggler" data-trackstep="-1"><span class="mso">skip_previous</span></button>
<button id="play_arrowButton" title="play"><span class="mso">play_arrow</span></button>
<button id="skip_nextButton" title="skip_next" class="track-toggler" data-trackstep="+1"><span class="mso">skip_next</span></button>
<button id="playlist_removeButton" title="playlist_remove"><span class="mso">playlist_remove</span></button>
</div>
<div id="timeInfo">
<input type="range" id="progressSlider" min="0" max="100" value="0">
<div id="timer"><span id="currentTime">0:00</span> / <span id="totalTime">0:00</span></div>
</div>
</div>
<div id="tracks">
`+tracksHTML+`
</div>
`;
// add event listeners again after rendering
this.addEventListeners();
}
/**
* Sets the value of a global property and triggers property update events.
* @param {string} property - The name of the property to set.
* @param {*} newPropertyValue - The new value to set for the property.
*/
set(property, newPropertyValue) {
// set properties from attributes (if not yet done)
if (this.props === undefined) {
this.props = Object.fromEntries(
Array.from(this.attributes).map(a => [a.name, a.value])
);
} else {
this.props[property] = newPropertyValue;
}
// custom event for property update
const event = new CustomEvent('communicate-' + property + '-update', {
detail: { [property]: newPropertyValue },
bubbles: true
});
this.dispatchEvent(event);
// further handling of property change
this.handlePropertyChange(property, newPropertyValue);
}
/**
* Handles property changes for the audio player.
* @param {string} property - The name of the property being changed.
* @param {any} newPropertyValue - The new value of the property.
*/
handlePropertyChange(property, newPropertyValue) {
// get necessary objects and check if available
const audioPlayer = this.shadowRoot.querySelector('#audioPlayer');
const playerDiv = this.shadowRoot.querySelector('#player');
// handle property change
switch(property) {
// handle track setting
case 'track':
try {
const source = this.shadowRoot.querySelector('source');
// set info at source element
const tracks = JSON.parse(this.props.tracks);
const nextTrack = tracks[newPropertyValue];
if(source != null){
source.src = nextTrack.src;
source.type = nextTrack.type;
}
// mark active track, if exists in DOM, therefore querySelectorAll() is used
this.shadowRoot.querySelectorAll(".track-button").forEach((e) => { e.classList.remove('current'); });
this.shadowRoot.querySelectorAll('.track-button[data-trackidx="'+newPropertyValue+'"]').forEach((e) => { e.classList.add('current') });
// handle audio player state
(audioPlayer != null) ? audioPlayer.load() : console.log("Audio player not available");
this.set('start', this.props.start);
this.set('state', 'play');
} catch (error) {
console.log("Error setting track: ", error);
}
break;
// handle state setting
case 'state':
const playButton = this.shadowRoot.querySelector('#play_arrowButton');
if (newPropertyValue === 'play') {
// if audio player is currently paused, play it
this.shadowRoot.querySelector('#audioPlayer').play();
// set play button to pause
playButton.setAttribute('title', 'pause');
playButton.innerHTML = '<span class="mso">pause</span>';
}
if(newPropertyValue === 'pause') {
// if audio player is currently playing, pause it
this.shadowRoot.querySelector('#audioPlayer').pause();
// set play button to play
playButton.setAttribute('title', 'play');
playButton.innerHTML = '<span class="mso">play_arrow</span>';
}
break;
// handle time setting
case 'start':
(audioPlayer != null) ? audioPlayer.currentTime = parseFloat(newPropertyValue) : console.log("Audio player not available");
break;
// handle end setting
case 'end':
break;
// handle playbackrate setting
case 'playbackrate':
(audioPlayer != null) ? audioPlayer.playbackRate = newPropertyValue : console.log("Audio player not available");
break;
// handle progressbar setting
case 'progressbar':
const timeInfo = this.shadowRoot.querySelector('#timeInfo');
if(timeInfo) timeInfo.style.display = newPropertyValue === 'true' ? 'block' : 'none';
break;
// handle playlist setting
case 'playlist':
const tracksDiv = this.shadowRoot.querySelector('#tracks');
const tracksButton = this.shadowRoot.querySelector('#playlist_removeButton');
if(tracksDiv && tracksButton) {
tracksDiv.style.display = newPropertyValue === 'true' ? 'block' : 'none';
tracksButton.style.display = newPropertyValue === 'true' ? 'inline-block' : 'none';
}
// handle height setting
case 'height':
try {
playerDiv.style.height = newPropertyValue;
} catch (error) {
console.log("Warning: playerDiv.style.height could not be set ", error);
}
break;
// handle width setting
case 'width':
try {
playerDiv.style.width = newPropertyValue;
} catch (error) {
console.log("Warning: playerDiv.style.width could not be set ", error);
}
break;
// handle tracks setting
case 'tracks':
// get the tracks from custom element property and parse it to JSON
const tracks = JSON.parse(this.props.tracks);
const track = this.props.track;
this.render();
break;
// handle default
default:
console.log("Invalid property: '"+property+"'");
}
}
/**
* Adds event listeners to various elements in the audio player component.
* These event listeners handle play/pause button clicks, track toggler clicks,
* audio player events (duration change, time update), progress slider input,
* and playlist remove button clicks.
*/
addEventListeners() {
const audioPlayer = this.shadowRoot.querySelector('#audioPlayer');
const progressSlider = this.shadowRoot.querySelector('#progressSlider');
const currentTimeDisplay = this.shadowRoot.querySelector('#currentTime');
const totalTimeDisplay = this.shadowRoot.querySelector('#totalTime');
/** Event listener for play/pause button */
this.shadowRoot.querySelectorAll('#play_arrowButton').forEach(el => {
el.addEventListener('click', (evt) => {
this.set("state", evt.currentTarget.getAttribute('title'));
});
});
/**
* Event listener for prev/next buttons.
* It listens to all elements with class .track-toggler and reads the data-trackstep attribute to get an info how many tracks
* should be forwarded or rewinded. This allows for buttons to forward or rewind any number of tracks -> +/-n steps
*/
this.shadowRoot.querySelectorAll('.track-toggler').forEach(el => {
el.addEventListener('click', (evt) => {
const tracksJSON = JSON.parse(this.props.tracks);
const trackStep = evt.currentTarget.dataset.trackstep;
const trackIdx = evt.currentTarget.dataset.trackidx;
// if trackIdx is available, it will be used as next track index
if(trackIdx !== undefined) {
console.log("Using track index from dataset: ", trackIdx);
this.set('track', trackIdx);
}
// if trackIdx is not available, the next track index will be calculated by adding the track step to the current track index
else {
var nextTrackIndex = (parseInt(this.props.track) + parseInt(trackStep));
if(nextTrackIndex < 0) { nextTrackIndex = tracksJSON.length - 1 }
if(nextTrackIndex >= tracksJSON.length) { nextTrackIndex = 0 }
this.set('track', nextTrackIndex);
}
});
});
/** Event listeners for audio player */
this.shadowRoot.querySelectorAll('#audioPlayer').forEach(el => {
// Event listener for duration change to update total time display
el.addEventListener('durationchange', (evt) => {
const totalMinutes = Math.floor(audioPlayer.duration / 60);
const totalSeconds = Math.floor(audioPlayer.duration % 60);
if(totalTimeDisplay) totalTimeDisplay.textContent = `${totalMinutes}:${totalSeconds < 10 ? '0' : ''}${totalSeconds}`;
});
el.addEventListener('timeupdate', (evt) => {
// Send update event to host
const event = new CustomEvent('communicate-time-update', {
detail: { time: audioPlayer.currentTime },
bubbles: true
});
this.dispatchEvent(event);
// update current time display
if(currentTimeDisplay){
const currentMinutes = Math.floor(audioPlayer.currentTime / 60);
const currentSeconds = Math.floor(audioPlayer.currentTime % 60);
currentTimeDisplay.textContent = `${currentMinutes}:${currentSeconds < 10 ? '0' : ''}${currentSeconds}`;
}
// update progress slider
if(progressSlider){
const progress = (audioPlayer.currentTime / audioPlayer.duration) * 100;
progressSlider.value = progress;
}
// if audioPlayer is currently playing and end is reached, pause there
const end = Number(this.props.end);
if(this.props.state === 'play' && !isNaN(Number(end)) && end > 0 ) {
if (audioPlayer.currentTime >= end || audioPlayer.currentTime >= audioPlayer.duration) {
this.set('state', 'pause');
}
}
});
});
/** Event listeners for tracking progress slider */
this.shadowRoot.querySelectorAll('#progressSlider').forEach(el => {
el.addEventListener('input', (evt) => {
audioPlayer.currentTime = (evt.target.value / 100) * audioPlayer.duration;
});
});
this.shadowRoot.querySelectorAll('#playlist_removeButton').forEach(el => {
el.addEventListener('click', (evt) => {
const tracksDiv = this.shadowRoot.querySelector('#tracks');
const tracksButton = this.shadowRoot.querySelector('#playlist_removeButton');
tracksButton.innerHTML = tracksDiv.style.display === 'none' ? '<span class="mso">playlist_remove</span>' : '<span class="mso">playlist_add</span>';
tracksDiv.style.display = tracksDiv.style.display === 'none' ? 'block' : 'none';
});
});
}
}
/** Define the custom element */
customElements.define('edirom-audio-player', EdiromAudioPlayer);