Source: lib/polyfill/video_play_promise.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.polyfill.VideoPlayPromise');
  7. goog.require('shaka.log');
  8. goog.require('shaka.polyfill');
  9. /**
  10. * @summary A polyfill to silence the play() Promise in HTML5 video.
  11. * @export
  12. */
  13. shaka.polyfill.VideoPlayPromise = class {
  14. /**
  15. * Install the polyfill if needed.
  16. * @export
  17. */
  18. static install() {
  19. shaka.log.debug('VideoPlayPromise.install');
  20. if (window.HTMLMediaElement) {
  21. // eslint-disable-next-line no-restricted-syntax
  22. const originalPlay = HTMLMediaElement.prototype.play;
  23. // eslint-disable-next-line no-restricted-syntax
  24. HTMLMediaElement.prototype.play = function() {
  25. const p = originalPlay.apply(this);
  26. if (p) {
  27. // This browser is returning a Promise from play().
  28. // If the play() call fails or is interrupted, the Promise will be
  29. // rejected. Some apps, however, don't listen to this Promise,
  30. // especially since it is not available cross-browser. If the Promise
  31. // is rejected without anyone listening for the failure, an error will
  32. // appear in the JS console.
  33. // To avoid confusion over this innocuous "error", we will install a
  34. // catch handler on the Promise. This does not prevent the app from
  35. // also catching failures and handling them. It only prevents the
  36. // console message.
  37. p.catch(() => {});
  38. }
  39. return p;
  40. };
  41. }
  42. }
  43. };
  44. shaka.polyfill.register(shaka.polyfill.VideoPlayPromise.install);