-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathViroMaterialVideo.tsx
157 lines (139 loc) · 4.56 KB
/
ViroMaterialVideo.tsx
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
/**
* Copyright (c) 2018-present, Viro Media, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
import * as React from "react";
import {
findNodeHandle,
NativeModules,
NativeSyntheticEvent,
Platform,
requireNativeComponent,
ViewProps,
} from "react-native";
import {
ViroErrorEvent,
ViroVideoBufferEndEvent,
ViroVideoBufferStartEvent,
ViroVideoUpdateTimeEvent,
} from "./Types/ViroEvents";
import { ViroNativeRef } from "./Types/ViroUtils";
type Props = ViewProps & {
material?: string;
paused?: boolean;
loop?: boolean;
muted?: boolean;
volume?: number;
/**
* Callback invoked when the underlying video component begins buffering. Called at
* least once at the beginning of playback/video creation.
*/
onBufferStart?: (
event: NativeSyntheticEvent<ViroVideoBufferStartEvent>
) => void;
/**
* Callback invoked when the underlying video component has finished buffering.
*/
onBufferEnd?: (event: NativeSyntheticEvent<ViroVideoBufferEndEvent>) => void;
/**
* Callback that is called when the video is finished playing. This
* function isn't called at the end of a video if looping is enabled.
*/
onFinish?: () => void;
/**
* Callback that is called when the current playback position has changed.
* This is called in the form:
* onUpdateTime(currentPlaybackTimeInSeconds, totalPlayBackDurationInSeconds);
*/
onUpdateTime?: (currentTime: number, totalTime: number) => void;
/**
* Callback triggered when the video fails to load. Invoked with
* {nativeEvent: {error}}
*/
onError?: (event: NativeSyntheticEvent<ViroErrorEvent>) => void;
};
export class ViroMaterialVideo extends React.Component<Props> {
_component: ViroNativeRef = null;
componentWillUnmount() {
// pause the current video texture on Android since java gc will release when it feels like it.
if (Platform.OS == "android") {
NativeModules.UIManager.dispatchViewManagerCommand(
findNodeHandle(this),
NativeModules.UIManager.VRTMaterialVideo.Commands.pause,
[0]
);
}
}
_onBufferStart = (event: NativeSyntheticEvent<ViroVideoBufferStartEvent>) => {
this.props.onBufferStart && this.props.onBufferStart(event);
};
_onBufferEnd = (event: NativeSyntheticEvent<ViroVideoBufferEndEvent>) => {
this.props.onBufferEnd && this.props.onBufferEnd(event);
};
_onFinish = () => {
this.props.onFinish && this.props.onFinish();
};
_onError = (event: NativeSyntheticEvent<ViroErrorEvent>) => {
this.props.onError && this.props.onError(event);
};
_onUpdateTime = (event: NativeSyntheticEvent<ViroVideoUpdateTimeEvent>) => {
this.props.onUpdateTime &&
this.props.onUpdateTime(
event.nativeEvent.currentTime,
event.nativeEvent.totalTime
);
};
setNativeProps = (nativeProps: Props) => {
this._component?.setNativeProps(nativeProps);
};
render() {
// Since materials and transformBehaviors can be either a string or an array, convert the string to a 1-element array.
//let materials = typeof this.props.materials === 'string' ? new Array(this.props.materials) : this.props.materials;
let nativeProps = Object.assign({} as any, this.props);
//nativeProps.materials = materials;
nativeProps.onBufferStartViro = this._onBufferStart;
nativeProps.onBufferEndViro = this._onBufferEnd;
nativeProps.onFinishViro = this._onFinish;
nativeProps.onErrorViro = this._onError;
nativeProps.onUpdateTimeViro = this._onUpdateTime;
nativeProps.ref = (component: ViroNativeRef) => {
this._component = component;
};
return <VRTMaterialVideo {...nativeProps} />;
}
seekToTime = (timeInSeconds: number) => {
switch (Platform.OS) {
case "ios":
NativeModules.VRTMaterialVideoManager.seekToTime(
findNodeHandle(this),
timeInSeconds
);
break;
case "android":
NativeModules.UIManager.dispatchViewManagerCommand(
findNodeHandle(this),
NativeModules.UIManager.VRTMaterialVideo.Commands.seekToTime,
[timeInSeconds]
);
break;
}
};
}
var VRTMaterialVideo = requireNativeComponent(
"VRTMaterialVideo",
// @ts-ignore
ViroMaterialVideo,
{
nativeOnly: {
onBufferStartViro: true,
onBufferEndViro: true,
onUpdateTimeViro: true,
onFinishViro: true,
onErrorViro: true,
},
}
);