-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoPlayerView.js
More file actions
81 lines (69 loc) · 2.01 KB
/
Copy pathVideoPlayerView.js
File metadata and controls
81 lines (69 loc) · 2.01 KB
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
import React, { useEffect, useRef, useState } from 'react'
import {
DeviceEventEmitter,
NativeEventEmitter,
NativeModules,
Platform,
requireNativeComponent
} from 'react-native'
const { IAYVideoPlayerModule: VideoPlayerModule } = NativeModules
const NativeVideoPlayerView = requireNativeComponent('IAYVideoPlayerView')
let lastListenerId = 0
export const VideoPlayerView = props => {
const [myListenerId] = useState(lastListenerId)
const propsRef = useRef(props)
const mountedRef = useRef(false)
useEffect(() => {
propsRef.current = props
}, [props])
useEffect(() => {
mountedRef.current = true
lastListenerId += 1
const eventEmitter = Platform.select({
ios: new NativeEventEmitter(VideoPlayerModule),
android: DeviceEventEmitter
})
const subscription = eventEmitter.addListener(
VideoPlayerModule.EVENT_NAME,
event => {
if (!mountedRef.current) {
return
}
const { id, kind } = event
if (id !== myListenerId) {
return
}
if (kind === 'playing') {
if (propsRef.current.onPlaying) {
propsRef.current.onPlaying()
}
} else if (kind === 'viewing') {
if (propsRef.current.onViewing) {
propsRef.current.onViewing()
}
} else if (kind === 'paused') {
if (propsRef.current.onPaused) {
propsRef.current.onPaused()
}
} else if (kind === 'ended') {
if (propsRef.current.onEnded) {
propsRef.current.onEnded()
}
} else if (kind === 'stopped') {
if (propsRef.current.onStopped) {
propsRef.current.onStopped()
}
} else if (kind === 'error') {
if (propsRef.current.onError) {
propsRef.current.onError()
}
}
}
)
return () => {
mountedRef.current = false
subscription.remove()
}
}, [])
return <NativeVideoPlayerView listenerId={myListenerId} {...props} />
}