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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x | import React, { Component } from 'react' import { propTypes, defaultProps } from './props' const SEEK_ON_PLAY_EXPIRY = 5000 export default class Player extends Component { static displayName = 'Player' static propTypes = propTypes static defaultProps = defaultProps mounted = false isReady = false isPlaying = false // Track playing state internally to prevent bugs isLoading = true // Use isLoading to prevent onPause when switching URL startOnPlay = true seekOnPlay = null onDurationCalled = false componentDidMount () { this.mounted = true Iif (this.player) { this.player.load(this.props.url) } this.progress() } componentWillUnmount () { clearTimeout(this.progressTimeout) if (this.isReady) { this.player.stop() } this.mounted = false } componentWillReceiveProps (nextProps) { // Invoke player methods based on incoming props const { url, playing, volume, muted, playbackRate } = this.props Iif (url !== nextProps.url) { this.isLoading = true this.startOnPlay = true this.onDurationCalled = false this.player.load(nextProps.url, this.isReady) } Eif (!playing && nextProps.playing && !this.isPlaying) { this.player.play() } Iif (playing && !nextProps.playing && this.isPlaying) { this.player.pause() } Iif (nextProps.volume !== null) { if (volume !== nextProps.volume && !nextProps.muted) { this.player.setVolume(nextProps.volume) } if (muted !== nextProps.muted) { this.player.setVolume(nextProps.muted ? 0 : nextProps.volume) } } Iif (playbackRate !== nextProps.playbackRate && this.player.setPlaybackRate) { this.player.setPlaybackRate(nextProps.playbackRate) } } getDuration () { if (!this.isReady) return null return this.player.getDuration() } getCurrentTime () { if (!this.isReady) return null return this.player.getCurrentTime() } getSecondsLoaded () { if (!this.isReady) return null return this.player.getSecondsLoaded() } getInternalPlayer = (key) => { if (!this.player) return null return this.player[key] } progress = () => { Iif (this.props.url && this.player && this.isReady) { const playedSeconds = this.getCurrentTime() || 0 const loadedSeconds = this.getSecondsLoaded() const duration = this.getDuration() if (duration) { const progress = { playedSeconds, played: playedSeconds / duration } if (loadedSeconds !== null) { progress.loadedSeconds = loadedSeconds progress.loaded = loadedSeconds / duration } // Only call onProgress if values have changed if (progress.played !== this.prevPlayed || progress.loaded !== this.prevLoaded) { this.props.onProgress(progress) } this.prevPlayed = progress.played this.prevLoaded = progress.loaded } } this.progressTimeout = setTimeout(this.progress, this.props.progressFrequency || this.props.progressInterval) } seekTo (amount) { // When seeking before player is ready, store value and seek later if (!this.isReady && amount !== 0) { this.seekOnPlay = amount setTimeout(() => { this.seekOnPlay = null }, SEEK_ON_PLAY_EXPIRY) return } if (amount > 0 && amount < 1) { // Convert fraction to seconds based on duration const duration = this.player.getDuration() if (!duration) { console.warn('ReactPlayer: could not seek using fraction – duration not yet available') return } this.player.seekTo(duration * amount) return } this.player.seekTo(amount) } onReady = () => { if (!this.mounted) return this.isReady = true this.isLoading = false const { onReady, playing, volume, muted } = this.props onReady() if (muted || volume !== null) { this.player.setVolume(muted ? 0 : volume) } if (playing) { this.player.play() } this.onDurationCheck() } onPlay = () => { this.isPlaying = true this.isLoading = false const { onStart, onPlay, playbackRate } = this.props if (this.startOnPlay) { if (this.player.setPlaybackRate) { this.player.setPlaybackRate(playbackRate) } onStart() this.startOnPlay = false } onPlay() if (this.seekOnPlay) { this.seekTo(this.seekOnPlay) this.seekOnPlay = null } this.onDurationCheck() } onPause = (e) => { this.isPlaying = false if (!this.isLoading) { this.props.onPause(e) } } onEnded = () => { const { activePlayer, loop, onEnded } = this.props if (activePlayer.loopOnEnded && loop) { this.seekTo(0) } if (!loop) { this.isPlaying = false } onEnded() } onDurationCheck = () => { clearTimeout(this.durationCheckTimeout) const duration = this.getDuration() if (duration) { if (!this.onDurationCalled) { this.props.onDuration(duration) this.onDurationCalled = true } } else { this.durationCheckTimeout = setTimeout(this.onDurationCheck, 100) } } ref = player => { Eif (player) { this.player = player } } render () { const Player = this.props.activePlayer return ( <Player {...this.props} ref={this.ref} onReady={this.onReady} onPlay={this.onPlay} onPause={this.onPause} onEnded={this.onEnded} /> ) } } |