diff --git a/src/js/captions.js b/src/js/captions.js index c22a1ddb0..302bab0b8 100644 --- a/src/js/captions.js +++ b/src/js/captions.js @@ -148,8 +148,13 @@ const captions = { }); } + // Update track by track node if track and language exist + if (tracks.includes(currentTrackNode) && languageExists) { + captions.setNode.call(this, currentTrackNode); + captions.toggle.call(this, active && languageExists); + } // Update language first time it matches, or if the previous matching track was removed - if ((languageExists && this.language !== language) || !tracks.includes(currentTrackNode)) { + else if ((languageExists && this.language !== language) || !tracks.includes(currentTrackNode)) { captions.setLanguage.call(this, language); captions.toggle.call(this, active && languageExists); } @@ -286,6 +291,17 @@ const captions = { } }, + setNode(input, passive = true) { + if (!is.track(input)) { + this.debug.warn('Invalid track argument', input); + return; + } + + // Set currentTrack + const tracks = captions.getTracks.call(this); + captions.set.call(this, tracks.indexOf(input), passive); + }, + // Set captions by language // Used internally for the language setter with the passive option forced to false setLanguage(input, passive = true) { diff --git a/src/js/plyr.d.ts b/src/js/plyr.d.ts index e81d97e7e..2db71080e 100644 --- a/src/js/plyr.d.ts +++ b/src/js/plyr.d.ts @@ -122,6 +122,11 @@ declare class Plyr { */ currentTrack: number; + /** + * Sets the preferred captions node for the player. + */ + nodeTrack: TextTrack; + /** * Gets or sets the preferred captions language for the player. The setter accepts an ISO twoletter language code. Support for the languages is dependent on the captions you include. * If your captions don't have any language data, or if you have multiple tracks with the same language, you may want to use currentTrack instead. diff --git a/src/js/plyr.js b/src/js/plyr.js index 895b54ce8..14dca159c 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -968,6 +968,14 @@ class Plyr { return toggled ? currentTrack : -1; } + /** + * Set the wanted track node for captions + * @param {TextTrack} input - Two character ISO language code (e.g. EN, FR, PT, etc) + */ + set nodeTrack(input) { + captions.setNode.call(this, input, false); + } + /** * Set the wanted language for captions * Since tracks can be added later it won't update the actual caption track until there is a matching track