-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathApp.solution.js
243 lines (221 loc) · 5.84 KB
/
App.solution.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
- Make the Play button work
- Make the Pause button work
- Disable the play button if it's playing
- Disable the pause button if it's not playing
- Make the PlayPause button work
- Make the JumpForward button work
- Make the JumpBack button work
- Make the progress bar work
- change the width of the inner element to the percentage of the played track
- add a click handler on the progress bar to jump to the clicked spot
Here is the audio API you'll need to use, `audio` is the <audio/> dom nod
instance, you can access it as `this.audio` in `AudioPlayer`
```js
// play/pause
audio.play()
audio.pause()
// change the current time
audio.currentTime = audio.currentTime + 10
audio.currentTime = audio.currentTime - 30
// know the duration
audio.duration
// values to calculate relative mouse click position
// on the progress bar
event.clientX // left position *from window* of mouse click
let rect = node.getBoundingClientRect()
rect.left // left position *of node from window*
rect.width // width of node
```
Other notes about the `<audio/>` tag:
- You can't know the duration until `onLoadedData`
- `onTimeUpdate` is fired when the currentTime changes
- `onEnded` is called when the track plays through to the end and is no
longer playing
Good luck!
*/
import React, { Component } from "react";
import podcast from "./lib/podcast.mp4";
import mario from "./lib/mariobros.mp3";
import FaPause from "react-icons/lib/fa/pause";
import FaPlay from "react-icons/lib/fa/play";
import FaRepeat from "react-icons/lib/fa/repeat";
import FaRotateLeft from "react-icons/lib/fa/rotate-left";
let AudioContext = React.createContext();
class AudioPlayer extends Component {
state = {
isPlaying: false,
duration: null,
currentTime: 0,
loaded: false,
play: () => {
this.audio.play();
this.setState({ isPlaying: true });
},
pause: () => {
this.audio.pause();
this.setState({ isPlaying: false });
},
setTime: time => {
this.audio.currentTime = time;
},
jump: by => {
this.audio.currentTime = this.audio.currentTime + by;
}
};
render() {
return (
<AudioContext.Provider value={this.state}>
<div className="audio-player">
<audio
src={this.props.source}
onTimeUpdate={() => {
this.setState({
currentTime: this.audio.currentTime,
duration: this.audio.duration
});
}}
onLoadedData={() => {
this.setState({
duration: this.audio.duration,
loaded: true
});
}}
onEnded={() => {
this.setState({
isPlaying: false
});
}}
ref={n => (this.audio = n)}
/>
{this.props.children}
</div>
</AudioContext.Provider>
);
}
}
class Play extends Component {
render() {
return (
<AudioContext.Consumer>
{context => (
<button
className="icon-button"
onClick={context.play}
disabled={context.isPlaying}
title="play"
>
<FaPlay />
</button>
)}
</AudioContext.Consumer>
);
}
}
class Pause extends Component {
render() {
return (
<AudioContext.Consumer>
{context => (
<button
className="icon-button"
onClick={context.pause}
disabled={!context.isPlaying}
title="pause"
>
<FaPause />
</button>
)}
</AudioContext.Consumer>
);
}
}
class PlayPause extends Component {
render() {
return (
<AudioContext.Consumer>
{context => (context.isPlaying ? <Pause /> : <Play />)}
</AudioContext.Consumer>
);
}
}
class JumpForward extends Component {
render() {
return (
<AudioContext.Consumer>
{context => (
<button
className="icon-button"
onClick={() => context.jump(10)}
disabled={null}
title="Forward 10 Seconds"
>
<FaRepeat />
</button>
)}
</AudioContext.Consumer>
);
}
}
class JumpBack extends Component {
render() {
return (
<AudioContext.Consumer>
{context => (
<button
className="icon-button"
onClick={() => context.jump(-10)}
disabled={null}
title="Back 10 Seconds"
>
<FaRotateLeft />
</button>
)}
</AudioContext.Consumer>
);
}
}
class Progress extends Component {
render() {
return (
<AudioContext.Consumer>
{context => {
let { loaded, duration, currentTime, setTime } = context;
return (
<div
className="progress"
ref={n => (this.node = n)}
onClick={event => {
let rect = this.node.getBoundingClientRect();
let clientLeft = event.clientX;
let relativeLeft = clientLeft - rect.left;
setTime(relativeLeft / rect.width * duration);
}}
>
<div
className="progress-bar"
style={{
width: loaded ? `${currentTime / duration * 100}%` : "0%"
}}
/>
</div>
);
}}
</AudioContext.Consumer>
);
}
}
let Exercise = () => (
<div className="exercise">
<AudioPlayer source={mario}>
<PlayPause /> <span className="player-text">Mario Bros. Remix</span>
<Progress />
</AudioPlayer>
<AudioPlayer source={podcast}>
<PlayPause /> <JumpBack /> <JumpForward />{" "}
<span className="player-text">Workshop.me Podcast Episode 02</span>
<Progress />
</AudioPlayer>
</div>
);
export default Exercise;