@@ -41,7 +41,7 @@ export const enableCaptions = function(ignoreNowPlayingCheck) {
4141 const video = /** @type {?HTMLVideoElement } */ ( getResource ( ) . videoElement ( true ) ) ;
4242 if ( ! video ) return ;
4343 showingCaptions = videoPlayingPictureInPicture ( video ) ;
44- prepareCaptions ( video ) ;
44+ track = getCaptionTrack ( video ) ;
4545 processCaptions ( ) ;
4646} ;
4747
@@ -55,27 +55,35 @@ export const shouldProcessCaptions = function() {
5555} ;
5656
5757/**
58- * Prepares video for captions
58+ * Gets caption track for video (creates or returns existing track as needed)
5959 *
6060 * @param {HTMLVideoElement } video - video element that will display captions
61+ * @return {TextTrack }
6162 */
62- const prepareCaptions = function ( video ) {
63+ const getCaptionTrack = function ( video ) {
6364
6465 // Find existing caption track
65- track = null ;
6666 const allTracks = video . textTracks ;
6767 for ( let trackId = allTracks . length ; trackId -- ; ) {
6868 if ( allTracks [ trackId ] . label === TRACK_ID ) {
69- track = allTracks [ trackId ] ;
7069 info ( 'Existing caption track found' ) ;
71- break ;
70+ return allTracks [ trackId ] ;
7271 }
7372 }
74- if ( track ) return ;
7573
7674 // Otherwise create new caption track
7775 info ( 'Caption track created' ) ;
78- track = video . addTextTrack ( 'captions' , TRACK_ID , 'en' ) ;
76+ return video . addTextTrack ( 'captions' , TRACK_ID , 'en' ) ;
77+ } ;
78+
79+ /**
80+ * Adds caption tracks to all video elements
81+ */
82+ export const addVideoCaptionTracks = function ( ) {
83+ const elements = document . getElementsByTagName ( 'video' ) ;
84+ for ( let index = 0 , element ; element = elements [ index ] ; index ++ ) {
85+ getCaptionTrack ( /** @type {?HTMLVideoElement } */ ( element ) ) ;
86+ }
7987} ;
8088
8189/**
@@ -88,7 +96,10 @@ const pictureInPictureEventListener = function(video, isPlayingPictureInPicture)
8896
8997 // Toggle display of the captions and prepare video if needed
9098 showingCaptions = isPlayingPictureInPicture ;
91- if ( showingCaptions ) prepareCaptions ( video ) ;
99+ if ( showingCaptions ) {
100+ track = getCaptionTrack ( video ) ;
101+ track . mode = 'showing' ;
102+ }
92103 lastUnprocessedCaption = '' ;
93104 processCaptions ( ) ;
94105
0 commit comments