react-native-nitro-audio-record 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -33,17 +33,20 @@ class HybridNitroAudioRecord : HybridNitroAudioRecordSpec() {
|
|
|
33
33
|
|
|
34
34
|
override fun setup(options: AudioRecordOptions) {
|
|
35
35
|
sampleRateInHz = options.sampleRate.toInt()
|
|
36
|
-
channelConfig = if (options.channels
|
|
37
|
-
audioFormat = if (options.bitsPerSample
|
|
36
|
+
channelConfig = if (options.channels >= 2.0) AudioFormat.CHANNEL_IN_STEREO else AudioFormat.CHANNEL_IN_MONO
|
|
37
|
+
audioFormat = if (options.bitsPerSample <= 8.0) AudioFormat.ENCODING_PCM_8BIT else AudioFormat.ENCODING_PCM_16BIT
|
|
38
38
|
audioSource = (options.audioSource ?: AudioSource.VOICE_RECOGNITION.toDouble()).toInt()
|
|
39
39
|
|
|
40
|
-
val context = NitroModules.applicationContext
|
|
40
|
+
val context = NitroModules.applicationContext ?: throw Exception("NitroModules.applicationContext is null!")
|
|
41
41
|
val documentDirectoryPath = context.filesDir.absolutePath
|
|
42
42
|
outFile = "$documentDirectoryPath/${options.wavFile ?: "audio.wav"}"
|
|
43
43
|
tmpFile = "$documentDirectoryPath/temp.pcm"
|
|
44
44
|
|
|
45
45
|
isRecording = false
|
|
46
46
|
bufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat)
|
|
47
|
+
if (bufferSize <= 0) {
|
|
48
|
+
bufferSize = 1024 // Fallback
|
|
49
|
+
}
|
|
47
50
|
val recordingBufferSize = bufferSize * 3
|
|
48
51
|
recorder = AudioRecord(audioSource, sampleRateInHz, channelConfig, audioFormat, recordingBufferSize)
|
|
49
52
|
}
|
|
@@ -63,7 +66,7 @@ class HybridNitroAudioRecord : HybridNitroAudioRecordSpec() {
|
|
|
63
66
|
val os = FileOutputStream(tmpFile)
|
|
64
67
|
|
|
65
68
|
while (isRecording) {
|
|
66
|
-
bytesRead = recorder?.read(buffer, 0, buffer.
|
|
69
|
+
bytesRead = recorder?.read(buffer, 0, buffer.size) ?: -1
|
|
67
70
|
|
|
68
71
|
// skip first 2 buffers to eliminate "click sound"
|
|
69
72
|
if (bytesRead > 0 && ++count > 2) {
|
|
@@ -89,15 +92,16 @@ class HybridNitroAudioRecord : HybridNitroAudioRecordSpec() {
|
|
|
89
92
|
|
|
90
93
|
override fun stop(): Promise<String> {
|
|
91
94
|
isRecording = false
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// For now, we'll wait a bit (simplistic).
|
|
96
|
-
Thread {
|
|
95
|
+
val promise = Promise<String>()
|
|
96
|
+
Thread {
|
|
97
|
+
try {
|
|
97
98
|
Thread.sleep(100)
|
|
98
|
-
resolve(outFile)
|
|
99
|
-
}
|
|
100
|
-
|
|
99
|
+
promise.resolve(outFile)
|
|
100
|
+
} catch (e: Exception) {
|
|
101
|
+
promise.reject(e)
|
|
102
|
+
}
|
|
103
|
+
}.start()
|
|
104
|
+
return promise
|
|
101
105
|
}
|
|
102
106
|
|
|
103
107
|
override fun onData(callback: (data: ArrayBuffer) -> Unit) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-audio-record",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "\u001b[33mreact-native-nitro-audio-record\u001b[39m is a react native package built with Nitro",
|
|
5
5
|
"main": "./lib/commonjs/index.js",
|
|
6
6
|
"module": "./lib/module/index.js",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"android/fix-prefab.gradle",
|
|
30
30
|
"android/gradle.properties",
|
|
31
31
|
"android/CMakeLists.txt",
|
|
32
|
-
"android/src",
|
|
32
|
+
"android/src/**/*",
|
|
33
33
|
"ios/**/*.h",
|
|
34
34
|
"ios/**/*.m",
|
|
35
35
|
"ios/**/*.mm",
|
|
@@ -120,4 +120,4 @@
|
|
|
120
120
|
]
|
|
121
121
|
},
|
|
122
122
|
"packageManager": "yarn@4.13.0"
|
|
123
|
-
}
|
|
123
|
+
}
|