Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | 2x 2x 2x 2x 2x 33x 33x 33x 33x 2x 1x 1x 33x 2x 1x 1x 33x 2x 1x 1x 33x 2x 33x 2x 33x 2x 33x 2x 1x 1x 33x 2x 1x 1x 33x 2x 1x 1x 24x 24x 24x 24x 24x 24x 1x 1x 14x 2x 2x 2x 1x 1x 13x 1x 1x 13x 1x 25x 1x 25x 25x 33x 17x 10x 8x 14x 8x 6x 4x 7x 33x 7x 7x 7x 1x 7x 33x 33x 2x 33x 1x 38x | /* eslint-disable @typescript-eslint/no-non-null-assertion */
import * as React from "react"
import { PlayerCore } from "../core"
import { removeUndefined } from "../utils"
import { Channel, PlaybackRate, PlayerOptions, Segment } from "../types"
import { MarkContext } from "./Mark"
interface PlayerProps {
channels?: Channel[]
children?: React.ReactNode
currentSegment?: string // if not specified then first in the array is initial
onReady?: (player: Player) => void
onError?: (error: Error) => void
options?: PlayerOptions
segments?: Segment[]
style?: React.CSSProperties
playbackRate?: PlaybackRate
recordingUrl: string
}
interface PlayerState {
isReady: boolean
}
export class Player extends React.PureComponent<PlayerProps, PlayerState> {
state = {
isReady: false,
}
container = React.createRef<HTMLDivElement>()
mediaElement: HTMLMediaElement | null = null
core?: PlayerCore
public on: PlayerCore["on"] = (event, fn) => {
if (!this.core) {
throw new Error("Cannot add listeners before Player is mounted")
}
return this.core.on(event, fn)
}
public once: PlayerCore["once"] = (event, fn) => {
if (!this.core) {
throw new Error("Cannot add listeners before Player is mounted")
}
return this.core.once(event, fn)
}
public off: PlayerCore["off"] = (event, fn) => {
if (!this.core) {
throw new Error("Cannot remove listeners before Player is mounted")
}
return this.core.off(event, fn)
}
public getCurrentTime = () => {
return this.core?.getCurrentTime()
}
public isPlaying = () => {
return this.core?.isPlaying() || false
}
public isReady = () => {
return this.core?.isReady() || false
}
public play = (start?: number, end?: number) => {
if (!this.core) {
throw new Error(
"Player.play() cannot be called before the component is mounted"
)
}
return this.core.play(start, end)
}
public pause = () => {
if (!this.core) {
throw new Error(
"Player.pause() cannot be called before the component is mounted"
)
}
return this.core.pause()
}
public seekToSecond = (seconds: number, offsetFromSegment = true) => {
if (!this.core) {
throw new Error(
"Player.seekToSecond() cannot be called before the component is mounted"
)
}
return this.core.seekToSecond(seconds, offsetFromSegment)
}
componentDidMount() {
this.core = new PlayerCore(
this.container.current!,
removeUndefined(this.props.options || {})
)
this.mediaElement = this.core.mediaElement
this.core.on("ready", this.onReady)
this.core.on("redraw", this.onRedraw)
this.core.on("error", this.onError)
this.load()
}
componentWillUnmount() {
this.core!.destroy()
this.core!.removeAllListeners()
}
componentDidUpdate(prevProps: PlayerProps) {
if (prevProps.recordingUrl !== this.props.recordingUrl) {
const url = new URL(prevProps.recordingUrl)
const prevUrl = new URL(this.props.recordingUrl)
if (url.origin + url.pathname === prevUrl.origin + prevUrl.pathname) {
// Same URL, only params have changed (secure url refresh)
this.core!.hotLoad(this.props.recordingUrl)
} else {
return this.load()
}
}
if (prevProps.currentSegment !== this.props.currentSegment) {
const [duration, offset] = this.getCurrentSegment()
this.core!.setCurrentSegment(duration, offset)
}
if (prevProps.playbackRate !== this.props.playbackRate) {
this.setPlaybackRate(this.props.playbackRate)
}
}
private load() {
if (this.state.isReady) {
this.setState({ isReady: false })
}
this.core!.load(this.props.recordingUrl, this.props.channels, { isAuthenticatedEndpoint: !!this.props.options?.xhr })
this.core!.setPlaybackRate(this.props.playbackRate)
}
private getCurrentSegment = (): [number, number] => {
if (this.props.segments) {
if (this.props.currentSegment) {
const segment = this.props.segments.find(
s => s.id === this.props.currentSegment
)
if (segment) {
return [segment.duration, segment.offset]
}
}
return [this.props.segments[0].duration, this.props.segments[0].offset]
}
return [0, 0]
}
private onReady = () => {
const [duration, offset] = this.getCurrentSegment()
this.core!.setCurrentSegment(duration, offset)
if (this.props.onReady) {
this.props.onReady(this)
}
this.setState({ isReady: true })
}
private onError = (e: Error) => {
if (this.props.onError) {
this.props.onError(e)
}
}
private onRedraw = () => {
this.forceUpdate()
}
private setPlaybackRate = (rate?: PlaybackRate) => {
return this.core!.setPlaybackRate(rate)
}
render() {
return (
<div
ref={this.container}
className="twilio-player-container"
style={{ ...this.props.style, position: "relative" }}
>
<div className="twilio-player-markings">
{this.state.isReady && this.container.current && this.core!.ws && (
<MarkContext.Provider
value={{
segmentOffset: this.getCurrentSegment()[1],
containerWidth: this.container.current.clientWidth,
pxPerSec:
this.container.current.clientWidth /
this.core!.ws.getDuration(),
regionsContainer: this.core!.regionsContainer,
}}
>
{this.props.children}
</MarkContext.Provider>
)}
</div>
</div>
)
}
}
|