Skip to content

Commit

Permalink
fix cleanups and initialisations
Browse files Browse the repository at this point in the history
  • Loading branch information
casualchameleon committed Oct 25, 2020
1 parent c58e301 commit 9c55eda
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
5 changes: 5 additions & 0 deletions modules/apis/StreamingServiceApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ export class StreamingServiceApi
return undefined;
}

cleanupPlayer(ownerId, audioId)
{
return undefined;
}

}
14 changes: 11 additions & 3 deletions modules/apis/YouTubeApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,23 @@ export class YouTubeApi extends StreamingServiceApi

findOrCreatePlayer(ownerId, audioId, sourceId)
{
var player = this.getPlayerAt(`youtube-player-${ownerId}-${audioId}`);
let player = this.getPlayerAt(`youtube-player-${ownerId}-${audioId}`);
if (player === null || player === undefined)
{
player = new YouTubePlayer(ownerId, audioId, sourceId);
// display: none;
$('body').append(`<div class="yt-player"><div id="${player.playerId}"></div></div>`);
//player.createPlayer();
}
return player;
}

cleanupPlayer(ownerId, audioId)
{
let playerId = `youtube-player-${ownerId}-${audioId}`;
let player = this.getPlayerAt(playerId);
if (player) {
player.delete();
this.setPlayerAt(playerId, undefined);
}
}

}
6 changes: 5 additions & 1 deletion modules/apis/YouTubePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,14 @@ export class YouTubePlayer
{
if (this.hasPlayer())
{
if (this.isPlaying())
{
this.player.stopVideo();
}
this.player.destroy();
this.player = null;
this.bPlayerReady = false;
$(`#${this.playerId}`).parent().remove();
$(`div#${this.playerId}`).closest('div.yt-player').remove();
}
}

Expand Down
27 changes: 20 additions & 7 deletions modules/patches/Playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ Playlist.prototype.findOrCreatePlayer = function(sound) {
return null;
};

Playlist.prototype.cleanupPlayer = function (sound) {
if (sound.flags.bIsStreamed && sound.flags.streamingApi !== undefined)
{
getApi(sound.flags.streamingApi).cleanupPlayer(
this.id, sound._id
);
}
}

/**
* Set up the Howl object by calling the core AudioHelper utility
* @param {Object} sound The PlaylistSound for which to create an audio object
Expand All @@ -24,7 +33,6 @@ overrideFunc(Playlist.prototype, '_createAudio', function(super_createAudio, sou
super_createAudio.call(this, sound);
return;
}
this.findOrCreatePlayer(sound);
});

overrideFunc(Playlist.prototype, 'playSound', function(super_playSound, sound)
Expand All @@ -35,12 +43,17 @@ overrideFunc(Playlist.prototype, 'playSound', function(super_playSound, sound)
return;
}

const ytPlayer = this.findOrCreatePlayer(sound);
console.log('playSound', sound.flags.streamingId, sound.playing);
ytPlayer.setSourceId(sound.flags.streamingId);
ytPlayer.setLoop(sound.repeat);
ytPlayer.setVolume(sound.volume * game.settings.get("core", "globalPlaylistVolume"));
ytPlayer.ensurePlaying(sound.playing);
if (sound.playing) {

let ytPlayer = this.findOrCreatePlayer(sound);
console.log('playSound', sound.flags.streamingId, sound.playing);
ytPlayer.setSourceId(sound.flags.streamingId);
ytPlayer.setLoop(sound.repeat);
ytPlayer.setVolume(sound.volume * game.settings.get("core", "globalPlaylistVolume"));
ytPlayer.ensurePlaying(sound.playing);
} else {
let ytPlayer = this.cleanupPlayer(sound);
}
});

overrideFunc(Playlist.prototype, '_onDeleteEmbeddedEntity', function(
Expand Down

0 comments on commit 9c55eda

Please sign in to comment.