Source: lib/polyfill/video_play_promise.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.polyfill.VideoPlayPromise');
  18. goog.require('shaka.log');
  19. goog.require('shaka.polyfill.register');
  20. /**
  21. * @namespace shaka.polyfill.VideoPlayPromise
  22. *
  23. * @summary A polyfill to silence the play() Promise in HTML5 video.
  24. */
  25. /**
  26. * Install the polyfill if needed.
  27. */
  28. shaka.polyfill.VideoPlayPromise.install = function() {
  29. shaka.log.debug('VideoPlayPromise.install');
  30. if (window.HTMLMediaElement) {
  31. var originalPlay = HTMLMediaElement.prototype.play;
  32. HTMLMediaElement.prototype.play = function() {
  33. var p = originalPlay.apply(this, arguments);
  34. if (p) {
  35. // This browser is returning a Promise from play().
  36. // If the play() call fails or is interrupted, the Promise will be
  37. // rejected. Some apps, however, don't listen to this Promise,
  38. // especially since it is not available cross-browser. If the Promise
  39. // is rejected without anyone listening for the failure, an error will
  40. // appear in the JS console.
  41. // To avoid confusion over this innocuous "error", we will install a
  42. // catch handler on the Promise. This does not prevent the app from
  43. // also catching failures and handling them. It only prevents the
  44. // console message.
  45. p.catch(function() {});
  46. }
  47. return p;
  48. };
  49. }
  50. };
  51. shaka.polyfill.register(shaka.polyfill.VideoPlayPromise.install);