forked from banshiAnton/react-native-audio-recorder-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
210 lines (189 loc) · 5.2 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
205
206
207
208
209
210
import {
NativeModules,
DeviceEventEmitter,
NativeEventEmitter,
Platform,
} from 'react-native';
const { RNAudioRecorderPlayer } = NativeModules;
const pad = (num) => {
return ('0' + num).slice(-2);
};
class AudioRecorderPlayer {
static _isRecording;
static _isPlaying;
static _recorderSubscription;
static _playerSubscription;
static _recordInterval;
requestRecordPermissionIOS = () => RNAudioRecorderPlayer.requestRecordPermission()
mmss = (secs) => {
let minutes = Math.floor(secs / 60);
secs = secs % 60;
minutes = minutes % 60;
// minutes = ('0' + minutes).slice(-2);
// secs = ('0' + secs).slice(-2);
return pad(minutes) + ':' + pad(secs);
}
mmssss = (milisecs) => {
const secs = Math.floor(milisecs / 1000);
const minutes = Math.floor(secs / 60);
const seconds = secs % 60;
const miliseconds = Math.floor((milisecs % 1000) / 10);
console.log('milisecs: ' + milisecs);
console.log('min: ' + minutes + ', secs: ' + seconds + ', ' + miliseconds);
return pad(minutes) + ':' + pad(seconds) + ':' + pad(miliseconds);
};
/**
* set listerner from native module for recorder.
* @returns {callBack(e: Event)}
*/
addRecordBackListener = (e: Event) => {
if (Platform.OS === 'android') {
this._recorderSubscription = DeviceEventEmitter.addListener('rn-recordback', e);
} else {
const myModuleEvt = new NativeEventEmitter(RNAudioRecorderPlayer);
this._recorderSubscription = myModuleEvt.addListener('rn-recordback', e);
}
}
/**
* remove listener for recorder.
* @returns {void}
*/
removeRecordBackListener = () => {
if (this._recorderSubscription) {
this._recorderSubscription.remove();
this._recorderSubscription = null;
}
}
/**
* set listener from native module for player.
* @returns {callBack(e: Event)}
*/
addPlayBackListener = (e: Event) => {
if (Platform.OS === 'android') {
this._playerSubscription = DeviceEventEmitter.addListener('rn-playback', e);
} else {
const myModuleEvt = new NativeEventEmitter(RNAudioRecorderPlayer);
this._playerSubscription = myModuleEvt.addListener('rn-playback', e);
}
}
/**
* remove listener for player.
* @returns {void}
*/
removePlayBackListener = () => {
if (this._playerSubscription) {
this._playerSubscription.remove();
this._playerSubscription = null;
}
}
/**
* start recording with param.
* @param {string} uri audio uri.
* @returns {Promise<string>}
*/
startRecorder = async(uri) => {
if (!uri) {
uri = 'DEFAULT';
}
if (!this._isRecording) {
this._isRecording = true;
return RNAudioRecorderPlayer.startRecorder(uri);
}
console.log('Already recording');
}
/**
* stop recording.
* @returns {Promise<string>}
*/
stopRecorder = async(cancel = false) => {
if (this._isRecording) {
this._isRecording = false;
return RNAudioRecorderPlayer.stopRecorder(cancel);
}
console.log('Already stopped recording');
}
/**
* resume playing.
* @returns {Promise<string>}
*/
resumePlayer = async() => {
if (!this._isPlaying && this._isPaused) {
this._isPlaying = true;
this._isPaused = false;
return RNAudioRecorderPlayer.resumePlayer();
}
console.log('Already playing');
}
/**
* start playing with param.
* @param {string} uri audio uri.
* @returns {Promise<string>}
*/
startPlayer = async(uri, seekTo = 0) => {
if (!uri) {
uri = 'DEFAULT';
}
if (!this._isPlaying) {
this._isPlaying = true;
this._isPaused = false;
console.log('Start play', seekTo, uri);
return RNAudioRecorderPlayer.startPlayer(uri, seekTo);
}
console.log('Already started playing');
}
/**
* stop playing.
* @returns {Promise<string>}
*/
stopPlayer = async() => {
if (this._isPlaying || this._isPaused) {
this._isPlaying = false;
this._isPaused = false;
return RNAudioRecorderPlayer.stopPlayer();
}
console.log('Already stopped playing');
}
/**
* pause playing.
* @returns {Promise<string>}
*/
pausePlayer = async() => {
if (this._isPlaying) {
this._isPlaying = false;
this._isPaused = true;
return RNAudioRecorderPlayer.pausePlayer();
}
console.log('Already paused or stopped');
}
/**
* seek to.
* @param {number} time position seek to in second.
* @returns {Promise<string>}
*/
seekToPlayer = async(time: number) => {
if (Platform.OS === 'ios') {
time = time / 1000;
}
return RNAudioRecorderPlayer.seekToPlayer(time);
}
/**
* set volume.
* @param {number} setVolume set volume.
* @returns {Promise<string>}
*/
setVolume = async(volume: number) => {
if (volume < 0 || volume > 1) {
return console.warn('Value of volume should be between 0.0 to 1.0');
}
return RNAudioRecorderPlayer.setVolume(volume);
}
/**
* set subscription duration.
* @param {number} sec subscription callback duration in seconds.
* @returns {Promise<string>}
*/
setSubscriptionDuration = async(sec) => {
return RNAudioRecorderPlayer.setSubscriptionDuration(sec);
}
}
export default AudioRecorderPlayer;