|
| 1 | +import React, { PureComponent } from 'react'; |
| 2 | +import { |
| 3 | + StyleSheet, |
| 4 | + requireNativeComponent, |
| 5 | + NativeModules, |
| 6 | + View |
| 7 | +} from 'react-native'; |
| 8 | +import PropTypes from 'prop-types'; |
| 9 | +import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource'; |
| 10 | + |
| 11 | +export default class VLCPlayer extends PureComponent { |
| 12 | + constructor(props, context) { |
| 13 | + super(props, context); |
| 14 | + this.seek = this.seek.bind(this); |
| 15 | + this.resume = this.resume.bind(this); |
| 16 | + this.snapshot = this.snapshot.bind(this); |
| 17 | + this._assignRoot = this._assignRoot.bind(this); |
| 18 | + this._onError = this._onError.bind(this); |
| 19 | + this._onProgress = this._onProgress.bind(this); |
| 20 | + this._onEnded = this._onEnded.bind(this); |
| 21 | + this._onPlaying = this._onPlaying.bind(this); |
| 22 | + this._onStopped = this._onStopped.bind(this); |
| 23 | + this._onPaused = this._onPaused.bind(this); |
| 24 | + this._onBuffering = this._onBuffering.bind(this); |
| 25 | + this._onOpen = this._onOpen.bind(this); |
| 26 | + this._onLoadStart = this._onLoadStart.bind(this); |
| 27 | + } |
| 28 | + |
| 29 | + setNativeProps(nativeProps) { |
| 30 | + this._root.setNativeProps(nativeProps); |
| 31 | + } |
| 32 | + |
| 33 | + seek(pos) { |
| 34 | + this.setNativeProps({ seek: pos }); |
| 35 | + } |
| 36 | + |
| 37 | + resume(isResume) { |
| 38 | + this.setNativeProps({ resume: isResume }); |
| 39 | + } |
| 40 | + |
| 41 | + snapshot(path) { |
| 42 | + this.setNativeProps({ snapshotPath: path }); |
| 43 | + } |
| 44 | + |
| 45 | + _assignRoot(component) { |
| 46 | + this._root = component; |
| 47 | + } |
| 48 | + |
| 49 | + _onBuffering(event) { |
| 50 | + if (this.props.onBuffering) { |
| 51 | + this.props.onBuffering(event.nativeEvent); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + _onError(event) { |
| 56 | + if (this.props.onError) { |
| 57 | + this.props.onError(event.nativeEvent); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + _onOpen(event) { |
| 62 | + if (this.props.onOpen) { |
| 63 | + this.props.onOpen(event.nativeEvent); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + _onLoadStart(event) { |
| 68 | + if (this.props.onLoadStart) { |
| 69 | + this.props.onLoadStart(event.nativeEvent); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + _onProgress(event) { |
| 74 | + if (this.props.onProgress) { |
| 75 | + this.props.onProgress(event.nativeEvent); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + _onEnded(event) { |
| 80 | + if (this.props.onEnd) { |
| 81 | + this.props.onEnd(event.nativeEvent); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + _onStopped(event) { |
| 86 | + this.setNativeProps({ paused: true }); |
| 87 | + if (this.props.onStopped) { |
| 88 | + this.props.onStopped(event.nativeEvent); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + _onPaused(event) { |
| 93 | + if (this.props.onPaused) { |
| 94 | + this.props.onPaused(event.nativeEvent); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + _onPlaying(event) { |
| 99 | + if (this.props.onPlaying) { |
| 100 | + this.props.onPlaying(event.nativeEvent); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + render() { |
| 105 | + /* const { |
| 106 | + source |
| 107 | + } = this.props;*/ |
| 108 | + const source = resolveAssetSource(this.props.source) || {}; |
| 109 | + |
| 110 | + let uri = source.uri || ''; |
| 111 | + if (uri && uri.match(/^\//)) { |
| 112 | + uri = `file://${uri}`; |
| 113 | + } |
| 114 | + |
| 115 | + let isNetwork = !!(uri && uri.match(/^https?:/)); |
| 116 | + const isAsset = !!(uri && uri.match(/^(assets-library|file|content|ms-appx|ms-appdata):/)); |
| 117 | + if (!isAsset) { |
| 118 | + isNetwork = true; |
| 119 | + } |
| 120 | + source.initOptions = source.initOptions || []; |
| 121 | + //repeat the input media |
| 122 | + source.initOptions.push('--input-repeat=1000'); |
| 123 | + const nativeProps = Object.assign({}, this.props); |
| 124 | + Object.assign(nativeProps, { |
| 125 | + style: [styles.base, nativeProps.style], |
| 126 | + source: source, |
| 127 | + src: { |
| 128 | + uri, |
| 129 | + isNetwork, |
| 130 | + isAsset, |
| 131 | + type: source.type || '', |
| 132 | + mainVer: source.mainVer || 0, |
| 133 | + patchVer: source.patchVer || 0, |
| 134 | + }, |
| 135 | + onVideoLoadStart: this._onLoadStart, |
| 136 | + onVideoOpen: this._onOpen, |
| 137 | + onVideoError: this._onError, |
| 138 | + onVideoProgress: this._onProgress, |
| 139 | + onVideoEnded: this._onEnded, |
| 140 | + onVideoEnd: this._onEnded, |
| 141 | + onVideoPlaying: this._onPlaying, |
| 142 | + onVideoPaused: this._onPaused, |
| 143 | + onVideoStopped: this._onStopped, |
| 144 | + onVideoBuffering: this._onBuffering, |
| 145 | + progressUpdateInterval: 250, |
| 146 | + }); |
| 147 | + |
| 148 | + return <RCTVLCPlayer ref={this._assignRoot} {...nativeProps} />; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +VLCPlayer.propTypes = { |
| 153 | + /* Native only */ |
| 154 | + rate: PropTypes.number, |
| 155 | + seek: PropTypes.number, |
| 156 | + resume: PropTypes.bool, |
| 157 | + snapshotPath: PropTypes.string, |
| 158 | + paused: PropTypes.bool, |
| 159 | + |
| 160 | + videoAspectRatio: PropTypes.string, |
| 161 | + volume: PropTypes.number, |
| 162 | + disableFocus: PropTypes.bool, |
| 163 | + src: PropTypes.string, |
| 164 | + playInBackground: PropTypes.bool, |
| 165 | + playWhenInactive: PropTypes.bool, |
| 166 | + resizeMode: PropTypes.string, |
| 167 | + poster: PropTypes.string, |
| 168 | + repeat: PropTypes.bool, |
| 169 | + muted: PropTypes.bool, |
| 170 | + |
| 171 | + |
| 172 | + onVideoLoadStart: PropTypes.func, |
| 173 | + onVideoError: PropTypes.func, |
| 174 | + onVideoProgress: PropTypes.func, |
| 175 | + onVideoEnded: PropTypes.func, |
| 176 | + onVideoPlaying: PropTypes.func, |
| 177 | + onVideoPaused: PropTypes.func, |
| 178 | + onVideoStopped: PropTypes.func, |
| 179 | + onVideoBuffering: PropTypes.func, |
| 180 | + onVideoOpen: PropTypes.func, |
| 181 | + |
| 182 | + /* Wrapper component */ |
| 183 | + source: PropTypes.object, |
| 184 | + |
| 185 | + onError: PropTypes.func, |
| 186 | + onProgress: PropTypes.func, |
| 187 | + onEnded: PropTypes.func, |
| 188 | + onStopped: PropTypes.func, |
| 189 | + onPlaying: PropTypes.func, |
| 190 | + onPaused: PropTypes.func, |
| 191 | + |
| 192 | + /* Required by react-native */ |
| 193 | + scaleX: PropTypes.number, |
| 194 | + scaleY: PropTypes.number, |
| 195 | + translateX: PropTypes.number, |
| 196 | + translateY: PropTypes.number, |
| 197 | + rotation: PropTypes.number, |
| 198 | + ...View.propTypes, |
| 199 | +}; |
| 200 | + |
| 201 | +const styles = StyleSheet.create({ |
| 202 | + base: { |
| 203 | + overflow: 'hidden', |
| 204 | + }, |
| 205 | +}); |
| 206 | +const RCTVLCPlayer = requireNativeComponent('RCTVLCPlayer', VLCPlayer); |
0 commit comments