-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
136 lines (117 loc) · 3.17 KB
/
index.js
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
import React, { useRef } from "react";
import { requireNativeComponent, View, Dimensions } from "react-native";
import { NativeModules, NativeEventEmitter } from "react-native";
const { width } = Dimensions.get("window");
const RNByronDLNA = NativeModules.RNByronDLNA || {};
export const startService = (name) => {
if (RNByronDLNA.startService) {
RNByronDLNA.startService(name);
}
};
export const closeService = () => {
if (RNByronDLNA.closeService) {
RNByronDLNA.closeService();
}
};
export const isInstalledApp = (name) => {
if (RNByronDLNA.isInstalledApp) {
return RNByronDLNA.isInstalledApp(name);
}
};
export const startApp = (name) => {
if (RNByronDLNA.startApp) {
RNByronDLNA.startApp(name);
}
};
export const dlnaEventName = "dlna-player";
const invertKeyValues = (obj) => {
return Object.keys(obj).reduce((acc, key) => {
acc[obj[key]] = key;
return acc;
}, {});
};
export const ByronEmitter = new NativeEventEmitter(RNByronDLNA);
const RNByronVlc = requireNativeComponent("RNByronVlc");
export const ScaleType = {
SURFACE_BEST_FIT: 0,
SURFACE_FIT_SCREEN: 1,
SURFACE_FILL: 2,
SURFACE_16_9: 3,
SURFACE_4_3: 4,
SURFACE_ORIGINAL: 5,
};
export const EventType = {
Buffering: 259,
EncounteredError: 266,
EndReached: 265,
Playing: 260,
Paused: 261,
PositionChanged: 268,
Stopped: 262,
};
const RNByronPlayer = React.forwardRef((props, ref) => {
const viewRef = useRef(null);
React.useImperativeHandle(ref, () => ({
setNativeProps: (nativeProps) => {
viewRef.current?.setNativeProps(nativeProps);
},
}));
const onEventVlc = (event) => {
const data = event.nativeEvent;
switch (data.type) {
case EventType.Buffering:
props.onBuffering && props.onBuffering();
break;
case EventType.EncounteredError:
props.onError && props.onError();
break;
case EventType.EndReached:
props.onEndReached && props.onEndReached();
break;
case EventType.Playing:
props.onPlaying && props.onPlaying(data);
break;
case EventType.Paused:
props.onPaused && props.onPaused();
break;
case EventType.PositionChanged:
props.onProgress && props.onProgress(data);
break;
case EventType.Stopped:
props.onStopped && props.onStopped();
break;
}
};
const onLayout = (e) => {
const layout = e.nativeEvent.layout;
viewRef.current?.setNativeProps({
style: {
width: layout.width,
height: layout.height,
},
});
};
const style = props.style || {};
const source = props.source || {};
const options = source.options ? source.options : ["--rtsp-tcp", "-vvv"];
const uri = source.uri || "";
if (!uri) return null;
const nativeProps = Object.assign({}, props, {
source: { uri, options },
style: {
width: style.width || width,
height: style.height || 240,
},
});
return (
<View style={style} onLayout={onLayout}>
<RNByronVlc
ref={viewRef}
source={{ uri, options }}
onEventVlc={onEventVlc}
{...nativeProps}
/>
</View>
);
});
export const ByronPlayer = React.memo(RNByronPlayer);