UNPKG

react-player

Version:

A React component for playing a variety of URLs, including file paths, YouTube, Facebook, Twitch, SoundCloud, Streamable, Vimeo, Wistia and DailyMotion

113 lines (112 loc) 3.08 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; import React, { Component } from "react"; import { callPlayer, getSDK } from "../utils.js"; import { canPlay } from "../patterns.js"; const SDK_URL = "https://cdn.embed.ly/player-0.1.0.min.js"; const SDK_GLOBAL = "playerjs"; class Kaltura extends Component { constructor() { super(...arguments); __publicField(this, "callPlayer", callPlayer); __publicField(this, "duration", null); __publicField(this, "currentTime", null); __publicField(this, "secondsLoaded", null); __publicField(this, "mute", () => { this.callPlayer("mute"); }); __publicField(this, "unmute", () => { this.callPlayer("unmute"); }); __publicField(this, "ref", (iframe) => { this.iframe = iframe; }); } componentDidMount() { this.props.onMount && this.props.onMount(this); } load(url) { getSDK(SDK_URL, SDK_GLOBAL).then((playerjs) => { if (!this.iframe) return; this.player = new playerjs.Player(this.iframe); this.player.on("ready", () => { setTimeout(() => { this.player.isReady = true; this.player.setLoop(this.props.loop); if (this.props.muted) { this.player.mute(); } this.addListeners(this.player, this.props); this.props.onReady(); }, 500); }); }, this.props.onError); } addListeners(player, props) { player.on("play", props.onPlay); player.on("pause", props.onPause); player.on("ended", props.onEnded); player.on("error", props.onError); player.on("timeupdate", ({ duration, seconds }) => { this.duration = duration; this.currentTime = seconds; }); } play() { this.callPlayer("play"); } pause() { this.callPlayer("pause"); } stop() { } seekTo(seconds, keepPlaying = true) { this.callPlayer("setCurrentTime", seconds); if (!keepPlaying) { this.pause(); } } setVolume(fraction) { this.callPlayer("setVolume", fraction); } setLoop(loop) { this.callPlayer("setLoop", loop); } getDuration() { return this.duration; } getCurrentTime() { return this.currentTime; } getSecondsLoaded() { return this.secondsLoaded; } render() { const style = { width: "100%", height: "100%" }; return /* @__PURE__ */ React.createElement( "iframe", { ref: this.ref, src: this.props.url, frameBorder: "0", scrolling: "no", style, allow: "encrypted-media; autoplay; fullscreen;", referrerPolicy: "no-referrer-when-downgrade" } ); } } __publicField(Kaltura, "displayName", "Kaltura"); __publicField(Kaltura, "canPlay", canPlay.kaltura); export { Kaltura as default };