forked from jsierles/react-native-audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (122 loc) · 3.92 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
'use strict';
import React from "react";
import ReactNative, {
NativeModules,
NativeAppEventEmitter,
DeviceEventEmitter,
PermissionsAndroid,
Platform
} from "react-native";
var AudioRecorderManager = NativeModules.AudioRecorderManager;
var AudioRecorder = {
prepareRecordingAtPath: function(path, options) {
if (this.progressSubscription) this.progressSubscription.remove();
this.progressSubscription = NativeAppEventEmitter.addListener('recordingProgress',
(data) => {
if (this.onProgress) {
this.onProgress(data);
}
}
);
if (this.finishedSubscription) this.finishedSubscription.remove();
this.finishedSubscription = NativeAppEventEmitter.addListener('recordingFinished',
(data) => {
if (this.onFinished) {
this.onFinished(data);
}
}
);
var defaultOptions = {
SampleRate: 44100.0,
Channels: 2,
AudioQuality: 'High',
AudioEncoding: 'ima4',
OutputFormat: 'mpeg_4',
MeteringEnabled: false,
MeasurementMode: false,
AudioEncodingBitRate: 32000,
IncludeBase64: false,
AudioSource: 0
};
var recordingOptions = {...defaultOptions, ...options};
if (Platform.OS === 'ios') {
AudioRecorderManager.prepareRecordingAtPath(
path,
recordingOptions.SampleRate,
recordingOptions.Channels,
recordingOptions.AudioQuality,
recordingOptions.AudioEncoding,
recordingOptions.MeteringEnabled,
recordingOptions.MeasurementMode,
recordingOptions.IncludeBase64
);
} else {
return AudioRecorderManager.prepareRecordingAtPath(path, recordingOptions);
}
},
startRecording: function() {
return AudioRecorderManager.startRecording();
},
pauseRecording: function() {
return AudioRecorderManager.pauseRecording();
},
resumeRecording: function() {
return AudioRecorderManager.resumeRecording();
},
stopRecording: function() {
return AudioRecorderManager.stopRecording();
},
checkAuthorizationStatus: AudioRecorderManager.checkAuthorizationStatus,
requestAuthorization: () => {
if (Platform.OS === 'ios')
return AudioRecorderManager.requestAuthorization();
else
return new Promise((resolve, reject) => {
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO
).then(result => {
if (result == PermissionsAndroid.RESULTS.GRANTED || result == true)
resolve(true);
else
resolve(false)
})
});
},
removeListeners: function() {
if (this.progressSubscription) this.progressSubscription.remove();
if (this.finishedSubscription) this.finishedSubscription.remove();
},
};
let AudioUtils = {};
let AudioSource = {};
if (Platform.OS === 'ios') {
AudioUtils = {
MainBundlePath: AudioRecorderManager.MainBundlePath,
CachesDirectoryPath: AudioRecorderManager.NSCachesDirectoryPath,
DocumentDirectoryPath: AudioRecorderManager.NSDocumentDirectoryPath,
LibraryDirectoryPath: AudioRecorderManager.NSLibraryDirectoryPath,
};
} else if (Platform.OS === 'android') {
AudioUtils = {
MainBundlePath: AudioRecorderManager.MainBundlePath,
CachesDirectoryPath: AudioRecorderManager.CachesDirectoryPath,
DocumentDirectoryPath: AudioRecorderManager.DocumentDirectoryPath,
LibraryDirectoryPath: AudioRecorderManager.LibraryDirectoryPath,
PicturesDirectoryPath: AudioRecorderManager.PicturesDirectoryPath,
MusicDirectoryPath: AudioRecorderManager.MusicDirectoryPath,
DownloadsDirectoryPath: AudioRecorderManager.DownloadsDirectoryPath
};
AudioSource = {
DEFAULT: 0,
MIC: 1,
VOICE_UPLINK: 2,
VOICE_DOWNLINK: 3,
VOICE_CALL: 4,
CAMCORDER: 5,
VOICE_RECOGNITION: 6,
VOICE_COMMUNICATION: 7,
REMOTE_SUBMIX: 8, // added in API 19
UNPROCESSED: 9, // added in API 24
};
}
module.exports = {AudioRecorder, AudioUtils, AudioSource};