Worked around Safari bug causing captions to persist

Worked around Safari bug causing captions to persist after active cues
removed and text tracks disabled
This commit is contained in:
amarcu5
2017-08-24 13:53:10 +01:00
parent c62aa6de1b
commit 0e4d709bba
3 changed files with 24 additions and 14 deletions

Binary file not shown.

View File

@@ -124,15 +124,32 @@ const prepareCaptions = function(video) {
video.addEventListener('webkitendfullscreen', toggleCaptions);
};
/**
* Removes visible Picture in Picture mode captions
* @param {HTMLVideoElement} video - video element showing captions
*/
const removeCaptions = function(video) {
track.mode = 'showing';
while (track.activeCues.length) track.removeCue(track.activeCues[0]);
// Workaround Safari bug; 'removeCue' doesn't immediately remove captions shown in Picture in Picture mode
track.addCue(new VTTCue(video.currentTime, video.currentTime, ''));
}
/**
* Updates visible captions
*/
const processCaptions = function() {
const captionElement = currentResource.captionElement();
// Hide Picture in Picture mode captions and show native captions if no longer showing captions or encountered an error
// Get handles to caption and video elements
const captionElement = currentResource.captionElement();
const video = /** @type {?HTMLVideoElement} */ (currentResource.videoElement());
// Remove old captions
removeCaptions(video);
// Show native captions if no longer showing captions or encountered an error
if (!showingCaptions || !captionElement) {
track.mode = 'disabled';
if (captionElement) captionElement.style.visibility = '';
return;
}
@@ -144,16 +161,9 @@ const processCaptions = function() {
const unprocessedCaption = captionElement.textContent;
if (unprocessedCaption == lastUnprocessedCaption) return;
lastUnprocessedCaption = unprocessedCaption;
// Get handle to video (called before accessing 'track' to guarentee valid)
const video = /** @type {?HTMLVideoElement} */ (currentResource.videoElement());
// Remove old captions
track.mode = 'showing';
while (track.activeCues.length) track.removeCue(track.activeCues[0]);
// Line commented out to workaround Safari bug; 'removeCue' doesn't immediately remove captions shown in Picture in Picture mode
//if (!unprocessedCaption) return;
// Performance optimisation - early exit if caption has no content
if (!unprocessedCaption) return;
// Show correctly spaced and formatted Picture in Picture mode caption
let caption = '';

View File

@@ -10,7 +10,7 @@
<key>CFBundleShortVersionString</key>
<string>0.2.1</string>
<key>CFBundleVersion</key>
<string>60</string>
<string>61</string>
<key>Developer Identifier</key>
<string>BQ6Q24MF9X</string>
<key>URL</key>