diff --git a/README.md b/README.md index b0f60d7..1a7646e 100644 --- a/README.md +++ b/README.md @@ -330,6 +330,16 @@ type Theme = { onJSLoadError No + + onInit + + playerId: string; + + + + onInitError + Error + onReady diff --git a/dist/player.d.ts b/dist/player.d.ts index c28f4d8..680c016 100644 --- a/dist/player.d.ts +++ b/dist/player.d.ts @@ -15,6 +15,9 @@ export type BookmarkTypes = { time: number; title?: string; }; +export type EventInitTypes = { + playerId: string; +}; export type EventReadyTypes = { currentTime: number; duration: number; @@ -106,6 +109,8 @@ export type PlayerPropsTypes = { watermark?: WatermarkTypes; localStorage?: boolean; theme?: Theme; + onInit?: (data: EventInitTypes) => void; + onInitError?: (error: Error) => void; onReady?: (data: EventReadyTypes) => void; onQualityChanged?: (data: EventQualityChangedTypes) => void; onCurrentTrackChanged?: (data: EventCurrentTrackChangedTypes) => void; diff --git a/dist/player.js b/dist/player.js index d4954a1..2e0e05d 100644 --- a/dist/player.js +++ b/dist/player.js @@ -563,6 +563,9 @@ var Player = /*#__PURE__*/function (_Component) { _this.create = function () { try { + var _this$props4 = _this.props, + onInit = _this$props4.onInit, + onInitError = _this$props4.onInitError; return Promise.resolve(_this.destroy()).then(function () { var parentsRef = _this.parentsRef.current; @@ -577,7 +580,7 @@ var Player = /*#__PURE__*/function (_Component) { var playerDiv = document.createElement('div'); playerDiv.setAttribute('id', playerId); parentsRef.appendChild(playerDiv); - /* fast re create player fix */ + /* fast re-create player fix */ return Promise.resolve(new Promise(function (resolve) { setTimeout(resolve, 0); @@ -586,8 +589,18 @@ var Player = /*#__PURE__*/function (_Component) { return; } - return Promise.resolve(_this.createPlayer(playerId)).then(function (_this$createPlayer) { - _this.player = _this$createPlayer; + return Promise.resolve(new Promise(function (resolve, reject) { + _this.createPlayer(playerId).then(function (player) { + resolve(player); + onInit && onInit({ + playerId: playerId + }); + })["catch"](function (e) { + reject(e); + onInitError && onInitError(e); + }); + })).then(function (_Promise) { + _this.player = _Promise; _this.getEventList().forEach(function (event) { var _this$player; @@ -657,31 +670,31 @@ var Player = /*#__PURE__*/function (_Component) { }; _this.createPlayer = function (playerId) { - var _this$props4 = _this.props, - title = _this$props4.title, - subtitle = _this$props4.subtitle, - poster = _this$props4.poster, - chapters = _this$props4.chapters, - vtt = _this$props4.vtt, - externalId = _this$props4.externalId, - drmAuthToken = _this$props4.drmAuthToken, - width = _this$props4.width, - height = _this$props4.height, - autoPause = _this$props4.autoPause, - autoPlay = _this$props4.autoPlay, - loop = _this$props4.loop, - muted = _this$props4.muted, - playsInline = _this$props4.playsInline, - preload = _this$props4.preload, - language = _this$props4.language, - controls = _this$props4.controls, - mainPlayButton = _this$props4.mainPlayButton, - playbackRateButton = _this$props4.playbackRateButton, - bookmarks = _this$props4.bookmarks, - actions = _this$props4.actions, - watermark = _this$props4.watermark, - localStorage = _this$props4.localStorage, - theme = _this$props4.theme; + var _this$props5 = _this.props, + title = _this$props5.title, + subtitle = _this$props5.subtitle, + poster = _this$props5.poster, + chapters = _this$props5.chapters, + vtt = _this$props5.vtt, + externalId = _this$props5.externalId, + drmAuthToken = _this$props5.drmAuthToken, + width = _this$props5.width, + height = _this$props5.height, + autoPause = _this$props5.autoPause, + autoPlay = _this$props5.autoPlay, + loop = _this$props5.loop, + muted = _this$props5.muted, + playsInline = _this$props5.playsInline, + preload = _this$props5.preload, + language = _this$props5.language, + controls = _this$props5.controls, + mainPlayButton = _this$props5.mainPlayButton, + playbackRateButton = _this$props5.playbackRateButton, + bookmarks = _this$props5.bookmarks, + actions = _this$props5.actions, + watermark = _this$props5.watermark, + localStorage = _this$props5.localStorage, + theme = _this$props5.theme; var options = { url: _this.getIFrameUrl(), size: { @@ -1123,10 +1136,10 @@ var Player = /*#__PURE__*/function (_Component) { }; _proto.render = function render() { - var _this$props5 = this.props, - className = _this$props5.className, - style = _this$props5.style, - onJSLoadError = _this$props5.onJSLoadError; + var _this$props6 = this.props, + className = _this$props6.className, + style = _this$props6.style, + onJSLoadError = _this$props6.onJSLoadError; return React__default['default'].createElement(Loader, { onJSLoad: this.handleJSLoad, onJSLoadError: onJSLoadError diff --git a/dist/player.module.js b/dist/player.module.js index b4fdf0e..ff376e1 100644 --- a/dist/player.module.js +++ b/dist/player.module.js @@ -559,6 +559,9 @@ var Player = /*#__PURE__*/function (_Component) { _this.create = function () { try { + var _this$props4 = _this.props, + onInit = _this$props4.onInit, + onInitError = _this$props4.onInitError; return Promise.resolve(_this.destroy()).then(function () { var parentsRef = _this.parentsRef.current; @@ -573,7 +576,7 @@ var Player = /*#__PURE__*/function (_Component) { var playerDiv = document.createElement('div'); playerDiv.setAttribute('id', playerId); parentsRef.appendChild(playerDiv); - /* fast re create player fix */ + /* fast re-create player fix */ return Promise.resolve(new Promise(function (resolve) { setTimeout(resolve, 0); @@ -582,8 +585,18 @@ var Player = /*#__PURE__*/function (_Component) { return; } - return Promise.resolve(_this.createPlayer(playerId)).then(function (_this$createPlayer) { - _this.player = _this$createPlayer; + return Promise.resolve(new Promise(function (resolve, reject) { + _this.createPlayer(playerId).then(function (player) { + resolve(player); + onInit && onInit({ + playerId: playerId + }); + })["catch"](function (e) { + reject(e); + onInitError && onInitError(e); + }); + })).then(function (_Promise) { + _this.player = _Promise; _this.getEventList().forEach(function (event) { var _this$player; @@ -653,31 +666,31 @@ var Player = /*#__PURE__*/function (_Component) { }; _this.createPlayer = function (playerId) { - var _this$props4 = _this.props, - title = _this$props4.title, - subtitle = _this$props4.subtitle, - poster = _this$props4.poster, - chapters = _this$props4.chapters, - vtt = _this$props4.vtt, - externalId = _this$props4.externalId, - drmAuthToken = _this$props4.drmAuthToken, - width = _this$props4.width, - height = _this$props4.height, - autoPause = _this$props4.autoPause, - autoPlay = _this$props4.autoPlay, - loop = _this$props4.loop, - muted = _this$props4.muted, - playsInline = _this$props4.playsInline, - preload = _this$props4.preload, - language = _this$props4.language, - controls = _this$props4.controls, - mainPlayButton = _this$props4.mainPlayButton, - playbackRateButton = _this$props4.playbackRateButton, - bookmarks = _this$props4.bookmarks, - actions = _this$props4.actions, - watermark = _this$props4.watermark, - localStorage = _this$props4.localStorage, - theme = _this$props4.theme; + var _this$props5 = _this.props, + title = _this$props5.title, + subtitle = _this$props5.subtitle, + poster = _this$props5.poster, + chapters = _this$props5.chapters, + vtt = _this$props5.vtt, + externalId = _this$props5.externalId, + drmAuthToken = _this$props5.drmAuthToken, + width = _this$props5.width, + height = _this$props5.height, + autoPause = _this$props5.autoPause, + autoPlay = _this$props5.autoPlay, + loop = _this$props5.loop, + muted = _this$props5.muted, + playsInline = _this$props5.playsInline, + preload = _this$props5.preload, + language = _this$props5.language, + controls = _this$props5.controls, + mainPlayButton = _this$props5.mainPlayButton, + playbackRateButton = _this$props5.playbackRateButton, + bookmarks = _this$props5.bookmarks, + actions = _this$props5.actions, + watermark = _this$props5.watermark, + localStorage = _this$props5.localStorage, + theme = _this$props5.theme; var options = { url: _this.getIFrameUrl(), size: { @@ -1119,10 +1132,10 @@ var Player = /*#__PURE__*/function (_Component) { }; _proto.render = function render() { - var _this$props5 = this.props, - className = _this$props5.className, - style = _this$props5.style, - onJSLoadError = _this$props5.onJSLoadError; + var _this$props6 = this.props, + className = _this$props6.className, + style = _this$props6.style, + onJSLoadError = _this$props6.onJSLoadError; return React.createElement(Loader, { onJSLoad: this.handleJSLoad, onJSLoadError: onJSLoadError diff --git a/dist/player.umd.js b/dist/player.umd.js index c707166..43e8c49 100644 --- a/dist/player.umd.js +++ b/dist/player.umd.js @@ -566,6 +566,9 @@ _this.create = function () { try { + var _this$props4 = _this.props, + onInit = _this$props4.onInit, + onInitError = _this$props4.onInitError; return Promise.resolve(_this.destroy()).then(function () { var parentsRef = _this.parentsRef.current; @@ -580,7 +583,7 @@ var playerDiv = document.createElement('div'); playerDiv.setAttribute('id', playerId); parentsRef.appendChild(playerDiv); - /* fast re create player fix */ + /* fast re-create player fix */ return Promise.resolve(new Promise(function (resolve) { setTimeout(resolve, 0); @@ -589,8 +592,18 @@ return; } - return Promise.resolve(_this.createPlayer(playerId)).then(function (_this$createPlayer) { - _this.player = _this$createPlayer; + return Promise.resolve(new Promise(function (resolve, reject) { + _this.createPlayer(playerId).then(function (player) { + resolve(player); + onInit && onInit({ + playerId: playerId + }); + })["catch"](function (e) { + reject(e); + onInitError && onInitError(e); + }); + })).then(function (_Promise) { + _this.player = _Promise; _this.getEventList().forEach(function (event) { var _this$player; @@ -660,31 +673,31 @@ }; _this.createPlayer = function (playerId) { - var _this$props4 = _this.props, - title = _this$props4.title, - subtitle = _this$props4.subtitle, - poster = _this$props4.poster, - chapters = _this$props4.chapters, - vtt = _this$props4.vtt, - externalId = _this$props4.externalId, - drmAuthToken = _this$props4.drmAuthToken, - width = _this$props4.width, - height = _this$props4.height, - autoPause = _this$props4.autoPause, - autoPlay = _this$props4.autoPlay, - loop = _this$props4.loop, - muted = _this$props4.muted, - playsInline = _this$props4.playsInline, - preload = _this$props4.preload, - language = _this$props4.language, - controls = _this$props4.controls, - mainPlayButton = _this$props4.mainPlayButton, - playbackRateButton = _this$props4.playbackRateButton, - bookmarks = _this$props4.bookmarks, - actions = _this$props4.actions, - watermark = _this$props4.watermark, - localStorage = _this$props4.localStorage, - theme = _this$props4.theme; + var _this$props5 = _this.props, + title = _this$props5.title, + subtitle = _this$props5.subtitle, + poster = _this$props5.poster, + chapters = _this$props5.chapters, + vtt = _this$props5.vtt, + externalId = _this$props5.externalId, + drmAuthToken = _this$props5.drmAuthToken, + width = _this$props5.width, + height = _this$props5.height, + autoPause = _this$props5.autoPause, + autoPlay = _this$props5.autoPlay, + loop = _this$props5.loop, + muted = _this$props5.muted, + playsInline = _this$props5.playsInline, + preload = _this$props5.preload, + language = _this$props5.language, + controls = _this$props5.controls, + mainPlayButton = _this$props5.mainPlayButton, + playbackRateButton = _this$props5.playbackRateButton, + bookmarks = _this$props5.bookmarks, + actions = _this$props5.actions, + watermark = _this$props5.watermark, + localStorage = _this$props5.localStorage, + theme = _this$props5.theme; var options = { url: _this.getIFrameUrl(), size: { @@ -1126,10 +1139,10 @@ }; _proto.render = function render() { - var _this$props5 = this.props, - className = _this$props5.className, - style = _this$props5.style, - onJSLoadError = _this$props5.onJSLoadError; + var _this$props6 = this.props, + className = _this$props6.className, + style = _this$props6.style, + onJSLoadError = _this$props6.onJSLoadError; return React__default['default'].createElement(Loader, { onJSLoad: this.handleJSLoad, onJSLoadError: onJSLoadError diff --git a/src/player.tsx b/src/player.tsx index 99f45b7..9d8d3c3 100644 --- a/src/player.tsx +++ b/src/player.tsx @@ -38,6 +38,10 @@ export type BookmarkTypes = { title?: string; }; +export type EventInitTypes = { + playerId: string; +}; + export type EventReadyTypes = { currentTime: number; duration: number; @@ -145,6 +149,8 @@ export type PlayerPropsTypes = { localStorage?: boolean; theme?: Theme; + onInit?: (data: EventInitTypes) => void; + onInitError?: (error: Error) => void; onReady?: (data: EventReadyTypes) => void; onQualityChanged?: (data: EventQualityChangedTypes) => void; onCurrentTrackChanged?: (data: EventCurrentTrackChangedTypes) => void; @@ -398,6 +404,7 @@ class Player extends Component { }; private create = async () => { + const {onInit, onInitError} = this.props; await this.destroy(); const parentsRef = this.parentsRef.current; @@ -412,15 +419,24 @@ class Player extends Component { playerDiv.setAttribute('id', playerId); parentsRef.appendChild(playerDiv); - /* fast re create player fix */ + /* fast re-create player fix */ await new Promise(resolve => { setTimeout(resolve, 0); }); if (!document.getElementById(playerId)) { return; } - - this.player = await this.createPlayer(playerId); + this.player = await new Promise((resolve, reject) => { + this.createPlayer(playerId) + .then(player => { + resolve(player); + onInit && onInit({playerId: playerId}); + }) + .catch((e: Error) => { + reject(e); + onInitError && onInitError(e); + }); + }); this.getEventList().forEach(event => { this.player?.on(event[0], event[1]); });