Skip to content

Commit

Permalink
Polyfill video.play to silence confusing errors
Browse files Browse the repository at this point in the history
Closes #836

Change-Id: Ie5b42bc707ce7443641e86c04ec116fd732bbbae
  • Loading branch information
joeyparrish committed Jun 5, 2017
1 parent 8fdd292 commit 53220b5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions build/types/polyfill
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
+../../lib/polyfill/patchedmediakeys_nop.js
+../../lib/polyfill/patchedmediakeys_webkit.js
+../../lib/polyfill/promise.js
+../../lib/polyfill/video_play_promise.js
+../../lib/polyfill/videoplaybackquality.js
+../../lib/polyfill/vttcue.js
59 changes: 59 additions & 0 deletions lib/polyfill/video_play_promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

goog.provide('shaka.polyfill.VideoPlayPromise');

goog.require('shaka.log');
goog.require('shaka.polyfill.register');


/**
* @namespace shaka.polyfill.VideoPlayPromise
*
* @summary A polyfill to silence the play() Promise in HTML5 video.
*/


/**
* Install the polyfill if needed.
*/
shaka.polyfill.VideoPlayPromise.install = function() {
shaka.log.debug('VideoPlayPromise.install');

if (window.HTMLMediaElement) {
var originalPlay = HTMLMediaElement.prototype.play;
HTMLMediaElement.prototype.play = function() {
var p = originalPlay.apply(this, arguments);
if (p) {
// This browser is returning a Promise from play().
// If the play() call fails or is interrupted, the Promise will be
// rejected. Some apps, however, don't listen to this Promise,
// especially since it is not available cross-browser. If the Promise
// is rejected without anyone listening for the failure, an error will
// appear in the JS console.
// To avoid confusion over this innocuous "error", we will install a
// catch handler on the Promise. This does not prevent the app from
// also catching failures and handling them. It only prevents the
// console message.
p.catch(function() {});
}
return p;
};
}
};

shaka.polyfill.register(shaka.polyfill.VideoPlayPromise.install);
1 change: 1 addition & 0 deletions shaka-player.uncompiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ goog.require('shaka.polyfill.MediaKeys');
goog.require('shaka.polyfill.MediaSource');
goog.require('shaka.polyfill.Promise');
goog.require('shaka.polyfill.VTTCue');
goog.require('shaka.polyfill.VideoPlayPromise');
goog.require('shaka.polyfill.VideoPlaybackQuality');
goog.require('shaka.polyfill.installAll');
goog.require('shaka.util.Error');

0 comments on commit 53220b5

Please sign in to comment.