Skip to content

Commit

Permalink
Add afterSongEnded and onSongEnd listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
Guido van Helvoort committed Nov 27, 2022
1 parent ef8f940 commit a5fa24b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/CDGKaraokePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default class CDGKaraokePlayer {
width = 2 * WIDTH,
height = 2 * HEIGHT,
backgroundContainer,
onSongEnd,
canvas = this.createDisplayCanvas(width, height),
ctx = this.createCanvasContext(canvas),
audio = this.createAudio(),
Expand All @@ -46,10 +47,12 @@ export default class CDGKaraokePlayer {
this.canvas = canvas;
this.ctx = ctx;
this.audio = audio;
this.onSongEnd = onSongEnd;

// Create the CDGPlayer instance
this.player = new CDGPlayer({
afterRender: this.afterRender,
afterSongEnded: this.afterSongEnded,
...playerOptions,
});

Expand Down Expand Up @@ -136,6 +139,17 @@ export default class CDGKaraokePlayer {
this.copyContextToCanvas(context);
};

/**
* Gets fired when a song ends
*
* @param {CDGContext} context - CDG rendering context
*/
afterSongEnded = (context) => {
if (this.onSongEnd) {
this.onSongEnd(context);
}
};

/**
* Sets the background color of the container
*/
Expand Down
8 changes: 8 additions & 0 deletions src/CDGPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,17 @@ export default class CDGPlayer {
* @param {Object} [options] - CDG player options
* @param {Object} [options.contextOptions] - options for the CDG context
* @param {function} [options.afterRender] - function to call after rendering a frame
* @param {function} [options.afterSongEnded] - function to call ater the song ended
*/
constructor({
contextOptions = {},
context = this.createContext(contextOptions),
afterRender,
afterSongEnded,
} = {}) {
this.context = context;
this.afterRender = afterRender;
this.afterSongEnded = afterSongEnded;
}

/**
Expand Down Expand Up @@ -240,6 +243,11 @@ export default class CDGPlayer {
* @return {self}
*/
stop() {
// TODO: dirty, this class has to know about the id of the audio element
if (document.getElementById("audio").ended) {
this.afterSongEnded && this.afterSongEnded(this.context);
}

cancelFrame(this.frameId);
this.frameId = null;
this.lastSyncPos = null;
Expand Down

0 comments on commit a5fa24b

Please sign in to comment.