-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
204 lines (192 loc) · 5.62 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const React = require('react');
const PropTypes = React.PropTypes;
const defaultPlay = <span>▶</span>;
const defaultPause = <span>❚❚</span>;
const playerContainerStyles = {
display: 'block',
width: '100%',
position: 'relative'
};
const playPauseButtonLayoutStyles = {
display: 'inline-block',
marginTop: '9px',
width: '14%',
verticalAlign: 'top',
cursor: 'pointer'
};
const playProgressLayoutWrapperStyles = {
display: 'inline-block',
width: '84%'
};
const audioProgressBarContainerStyles = () => ({
display: 'block',
width: '0%',
height: '21px',
marginTop: '8px',
background: 'transparent',
borderLeft: '1px solid #777',
borderRight: '1px solid #777'
});
const audioPlayerLineStyles = {
width: '100%',
height: '0px',
paddingTop: '10px',
display: 'block',
background: 'transparent',
borderBottom: '1px solid #777'
};
const AudioPlayer = React.createClass({
propTypes: {
source: PropTypes.string.isRequired,
sourceType: PropTypes.string.isRequired,
playElement: PropTypes.object,
pauseElement: PropTypes.object,
autoPlay: PropTypes.any,
onPlay: PropTypes.func
},
getInitialState() {
return {
fileFinishedDownloading: false,
mediaDownloadProgressPercent: 0,
mediaRequest: null,
mediaFile: null,
canPlay: false,
paused: true,
progressStyles: {}
};
},
componentDidMount() {
// The media is loaded as a blob to work around issues with Safari, for some reason,
// not being able to play a wav file when it is an audio source. This makes no sense.
// The other upside of having the file as a blob vs audio source is we can make
// clicking the audio progress bar do a seek. For some reason, that only worked on
// Firefox using an audio source.
const mediaRequest = new XMLHttpRequest();
this.setState({ mediaRequest });
mediaRequest.open('GET', this.props.source, true);
mediaRequest.responseType = 'blob';
mediaRequest.onload = () => {
const mediaFile = window.URL.createObjectURL(mediaRequest.response);
this.setState({
mediaFile,
mediaRequest: null,
fileFinishedDownloading: true,
mediaDownloadProgressPercent: 0
});
};
mediaRequest.onprogress = (e) => {
this.setState({
mediaDownloadProgressPercent: (e.loaded / e.total) * 100
});
};
mediaRequest.send();
},
componentWillUnmount() {
if (this.state.mediaRequest) {
this.state.mediaRequest.abort();
}
},
togglePlaying(e) {
e.stopPropagation();
e.preventDefault();
if (this.refs.audioElement.paused) {
this.refs.audioElement.play();
this.setState({
paused: false
});
if (this.props.onPlay) {
this.props.onPlay();
}
} else {
this.refs.audioElement.pause();
this.setState({
paused: true
});
}
},
setAudioProgress() {
const progressStyles = {
position: 'relative',
display: 'inline-block',
borderRight: '1px solid #777',
height: '21px',
padding: '0px',
top: '-11px',
left: '0'
};
const width = (
this.refs.audioElement.currentTime / this.refs.audioElement.duration
) * 100;
progressStyles.left = `${width}%`;
if (width > 98) {
this.setState({
paused: true
});
}
this.setState({ progressStyles });
},
replay(e) {
e.stopPropagation();
e.preventDefault();
const x = e.pageX - e.target.offsetLeft;
const howFarOverCursorX = Math.min((x / e.target.clientWidth), 1);
const newDuration = howFarOverCursorX * this.refs.audioElement.duration;
this.refs.audioElement.currentTime = newDuration;
this.setState({
paused: false
});
},
onCanPlay() {
this.setState({
canPlay: true
});
if (this.props.autoPlay) {
this.refs.audioElement.play();
this.setState({
paused: false
});
}
},
render() {
const fileFinishedDownloading = this.state.fileFinishedDownloading;
const mediaDownloadProgressPercent = this.state.mediaDownloadProgressPercent;
const canPlay = this.state.canPlay;
const paused = this.state.paused;
const progressStyles = this.state.progressStyles;
const mediaFile = this.state.mediaFile;
const sourceType = this.state.sourceType;
const playElement = this.props.playElement || defaultPlay;
const pauseElement = this.props.pauseElement || defaultPause;
const playPauseButtonInternals = paused ? playElement : pauseElement;
const playPauseButton = (<span style={playPauseButtonLayoutStyles}
disabled={canPlay}
onClick={this.togglePlaying}>
{playPauseButtonInternals}
</span>);
const progressContainerStyles = audioProgressBarContainerStyles();
const playProgress = (<div style={progressContainerStyles} onClick={this.replay}>
<span style={audioPlayerLineStyles} />
<span style={progressStyles} />
</div>);
// audio player line is not even added until you press play and it fully loads
let audioElement = null;
if (!fileFinishedDownloading) {
progressContainerStyles.width = `${mediaDownloadProgressPercent}%`;
} else {
progressContainerStyles.width = '100%';
audioElement = (<audio onCanPlay={this.onCanPlay}
onTimeUpdate={this.setAudioProgress}
ref="audioElement">
<source src={mediaFile} type={sourceType} />
</audio>);
}
return (
<div style={playerContainerStyles}>
{audioElement}
{playPauseButton}
<span style={playProgressLayoutWrapperStyles}>{playProgress}</span>
</div>
);
}
});
module.exports = AudioPlayer;