Skip to content

Commit

Permalink
refactor(picture-in-picture): audio mode change handler method
Browse files Browse the repository at this point in the history
  • Loading branch information
amtins committed Aug 28, 2022
1 parent 3762fc9 commit e703ebd
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions src/js/control-bar/picture-in-picture-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,7 @@ class PictureInPictureToggle extends Button {

this.on(player, ['enterpictureinpicture', 'leavepictureinpicture'], (e) => this.handlePictureInPictureChange(e));
this.on(player, ['disablepictureinpicturechanged', 'loadedmetadata'], (e) => this.handlePictureInPictureEnabledChange(e));

this.on(player, ['loadedmetadata', 'audioonlymodechange', 'audiopostermodechange'], () => {
// This audio detection will not detect HLS or DASH audio-only streams because there was no reliable way to detect them at the time
const isSourceAudio = player.currentType().substring(0, 5) === 'audio';

if (isSourceAudio || player.audioPosterMode() || player.audioOnlyMode()) {
if (player.isInPictureInPicture()) {
player.exitPictureInPicture();
}
this.hide();
} else {
this.show();
}

});
this.on(player, ['loadedmetadata', 'audioonlymodechange', 'audiopostermodechange'], () => this.handlePictureInPictureAudioModeChange());

// TODO: Deactivate button on player emptied event.
this.disable();
Expand All @@ -66,6 +52,29 @@ class PictureInPictureToggle extends Button {
return `vjs-picture-in-picture-control ${super.buildCSSClass()}`;
}

/**
* Displays or hides the button depending on the audio mode detection.
* Exits picture-in-picture if it is enabled when switching to audio mode.
*/
handlePictureInPictureAudioModeChange() {
// This audio detection will not detect HLS or DASH audio-only streams because there was no reliable way to detect them at the time
const isSourceAudio = this.player_.currentType().substring(0, 5) === 'audio';
const isAudioMode =
isSourceAudio || this.player_.audioPosterMode() || this.player_.audioOnlyMode();

if (!isAudioMode) {
this.show();

return;
}

if (this.player_.isInPictureInPicture()) {
this.player_.exitPictureInPicture();
}

this.hide();
}

/**
* Enables or disables button based on document.pictureInPictureEnabled property value
* or on value returned by player.disablePictureInPicture() method.
Expand Down

0 comments on commit e703ebd

Please sign in to comment.