Continuous playing between songs fixed.

This commit is contained in:
Harrison Deng 2022-04-18 01:59:54 -05:00
parent 8cb39bf9cc
commit 62145ef741
4 changed files with 29 additions and 10 deletions

View File

@ -36,18 +36,27 @@ export default class PlayListSong {
* @returns {HTMLAudioElement} The audio element that represents this song.
*/
fetchAudio(onReady) {
console.log("Fetching audio...");
if (this.#audio) {
if (this.#ready) {
console.log("Already ready.");
if (onReady) onReady(this.#audio);
} else if (onReady) {
this.#audio.addEventListener("canplaythrough", () => {
onReady(this.#audio);
}, { once: true });
}
return this.#audio;
}
this.#audio = new Audio(this.#url);
console.log("Audio is not ready. Loading.");
console.log("Fetching from url: " + this.#url);
const listener = () => {
this.#ready = true;
console.log("attempting to invoke onReady.");
console.log(onReady);
if (onReady && this.isAudioInstantiated()) {
onReady(this.#audio);
console.log("onReady invoked.");
}
};
this.#audio.addEventListener("canplaythrough", listener, { once: true });
@ -180,7 +189,9 @@ export default class PlayListSong {
* Stops the visualizer.
*/
unloadVisualizer() {
this.#visualizer.stop();
this.#visualizer = null;
if (this.#visualizer) {
this.#visualizer.stop();
this.#visualizer = null;
}
}
}

View File

@ -1,4 +1,4 @@
import "../styles/songplayer.css";
import "../styles/songPlayer.css";
import PlayListSong from "./PlaylistSong.js";
import SongPlaylist from "./SongPlaylist.js";
@ -10,6 +10,7 @@ export default class SongPlayer {
#current = 0;
#volume = 1;
#autoplay = false;
#wasPlaying = false;
#playlistChangeListeners = [];
#currentSongChangeListeners = [];
#volumeChangeListeners = [];
@ -95,7 +96,7 @@ export default class SongPlayer {
this.currentSong.fetchAudio().addEventListener(key, listener);
});
});
if (this.#autoplay) {
if (this.#wasPlaying || this.#autoplay) {
this.playCurrent();
}
return true;
@ -110,12 +111,12 @@ export default class SongPlayer {
}
playCurrent() {
console.log("Attempting to play...");
console.log("playing " + this.#current);
this.currentSong.fetchAudio((audio) => {
audio.volume = this.volume;
audio.play();
console.log("playing...");
});
this.#wasPlaying = true;
}
/**

View File

@ -1,13 +1,14 @@
.player.play-btn {
box-sizing: border-box;
height: 60px;
height: 48px;
width: 48;
transition: 80ms all ease;
will-change: border-width;
cursor: pointer;
border-color: transparent transparent transparent black;
border-style: solid;
border-width: 30px 0px 30px 50px;
border-width: 24px 0px 24px 48px;
}
.player.play-btn.pause {
@ -18,10 +19,14 @@
.player.next {
border-style: none;
width: 48px;
height: 48px;
}
.player.previous {
border-style: none;
width: 48px;
height: 48px;
}
table.player {

View File

@ -6,4 +6,6 @@ playlist.add("/audio/moments.mp3", "moments", "Lost Identities x Robbie Rosen");
playlist.add("/audio/XXI.mp3", "XXI", "QR");
const player = ask.player.createSongPlayer(playlist);
const playerElem = document.getElementById("player-showcase");
playerElem.appendChild(player.generatePlayElement());
playerElem.appendChild(player.generatePlayElement());
playerElem.appendChild(player.generateNextElement());
playerElem.appendChild(player.generatePreviousElement());