diff --git a/src/controller/eme-controller.ts b/src/controller/eme-controller.ts index 6e2bc09b4a3..2e8dd98382f 100644 --- a/src/controller/eme-controller.ts +++ b/src/controller/eme-controller.ts @@ -122,6 +122,9 @@ class EMEController implements ComponentAPI { private mediaKeysPromise: Promise | null = null; private _onMediaEncrypted = this.onMediaEncrypted.bind(this); + // try to call setMediaKeys again after media attached if needed + private _attemptSetMediaKeysAgain: boolean = false; + /** * @constructs * @param {Hls} hls Our Hls.js instance @@ -254,6 +257,7 @@ class EMEController implements ComponentAPI { logger.log(`Media-keys created for key-system "${keySystem}"`); this._onMediaKeysCreated(); + this._attemptSetMediaKeys(mediaKeys); return mediaKeys; }); @@ -358,9 +362,11 @@ class EMEController implements ComponentAPI { */ private _attemptSetMediaKeys(mediaKeys?: MediaKeys) { if (!this._media) { - throw new Error( + logger.warn( 'Attempted to set mediaKeys without first attaching a media element' ); + this._attemptSetMediaKeysAgain = true; + return; } if (!this._hasSetMediaKeys) { @@ -653,6 +659,11 @@ class EMEController implements ComponentAPI { // keep reference of media this._media = media; + if (this._attemptSetMediaKeysAgain) { + // mediaKeys not set, attempt again + this._attemptSetMediaKeys(); + } + media.addEventListener('encrypted', this._onMediaEncrypted); }