Skip to content

Commit 2b6a90f

Browse files
committed
added loop song and loop queue (on/off) examples
1 parent d368b11 commit 2b6a90f

File tree

3 files changed

+78
-12
lines changed

3 files changed

+78
-12
lines changed

examples/simple-player/client/src/bootstrap.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ import { PlayerUI } from './library/player/ui.js'
1414
const options: ICoreOptions = {
1515
soundsBaseUrl: 'http://127.0.0.1:35000/streaming/music/',
1616
loadPlayerMode: PlayerCore.PLAYER_MODE_AUDIO,
17-
loopQueue: true,
1817
addAudioElementsToDom: true,
1918
}
2019
/*const options: ICoreOptions = {
2120
soundsBaseUrl: 'http://127.0.0.1:35000/static/music/',
2221
loadPlayerMode: PlayerCore.PLAYER_MODE_AJAX,
23-
loopQueue: true,
2422
}*/
2523

2624
// create an instance of the player
@@ -125,4 +123,6 @@ myPlaylist.forEach((song) => {
125123

126124
console.log('sound got added to queue: ', sound)
127125

128-
})
126+
})
127+
128+
playerUI.refresh()

examples/simple-player/client/src/library/player/ui.ts

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,11 @@ class PlayerUI {
204204

205205
event.preventDefault()
206206

207-
const $button = event.target as HTMLElement
207+
let $button = event.target as HTMLElement
208+
209+
if ($button.localName === 'span') {
210+
$button = $button.parentElement
211+
}
208212

209213
if ($button.id === 'js-first') {
210214
this.player.play({ whichSound: 'first' })
@@ -224,15 +228,30 @@ class PlayerUI {
224228
}
225229
}
226230

227-
if ($button.id === 'pause') {
228-
this.player.pause()
231+
if ($button.id === 'js-stop') {
232+
this.player.stop()
229233
}
230234

231-
if ($button.id === 'stop') {
232-
this.player.stop()
235+
if ($button.id === 'js-loop-song') {
236+
const currentSound = this.player.getCurrentSound()
237+
if (currentSound.getLoop()) {
238+
currentSound.setLoop(false)
239+
} else {
240+
currentSound.setLoop(true)
241+
}
242+
this._updateLoopSong()
233243
}
234244

235-
if ($button.id === 'disconnect') {
245+
if ($button.id === 'js-loop-queue') {
246+
if (this.player.getLoopQueue()) {
247+
this.player.setLoopQueue(false)
248+
} else {
249+
this.player.setLoopQueue(true)
250+
}
251+
this._updateLoopQueue()
252+
}
253+
254+
if ($button.id === 'js-disconnect') {
236255
this.player.disconnect()
237256
}
238257

@@ -310,6 +329,38 @@ class PlayerUI {
310329
this._switchPlayerContext('on')
311330
}
312331

332+
protected _updateLoopQueue(): void {
333+
334+
const isLoopQueue = this.player.getLoopQueue()
335+
const $loopQueueOn = document.getElementById('js-loop-queue-on')
336+
const $loopQueueOff = document.getElementById('js-loop-queue-off')
337+
338+
if (isLoopQueue) {
339+
$loopQueueOn.classList.remove('hidden')
340+
$loopQueueOff.classList.add('hidden')
341+
} else {
342+
$loopQueueOn.classList.add('hidden')
343+
$loopQueueOff.classList.remove('hidden')
344+
}
345+
346+
}
347+
348+
protected _updateLoopSong(): void {
349+
350+
const isLoopSound = this.player.getCurrentSound().getLoop()
351+
const $loopSongOn = document.getElementById('js-loop-song-on')
352+
const $loopSongOff = document.getElementById('js-loop-song-off')
353+
354+
if (isLoopSound) {
355+
$loopSongOn.classList.remove('hidden')
356+
$loopSongOff.classList.add('hidden')
357+
} else {
358+
$loopSongOn.classList.add('hidden')
359+
$loopSongOff.classList.remove('hidden')
360+
}
361+
362+
}
363+
313364
protected _destroyListeners(): void {
314365

315366
this._buttonsBox.removeEventListener('click', this._onClickButtonsBox.bind(this))
@@ -326,6 +377,13 @@ class PlayerUI {
326377

327378
}
328379

380+
public refresh(): void {
381+
382+
this._updateLoopQueue()
383+
this._updateLoopSong()
384+
385+
}
386+
329387
public deconstructor(): void {
330388
this._destroyListeners()
331389
}

examples/simple-player/html/main.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,18 @@ <h1>web audio api player - simple example</h1>
125125
<button id="js-byId-2" class="button" data-song-id="song2">Play song by ID (song 2)</button>
126126
<button id="js-byId-3" class="button" data-song-id="song3" data-song-play-time-offset="10">Play song by ID (song
127127
3) & start at (playTimeOffset) 10 seconds</button>
128-
<button id="pause" class="button">Pause</button>
129-
<button id="stop" class="button">Stop</button>
128+
<button id="js-stop" class="button">Stop</button>
130129
<br>
131-
<button id="disconnect" class="button">Disconnect player</button>
130+
<button id="js-loop-song" class="button">
131+
<span id="js-loop-song-on" class="hidden">Loop song: ON</span>
132+
<span id="js-loop-song-off">Loop song: OFF</span>
133+
</button>
134+
<button id="js-loop-queue" class="button">
135+
<span id="js-loop-queue-on" class="hidden">Loop queue: ON</span>
136+
<span id="js-loop-queue-off">Loop queue: OFF</span>
137+
</button>
138+
<br>
139+
<button id="js-disconnect" class="button">Disconnect player</button>
132140
</section>
133141

134142
</body>

0 commit comments

Comments
 (0)