This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
App.js
95 lines (79 loc) · 2.09 KB
/
App.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
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import WaveStream from '../../../react-wave-stream/dist';
import Recorder from '../../../recorder/lib/Recorder';
class App extends Component {
constructor(...args) {
super(...args);
this.state = {
blob: null,
isRecording: false,
stream: null,
analyserData: {data: [], lineTo: 0},
};
this.start = this.start.bind(this);
this.stop = this.stop.bind(this);
this.download = this.download.bind(this);
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
this.recorder = new Recorder(this.audioContext, {
onAnalysed: data => this.setState({analyserData: data}),
});
navigator.mediaDevices.getUserMedia({audio: true})
.then((stream) => {
this.setState({stream});
this.recorder.init(stream);
})
.catch(this.dontGotStream);
}
start() {
this.recorder.start()
.then(() => this.setState({isRecording: true}));
}
stop() {
this.recorder.stop()
.then(({blob}) => this.setState({
isRecording: false,
blob,
}));
}
dontGotStream(error) {
console.log('Get stream failed', error);
}
download() {
Recorder.download(this.state.blob, 'react-audio');
this.setState({blob: null});
}
render() {
const {
isRecording,
blob,
stream,
} = this.state;
return (
<div className="App">
<div className="App-header">
<h2>Recording Studio</h2>
<div className="App-buttons">
{isRecording ? (
<button onClick={this.stop}>Stop</button>
) : (
<button onClick={this.start}>Start</button>
)}
{blob && (
<button
onClick={this.download}
>
Download
</button>
)}
</div>
</div>
<div className="App-studio">
<WaveStream {...this.state.analyserData} />
</div>
</div>
);
}
}
export default App;