forked from tsjing/react-native-audio-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recorder.js
149 lines (120 loc) · 3.36 KB
/
Recorder.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
'use strict';
import {
NativeModules,
DeviceEventEmitter,
NativeAppEventEmitter,
Platform
} from 'react-native';
import _ from 'lodash';
import async from 'async';
import EventEmitter from 'eventemitter3';
import MediaStates from './MediaStates';
var RCTAudioRecorder = NativeModules.AudioRecorder;
var recorderId = 0;
var defaultRecorderOptions = {
autoDestroy: true
};
/**
* Represents a media recorder
* @constructor
*/
class Recorder extends EventEmitter {
constructor(path, options = defaultRecorderOptions) {
super();
this._path = path;
this._options = options;
this._recorderId = recorderId++;
this._reset();
let appEventEmitter = Platform.OS === 'ios' ? NativeAppEventEmitter : DeviceEventEmitter;
appEventEmitter.addListener('RCTAudioRecorderEvent:' + this._recorderId, (payload: Event) => {
this._handleEvent(payload.event, payload.data);
});
}
_reset() {
this._state = MediaStates.IDLE;
this._duration = -1;
this._position = -1;
this._lastSync = -1;
}
_updateState(err, state) {
this._state = err ? MediaStates.ERROR : state;
}
_handleEvent(event, data) {
//console.log('event: ' + event + ', data: ' + JSON.stringify(data));
switch (event) {
case 'ended':
this._state = Math.min(this._state, MediaStates.PREPARED);
break;
case 'info':
// TODO
break;
case 'error':
this._reset();
//this.emit('error', data);
break;
}
this.emit(event, data);
}
prepare(callback = _.noop) {
this._updateState(null, MediaStates.PREPARING);
// Prepare recorder
RCTAudioRecorder.prepare(this._recorderId, this._path, this._options, (err, fsPath) => {
this._fsPath = fsPath;
this._updateState(err, MediaStates.PREPARED);
callback(err, fsPath);
});
return this;
}
record(callback = _.noop) {
let tasks = [];
// Make sure recorder is prepared
if (this._state === MediaStates.IDLE) {
tasks.push((next) => {
this.prepare(next);
});
}
// Start recording
tasks.push((next) => {
RCTAudioRecorder.record(this._recorderId, next);
});
async.series(tasks, (err) => {
this._updateState(err, MediaStates.RECORDING);
callback(err);
});
return this;
}
stop(callback = _.noop) {
if (this._state >= MediaStates.RECORDING) {
RCTAudioRecorder.stop(this._recorderId, (err) => {
this._updateState(err, MediaStates.DESTROYED);
callback(err);
});
} else {
setTimeout(callback, 0);
}
return this;
}
toggleRecord(callback = _.noop) {
if (this._state === MediaStates.RECORDING) {
this.stop((err) => {
callback(err, true);
});
} else {
this.record((err) => {
callback(err, false);
});
}
return this;
}
destroy(callback = _.noop) {
this._reset();
RCTAudioRecorder.destroy(this._recorderId, callback);
}
get state() { return this._state; }
get canRecord() { return this._state >= MediaStates.PREPARED; }
get canPrepare() { return this._state == MediaStates.IDLE; }
get isRecording() { return this._state == MediaStates.RECORDING; }
get isPrepared() { return this._state == MediaStates.PREPARED; }
get fsPath() { return this._fsPath; }
}
export default Recorder;