Skip to content

Commit ebb8e4c

Browse files
committed
Multimedia Player: added default cc functionality
1 parent 9529e94 commit ebb8e4c

File tree

8 files changed

+218
-7
lines changed

8 files changed

+218
-7
lines changed

site/pages/docs/ref/multimedia/multimedia-en.hbs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,21 @@
230230
</dl>
231231
</td>
232232
</tr>
233+
<tr>
234+
<td><code>closedCaptions</code></td>
235+
<td>
236+
<p>Displays the closed captions by default when the video is played for the first time.</p>
237+
</td>
238+
<td>Add <pre><code>data-wb-mltmd='{"closedCaptions": true}'</code></pre> to the element with <code>class="wb-mltmd"</code>.</td>
239+
<td>
240+
<dl>
241+
<dt><code>false (default)</code></dt>
242+
<dd>The video does not display closed captions on first playback</dd>
243+
<dt><code>true</code></dt>
244+
<dd>The video displays closed captions on first playback</dd>
245+
</dl>
246+
</td>
247+
</tr>
233248
</tbody>
234249
</table>
235250
</section>

site/pages/docs/ref/multimedia/multimedia-fr.hbs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,21 @@
231231
</dl>
232232
</td>
233233
</tr>
234+
<tr>
235+
<td><code>closedCaptions</code></td>
236+
<td>
237+
<p>Affiche les sous-titres par défaut lorsque la vidéo est lue pour la première fois.</p>
238+
</td>
239+
<td>Ajoutez <pre><code>data-wb-mltmd='{"closedCaptions": true}'</code></pre> à l'élément avec l'attribut <code>class="wb-mltmd"</code>.</td>
240+
<td>
241+
<dl>
242+
<dt><code>false (par défaut)</code></dt>
243+
<dd>La vidéo n'affiche pas de sous-titres lors de la première lecture</dd>
244+
<dt><code>true</code></dt>
245+
<dd>La vidéo affiche des sous-titres lors de la première lecture</dd>
246+
</dl>
247+
</td>
248+
</tr>
234249
</tbody>
235250
</table>
236251
</section>

src/plugins/multimedia/_base.scss

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,12 @@
117117
}
118118

119119
&.cc_on {
120-
.wb-mm-cc {
121-
display: table;
122-
height: calc(#{$line-height-base * $mm-cc-lines}em + #{$mm-cc-padding * 2});
123-
padding: $mm-cc-padding;
120+
&.played:not(.youtube) {
121+
.wb-mm-cc {
122+
display: table;
123+
height: calc(#{$line-height-base * $mm-cc-lines}em + #{$mm-cc-padding * 2});
124+
padding: $mm-cc-padding;
125+
}
124126
}
125127

126128
&:not(.errmsg) {

src/plugins/multimedia/multimedia-en.hbs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,40 @@
306306
</details>
307307
</section>
308308
</div>
309+
310+
311+
<div class="row">
312+
<section class="col-md-6">
313+
<h3>MPEG4 (H264 + AAC) and WebM (VP8) source with closed captions by default</h3>
314+
<figure class="wb-mltmd" data-wb-mltmd='{"closedCaptions": true}'>
315+
<video poster="demo/img/video1-en.jpg" title="Looking for a Job">
316+
<source type="video/webm" src="https://wet-boew.github.io/wet-boew-attachments/videos/video1-en.webm" />
317+
<source type="video/mp4" src="https://wet-boew.github.io/wet-boew-attachments/videos/video1-en.mp4" />
318+
<track src="cpts-lg-en.html" kind="captions" data-type="text/html" srclang="en" label="English" />
319+
</video>
320+
<figcaption>
321+
<p>Looking for a Job (<a href="cpts-lg-en.html">Transcript</a>)</p>
322+
</figcaption>
323+
</figure>
324+
325+
<details class="mrgn-tp-md mrgn-bttm-lg">
326+
<summary>View code</summary>
327+
<section>
328+
<h4>In-page HTML</h4>
329+
<pre><code>&lt;figure class="wb-mltmd" data-wb-mltmd='{"closedCaptions": true}'&gt;
330+
&lt;video poster="demo/img/video1-en.jpg" title="Looking for a Job"&gt;
331+
&lt;source type="video/webm" src="https://wet-boew.github.io/wet-boew-attachments/videos/video1-en.webm" /&gt;
332+
&lt;source type="video/mp4" src="https://wet-boew.github.io/wet-boew-attachments/videos/video1-en.mp4" /&gt;
333+
&lt;track src="cpts-lg-en.html" kind="captions" data-type="text/html" srclang="en" label="English" /&gt;
334+
&lt;/video&gt;
335+
&lt;figcaption&gt;
336+
&lt;p&gt;Looking for a Job (&lt;a href="cpts-lg-en.html"&gt;Transcript&lt;/a&gt;)&lt;/p&gt;
337+
&lt;/figcaption&gt;
338+
&lt;/figure&gt;</code></pre>
339+
</section>
340+
</details>
341+
</section>
342+
</div>
309343
</section>
310344

311345
<section id="mltmd-overlay" class="wb-overlay modal-content overlay-def wb-popup-full">

src/plugins/multimedia/multimedia-fr.hbs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,39 @@
316316
</details>
317317
</section>
318318
</div>
319+
320+
<div class="row">
321+
<section class="col-md-6">
322+
<h3>MPEG4 (H264 + AAC) et WebM (VP8) avec sous-titre par défaut</h3>
323+
<figure class="wb-mltmd" data-wb-mltmd='{"closedCaptions": true}'>
324+
<video poster="demo/img/video1-fr.jpg" title="Trouver un emploi">
325+
<source type="video/webm" src="https://wet-boew.github.io/wet-boew-attachments/videos/video1-fr.webm" />
326+
<source type="video/mp4" src="https://wet-boew.github.io/wet-boew-attachments/videos/video1-fr.mp4" />
327+
<track src="cpts-lg-fr.html" kind="captions" data-type="text/html" srclang="fr" label="Français" />
328+
</video>
329+
<figcaption>
330+
<p>Trouver un emploi (<a href="cpts-lg-fr.html">Transcription</a>)</p>
331+
</figcaption>
332+
</figure>
333+
334+
<details class="mrgn-tp-md mrgn-bttm-lg">
335+
<summary>Visualiser le code</summary>
336+
<section>
337+
<h4>HTML dans la page</h4>
338+
<pre><code>&lt;figure class=&quot;wb-mltmd&quot; data-wb-mltmd='{"closedCaptions": true}'&gt;
339+
&lt;video poster=&quot;demo/img/video1-fr.jpg&quot; title=&quot;Trouver un emploi&quot;&gt;
340+
&lt;source type=&quot;video/webm&quot; src=&quot;https://wet-boew.github.io/wet-boew-attachments/videos/video1-fr.webm&quot; /&gt;
341+
&lt;source type=&quot;video/mp4&quot; src=&quot;https://wet-boew.github.io/wet-boew-attachments/videos/video1-fr.mp4&quot; /&gt;
342+
&lt;track src=&quot;cpts-lg-fr.html&quot; kind=&quot;captions&quot; data-type=&quot;text/html&quot; srclang=&quot;fr&quot; label=&quot;Français&quot; /&gt;
343+
&lt;/video&gt;
344+
&lt;figcaption&gt;
345+
&lt;p&gt;Trouver un emploi (&lt;a href=&quot;cpts-lg-fr.html&quot;&gt;Transcription&lt;/a&gt;)&lt;/p&gt;
346+
&lt;/figcaption&gt;
347+
&lt;/figure&gt;</code></pre>
348+
</section>
349+
</details>
350+
</section>
351+
</div>
319352
</section>
320353

321354
<section id="mltmd-overlay" class="wb-overlay modal-content overlay-def wb-popup-full">

src/plugins/multimedia/multimedia-youtube-en.hbs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,32 @@
134134
</section>
135135
</div>
136136

137+
<div class="row">
138+
<section class="col-md-6">
139+
<h2>Example with closed captions by default</h2>
140+
<figure class="wb-mltmd" data-wb-mltmd='{"closedCaptions": true}'>
141+
<video title="Suspect">
142+
<source type="video/youtube" src="https://www.youtube.com/watch?v=9znKJqnFuuY">
143+
</video>
144+
<figcaption>
145+
<p>Suspect (<a href="https://www.canada.ca/en/health-canada/services/video/suspect.html#trans">Transcript</a>)</p>
146+
</figcaption>
147+
</figure>
148+
149+
<details class="mrgn-tp-md">
150+
<summary>View code</summary>
151+
<pre><code>&lt;figure class=&quot;wb-mltmd&quot; data-wb-mltmd='{"closedCaptions": true}'&gt;
152+
&lt;video title=&quot;Suspect&quot;&gt;
153+
&lt;source type=&quot;video/youtube&quot; src=&quot;https://www.youtube.com/watch?v=9znKJqnFuuY&quot; /&gt;
154+
&lt;/video&gt;
155+
&lt;figcaption&gt;
156+
&lt;p&gt;Suspect&lt;/p&gt;
157+
&lt;/figcaption&gt;
158+
&lt;/figure&gt;</code></pre>
159+
</details>
160+
</section>
161+
</div>
162+
137163
<div class="row">
138164
<section class="col-md-6">
139165
<h2>Video with defined video frame size</h2>

src/plugins/multimedia/multimedia-youtube-fr.hbs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,32 @@
133133
</section>
134134
</div>
135135

136+
<div class="row">
137+
<section class="col-md-6">
138+
<h2>Exemple avec sous-titre par défaut</h2>
139+
<figure class="wb-mltmd" data-wb-mltmd='{"closedCaptions": true}'>
140+
<video title="Suspect">
141+
<source type="video/youtube" src="https://www.youtube.com/watch?v=LLg-UsTr7us">
142+
</video>
143+
<figcaption>
144+
<p>Suspect (<a href="https://www.canada.ca/fr/sante-canada/services/video/suspect.html#trans">Transcription</a>)</p>
145+
</figcaption>
146+
</figure>
147+
148+
<details class="mrgn-tp-md">
149+
<summary>View code</summary>
150+
<pre><code>&lt;figure class=&quot;wb-mltmd&quot; data-wb-mltmd='{"closedCaptions": true}'&gt;
151+
&lt;video title=&quot;Suspect&quot;&gt;
152+
&lt;source type=&quot;video/youtube&quot; src=&quot;https://www.youtube.com/watch?v=LLg-UsTr7us&quot; /&gt;
153+
&lt;/video&gt;
154+
&lt;figcaption&gt;
155+
&lt;p&gt;Suspect&lt;/p&gt;
156+
&lt;/figcaption&gt;
157+
&lt;/figure&gt;</code></pre>
158+
</details>
159+
</section>
160+
</div>
161+
136162
<div class="row">
137163
<section class="col-md-6">
138164
<h2>Vidéo avec dimensions définies du cadre de la vidéo</h2>

src/plugins/multimedia/multimedia.js

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var componentName = "wb-mltmd",
2727
templateLoadedEvent = "templateloaded" + selector,
2828
cuepointEvent = "cuepoint" + selector,
2929
captionClass = "cc_on",
30+
firstPlayClass = "played",
31+
defaultCaptions = false,
3032
multimediaEvents = [
3133
"durationchange",
3234
"playing",
@@ -581,7 +583,9 @@ var componentName = "wb-mltmd",
581583
},
582584
$mltmPlayerElm,
583585
mltmPlayerElm,
584-
isMuted;
586+
isMuted,
587+
settings,
588+
$ccButton;
585589

586590
switch ( event.data ) {
587591
case null: // init
@@ -591,12 +595,30 @@ var componentName = "wb-mltmd",
591595

592596
// Put video on mute if the video is muted on init, run once
593597
$mltmPlayerElm = $media.parentsUntil( selector ).parent();
598+
mltmPlayerElm = $mltmPlayerElm.get( 0 );
599+
594600

595601
// Mute the player, GUI
596602
if ( $mltmPlayerElm.data( "putMutedOnInit" ) ) {
597603
youTubeApi.call( $mltmPlayerElm.get( 0 ), "setMuted", true );
598604
$mltmPlayerElm.data( "putMutedOnInit", false );
599605
}
606+
607+
// Check if closed captions should be enabled by default
608+
settings = wb.getData( mltmPlayerElm, componentName );
609+
610+
if ( settings !== undef ) {
611+
if ( settings.closedCaptions !== null && settings.closedCaptions === true ) {
612+
youTubeApi.call( mltmPlayerElm, "setCaptionsVisible", true );
613+
614+
$ccButton = $( mltmPlayerElm ).find( ".cc" );
615+
616+
$ccButton.attr( {
617+
"title": i18nText.cc_off,
618+
"aria-pressed": true
619+
} );
620+
}
621+
}
600622
break;
601623
case -1:
602624
event.target.unMute();
@@ -706,6 +728,10 @@ $document.on( initializedEvent, selector, function( event ) {
706728
if ( settings !== undef ) {
707729
data.shareUrl = settings.shareUrl;
708730
data.fullscreen = settings.fullscreenBtn || false;
731+
data.closedCaptions = settings.closedCaptions || false;
732+
if ( data.closedCaptions !== undef ) {
733+
defaultCaptions = data.closedCaptions;
734+
}
709735
}
710736

711737
$this.addClass( type );
@@ -868,7 +894,9 @@ $document.on( renderUIEvent, selector, function( event, type, data ) {
868894
captionsUrl = wb.getUrlParts( data.captions ),
869895
currentUrl = wb.getUrlParts( window.location.href ),
870896
$media = data.media,
871-
$eventReceiver;
897+
$eventReceiver,
898+
$button,
899+
$buttonSpan;
872900

873901
$media
874902
.after( tmpl( template, data ) )
@@ -926,6 +954,31 @@ $document.on( renderUIEvent, selector, function( event, type, data ) {
926954
if ( data.fullscreen ) {
927955
$this.attr( "data-fullscreen-btn", true );
928956
}
957+
958+
// Show captions by default if configured to do so
959+
let captionStatus;
960+
961+
if ( $this.data( "wbMltmd" ) !== undef ) {
962+
captionStatus = $this.data( "wbMltmd" ).closedCaptions;
963+
}
964+
965+
if ( captionStatus !== undef && defaultCaptions !== undef && defaultCaptions === true ) {
966+
967+
$this.addClass( captionClass );
968+
969+
// Trigger caption visibility change event only on the first time the video plays
970+
defaultCaptions = true;
971+
972+
$button = $this.find( ".cc" );
973+
$button
974+
.attr( {
975+
"title": i18nText.cc_off,
976+
"aria-pressed": true
977+
} );
978+
979+
$buttonSpan = $button.find( ".wb-inv" );
980+
$buttonSpan.text( i18nText.cc_off );
981+
}
929982
}
930983
} );
931984

@@ -1053,7 +1106,8 @@ $document.on( multimediaEvents, selector, function( event, simulated ) {
10531106
$this = $( eventTarget ),
10541107
invStart = "<span class='wb-inv'>",
10551108
invEnd = "</span>",
1056-
currentTime, $button, $slider, buttonData, isPlay, isMuted, isCCVisible, skipTo, volume;
1109+
currentTime, $button, $slider, buttonData, isPlay, isMuted, isCCVisible, skipTo, volume,
1110+
firstPlay = false;
10571111
switch ( eventType ) {
10581112
case "playing":
10591113
case "pause":
@@ -1064,6 +1118,12 @@ $document.on( multimediaEvents, selector, function( event, simulated ) {
10641118
if ( isPlay ) {
10651119
$this.addClass( "playing" );
10661120
$this.find( ".progress" ).addClass( "active" );
1121+
1122+
if ( firstPlay === false ) {
1123+
$this.addClass( firstPlayClass );
1124+
firstPlay = true;
1125+
}
1126+
10671127
} else {
10681128
if ( eventType === "ended" ) {
10691129
this.loading = clearTimeout( this.loading );

0 commit comments

Comments
 (0)