-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathMediaStreamer.js
More file actions
111 lines (90 loc) · 2.28 KB
/
MediaStreamer.js
File metadata and controls
111 lines (90 loc) · 2.28 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
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
import React, {Component} from 'react'
import videojs from 'video.js'
export default class MediaStreamer extends Component {
componentDidMount() {
const MAX_LATENCY = 300;
const INITIAL_REWIND_AMOUNT = 90;
const RETRY_REWIND_AMOUNT = 30;
var options = {
hls: {
overrideNative: true
}
};
this.player = videojs(this.audioNode, {
flash: options,
html5: options,
sources: [{
src: this.props.src,
type: 'application/x-mpegurl'
}]
})
this.player.ready(() => {
this.props.onReady(this.controls)
this.player.play()
this.player.tech().on('retryplaylist', (e) => {
if (this.getLatency() < MAX_LATENCY) {
this.rewind(RETRY_REWIND_AMOUNT)
}
})
this.latencyUpdateInterval = setInterval(() => {
this.props.onLatencyUpdate(this.getLatency(), this.player.currentTime())
}, 1000);
})
this.player.on('playing', e => {
this.props.onPlaying()
})
this.player.on('pause', () => {
this.props.onPaused()
})
this.player.on('waiting', () => {
this.props.onLoading()
})
}
componentWillUnmount() {
clearInterval(this.latencyUpdateInterval);
if (this.player) {
this.player.dispose()
}
}
play = () => {
if (this.player) {
this.player.play()
}
}
pause = () => {
if (this.player) {
this.player.pause()
}
}
playPause = () => {
if (this.player) {
this.player.paused() ? this.play() : this.pause()
}
}
seekToLive = (secondsFromLive = 30) => {
if (this.player && (this.player.readyState() > 0)) {
this.player.currentTime(this.player.seekable().end(0) - secondsFromLive)
}
}
rewind = (seconds) => {
if (this.player) {
this.player.currentTime(this.player.currentTime() - seconds)
}
}
getLatency = () => {
if (this.player) {
if (this.player.seekable().length > 0)
{
return this.player.seekable().end(0) - this.player.currentTime()
}
else return 0
}
}
controls = {play: this.play, pause: this.pause, playPause: this.playPause}
render() {
const {src} = this.props
return (
<audio id='audio_element' ref={node => {this.audioNode = node}} className="video-js" />
)
}
}