nosnia-audio-recorder 0.1.0
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.
- package/LICENSE +20 -0
- package/NosniaAudioRecorder.podspec +20 -0
- package/README.md +175 -0
- package/android/build.gradle +77 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/java/com/nosniaaudiorecorder/NosniaAudioRecorderModule.kt +232 -0
- package/android/src/main/java/com/nosniaaudiorecorder/NosniaAudioRecorderPackage.kt +33 -0
- package/ios/NosniaAudioRecorder.h +6 -0
- package/ios/NosniaAudioRecorder.mm +243 -0
- package/lib/module/NativeNosniaAudioRecorder.js +5 -0
- package/lib/module/NativeNosniaAudioRecorder.js.map +1 -0
- package/lib/module/index.js +133 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/NativeNosniaAudioRecorder.d.ts +25 -0
- package/lib/typescript/src/NativeNosniaAudioRecorder.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +50 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +170 -0
- package/src/NativeNosniaAudioRecorder.ts +27 -0
- package/src/index.tsx +140 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Parth Thakkar
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = "NosniaAudioRecorder"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = package["homepage"]
|
|
10
|
+
s.license = package["license"]
|
|
11
|
+
s.authors = package["author"]
|
|
12
|
+
|
|
13
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
14
|
+
s.source = { :git => "https://nosnia.ai.git", :tag => "#{s.version}" }
|
|
15
|
+
|
|
16
|
+
s.source_files = "ios/**/*.{h,m,mm,swift,cpp}"
|
|
17
|
+
s.private_header_files = "ios/**/*.h"
|
|
18
|
+
|
|
19
|
+
install_modules_dependencies(s)
|
|
20
|
+
end
|
package/README.md
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# nosnia-audio-recorder
|
|
2
|
+
|
|
3
|
+
A compact, high-performance audio recorder library for React Native that records directly to MP3 (M4A/AAC) format on both Android and iOS.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ποΈ **Direct MP3 Recording**: Records audio directly to M4A (AAC codec)
|
|
8
|
+
- π± **Cross-Platform**: Works seamlessly on Android and iOS
|
|
9
|
+
- ποΈ **Configurable**: Adjust bitrate, sample rate, and channels
|
|
10
|
+
- βΈοΈ **Pause/Resume**: Full control over recording with pause and resume
|
|
11
|
+
- π **Permission Handling**: Built-in permission checking and requesting
|
|
12
|
+
- πΎ **File Management**: Automatic file naming and directory management
|
|
13
|
+
- π¦ **Compact**: Minimal dependencies, optimized for bundle size
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install nosnia-audio-recorder
|
|
19
|
+
# or
|
|
20
|
+
yarn add nosnia-audio-recorder
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
### Request Permission
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import { NosniaAudioRecorder } from 'nosnia-audio-recorder';
|
|
29
|
+
|
|
30
|
+
// Request microphone permission
|
|
31
|
+
const hasPermission = await NosniaAudioRecorder.requestPermission();
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Record Audio
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
// Start recording
|
|
38
|
+
await NosniaAudioRecorder.startRecording({
|
|
39
|
+
bitrate: 128000,
|
|
40
|
+
channels: 1,
|
|
41
|
+
sampleRate: 44100,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// ... recording in progress ...
|
|
45
|
+
|
|
46
|
+
// Stop and save
|
|
47
|
+
const filePath = await NosniaAudioRecorder.stopRecording();
|
|
48
|
+
console.log('Recording saved to:', filePath);
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Full Example
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import { NosniaAudioRecorder } from 'nosnia-audio-recorder';
|
|
55
|
+
|
|
56
|
+
// Check and request permission
|
|
57
|
+
const hasPermission = await NosniaAudioRecorder.checkPermission();
|
|
58
|
+
if (!hasPermission) {
|
|
59
|
+
await NosniaAudioRecorder.requestPermission();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Start recording
|
|
63
|
+
await NosniaAudioRecorder.startRecording();
|
|
64
|
+
|
|
65
|
+
// Pause if needed
|
|
66
|
+
await NosniaAudioRecorder.pauseRecording();
|
|
67
|
+
await NosniaAudioRecorder.resumeRecording();
|
|
68
|
+
|
|
69
|
+
// Get status
|
|
70
|
+
const status = await NosniaAudioRecorder.getStatus();
|
|
71
|
+
console.log(`Recording: ${status.isRecording}, Duration: ${status.duration}ms`);
|
|
72
|
+
|
|
73
|
+
// Stop and save
|
|
74
|
+
const filePath = await NosniaAudioRecorder.stopRecording();
|
|
75
|
+
|
|
76
|
+
// Or cancel (discard)
|
|
77
|
+
await NosniaAudioRecorder.cancelRecording();
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## API Reference
|
|
81
|
+
|
|
82
|
+
### Methods
|
|
83
|
+
|
|
84
|
+
- `requestPermission(): Promise<boolean>` - Request microphone permission
|
|
85
|
+
- `checkPermission(): Promise<boolean>` - Check if permission is granted
|
|
86
|
+
- `startRecording(options?: RecorderOptions): Promise<void>` - Start recording
|
|
87
|
+
- `stopRecording(): Promise<string>` - Stop and save (returns file path)
|
|
88
|
+
- `pauseRecording(): Promise<void>` - Pause recording
|
|
89
|
+
- `resumeRecording(): Promise<void>` - Resume recording
|
|
90
|
+
- `cancelRecording(): Promise<void>` - Cancel and discard
|
|
91
|
+
- `getStatus(): Promise<RecorderStatus>` - Get recorder status
|
|
92
|
+
|
|
93
|
+
### Types
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
interface RecorderOptions {
|
|
97
|
+
filename?: string; // Auto-generated if not provided
|
|
98
|
+
bitrate?: number; // Default: 128000 (128 kbps)
|
|
99
|
+
channels?: number; // 1 = mono (default), 2 = stereo
|
|
100
|
+
sampleRate?: number; // Default: 44100 (44.1 kHz)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
interface RecorderStatus {
|
|
104
|
+
isRecording: boolean;
|
|
105
|
+
duration: number; // In milliseconds
|
|
106
|
+
currentFilePath?: string;
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Platform Setup
|
|
111
|
+
|
|
112
|
+
### Android
|
|
113
|
+
|
|
114
|
+
Add permission to `android/app/src/main/AndroidManifest.xml`:
|
|
115
|
+
|
|
116
|
+
```xml
|
|
117
|
+
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### iOS
|
|
121
|
+
|
|
122
|
+
Add microphone permission to `ios/[AppName]/Info.plist`:
|
|
123
|
+
|
|
124
|
+
```xml
|
|
125
|
+
<key>NSMicrophoneUsageDescription</key>
|
|
126
|
+
<string>This app needs access to your microphone to record audio.</string>
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Configuration
|
|
130
|
+
|
|
131
|
+
### Default Settings
|
|
132
|
+
|
|
133
|
+
| Setting | Value |
|
|
134
|
+
|---------|-------|
|
|
135
|
+
| Format | M4A (AAC Codec) |
|
|
136
|
+
| Bitrate | 128 kbps |
|
|
137
|
+
| Sample Rate | 44.1 kHz |
|
|
138
|
+
| Channels | 1 (Mono) |
|
|
139
|
+
|
|
140
|
+
### Recommended Settings
|
|
141
|
+
|
|
142
|
+
- **Speech**: 64 kbps, 16 kHz, mono
|
|
143
|
+
- **Music**: 192-320 kbps, 44.1-48 kHz, stereo
|
|
144
|
+
- **Balanced**: 128 kbps, 44.1 kHz, mono
|
|
145
|
+
|
|
146
|
+
## File Locations
|
|
147
|
+
|
|
148
|
+
- **Android**: `Documents/NosniaAudioRecorder/` (API 29+) or `Music/NosniaAudioRecorder/`
|
|
149
|
+
- **iOS**: `Documents/NosniaAudioRecorder/`
|
|
150
|
+
|
|
151
|
+
## Performance
|
|
152
|
+
|
|
153
|
+
- Lightweight native implementation
|
|
154
|
+
- Minimal memory footprint
|
|
155
|
+
- Direct encoding to M4A format
|
|
156
|
+
- Efficient file handling
|
|
157
|
+
|
|
158
|
+
## More Documentation
|
|
159
|
+
|
|
160
|
+
For detailed usage guide, see [USAGE.md](USAGE.md)
|
|
161
|
+
|
|
162
|
+
## Contributing
|
|
163
|
+
|
|
164
|
+
- [Development workflow](CONTRIBUTING.md#development-workflow)
|
|
165
|
+
- [Sending a pull request](CONTRIBUTING.md#sending-a-pull-request)
|
|
166
|
+
- [Code of conduct](CODE_OF_CONDUCT.md)
|
|
167
|
+
|
|
168
|
+
## License
|
|
169
|
+
|
|
170
|
+
MIT
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
|
|
175
|
+
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
ext.getExtOrDefault = {name ->
|
|
3
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['NosniaAudioRecorder_' + name]
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
repositories {
|
|
7
|
+
google()
|
|
8
|
+
mavenCentral()
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
dependencies {
|
|
12
|
+
classpath "com.android.tools.build:gradle:8.7.2"
|
|
13
|
+
// noinspection DifferentKotlinGradleVersion
|
|
14
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
apply plugin: "com.android.library"
|
|
20
|
+
apply plugin: "kotlin-android"
|
|
21
|
+
|
|
22
|
+
apply plugin: "com.facebook.react"
|
|
23
|
+
|
|
24
|
+
def getExtOrIntegerDefault(name) {
|
|
25
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["NosniaAudioRecorder_" + name]).toInteger()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
android {
|
|
29
|
+
namespace "com.nosniaaudiorecorder"
|
|
30
|
+
|
|
31
|
+
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
32
|
+
|
|
33
|
+
defaultConfig {
|
|
34
|
+
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
|
35
|
+
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
buildFeatures {
|
|
39
|
+
buildConfig true
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
buildTypes {
|
|
43
|
+
release {
|
|
44
|
+
minifyEnabled false
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
lintOptions {
|
|
49
|
+
disable "GradleCompatible"
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
compileOptions {
|
|
53
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
54
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
sourceSets {
|
|
58
|
+
main {
|
|
59
|
+
java.srcDirs += [
|
|
60
|
+
"generated/java",
|
|
61
|
+
"generated/jni"
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
repositories {
|
|
68
|
+
mavenCentral()
|
|
69
|
+
google()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
def kotlin_version = getExtOrDefault("kotlinVersion")
|
|
73
|
+
|
|
74
|
+
dependencies {
|
|
75
|
+
implementation "com.facebook.react:react-android"
|
|
76
|
+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
77
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
package com.nosniaaudiorecorder
|
|
2
|
+
|
|
3
|
+
import android.Manifest
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import android.media.MediaRecorder
|
|
6
|
+
import android.os.Build
|
|
7
|
+
import android.os.Environment
|
|
8
|
+
import androidx.core.content.ContextCompat
|
|
9
|
+
import com.facebook.react.bridge.Promise
|
|
10
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
11
|
+
import com.facebook.react.bridge.ReadableMap
|
|
12
|
+
import com.facebook.react.bridge.WritableMap
|
|
13
|
+
import com.facebook.react.bridge.WritableNativeMap
|
|
14
|
+
import com.facebook.react.module.annotations.ReactModule
|
|
15
|
+
import java.io.File
|
|
16
|
+
import java.text.SimpleDateFormat
|
|
17
|
+
import java.util.Date
|
|
18
|
+
import java.util.Locale
|
|
19
|
+
|
|
20
|
+
@ReactModule(name = NosniaAudioRecorderModule.NAME)
|
|
21
|
+
class NosniaAudioRecorderModule(reactContext: ReactApplicationContext) :
|
|
22
|
+
NativeNosniaAudioRecorderSpec(reactContext) {
|
|
23
|
+
|
|
24
|
+
private var mediaRecorder: MediaRecorder? = null
|
|
25
|
+
private var currentFilePath: String? = null
|
|
26
|
+
private var isRecording = false
|
|
27
|
+
private var isPaused = false
|
|
28
|
+
|
|
29
|
+
override fun getName(): String {
|
|
30
|
+
return NAME
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
override fun startRecording(options: ReadableMap, promise: Promise) {
|
|
34
|
+
try {
|
|
35
|
+
if (isRecording) {
|
|
36
|
+
promise.reject("ALREADY_RECORDING", "Recording is already in progress")
|
|
37
|
+
return
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
val filename = options.getString("filename") ?: generateFilename()
|
|
41
|
+
val bitrate = options.getInt("bitrate").takeIf { it > 0 } ?: 128000
|
|
42
|
+
val channels = options.getInt("channels").takeIf { it > 0 } ?: 1
|
|
43
|
+
val sampleRate = options.getInt("sampleRate").takeIf { it > 0 } ?: 44100
|
|
44
|
+
|
|
45
|
+
val recordingDir = getRecordingDirectory()
|
|
46
|
+
if (!recordingDir.exists()) {
|
|
47
|
+
recordingDir.mkdirs()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
currentFilePath = File(recordingDir, filename).absolutePath
|
|
51
|
+
|
|
52
|
+
mediaRecorder = (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
53
|
+
MediaRecorder(reactApplicationContext)
|
|
54
|
+
} else {
|
|
55
|
+
@Suppress("DEPRECATION")
|
|
56
|
+
MediaRecorder()
|
|
57
|
+
}).apply {
|
|
58
|
+
setAudioSource(MediaRecorder.AudioSource.MIC)
|
|
59
|
+
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
|
|
60
|
+
setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
|
|
61
|
+
setAudioEncodingBitRate(bitrate)
|
|
62
|
+
setAudioChannels(channels)
|
|
63
|
+
setAudioSamplingRate(sampleRate)
|
|
64
|
+
setOutputFile(currentFilePath)
|
|
65
|
+
prepare()
|
|
66
|
+
start()
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
isRecording = true
|
|
70
|
+
isPaused = false
|
|
71
|
+
promise.resolve(null)
|
|
72
|
+
} catch (e: Exception) {
|
|
73
|
+
mediaRecorder?.release()
|
|
74
|
+
mediaRecorder = null
|
|
75
|
+
isRecording = false
|
|
76
|
+
promise.reject("START_RECORDING_ERROR", e.message, e)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
override fun stopRecording(promise: Promise) {
|
|
81
|
+
try {
|
|
82
|
+
if (!isRecording && mediaRecorder == null) {
|
|
83
|
+
promise.reject("NOT_RECORDING", "No recording in progress")
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
mediaRecorder?.apply {
|
|
88
|
+
stop()
|
|
89
|
+
release()
|
|
90
|
+
}
|
|
91
|
+
mediaRecorder = null
|
|
92
|
+
isRecording = false
|
|
93
|
+
isPaused = false
|
|
94
|
+
|
|
95
|
+
val filePath = currentFilePath ?: ""
|
|
96
|
+
currentFilePath = null
|
|
97
|
+
promise.resolve(filePath)
|
|
98
|
+
} catch (e: Exception) {
|
|
99
|
+
mediaRecorder?.release()
|
|
100
|
+
mediaRecorder = null
|
|
101
|
+
isRecording = false
|
|
102
|
+
promise.reject("STOP_RECORDING_ERROR", e.message, e)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
override fun pauseRecording(promise: Promise) {
|
|
107
|
+
try {
|
|
108
|
+
if (!isRecording) {
|
|
109
|
+
promise.reject("NOT_RECORDING", "No recording in progress")
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
114
|
+
mediaRecorder?.pause()
|
|
115
|
+
isPaused = true
|
|
116
|
+
promise.resolve(null)
|
|
117
|
+
} else {
|
|
118
|
+
promise.reject("NOT_SUPPORTED", "Pause is not supported on this API level")
|
|
119
|
+
}
|
|
120
|
+
} catch (e: Exception) {
|
|
121
|
+
promise.reject("PAUSE_ERROR", e.message, e)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
override fun resumeRecording(promise: Promise) {
|
|
126
|
+
try {
|
|
127
|
+
if (!isRecording || !isPaused) {
|
|
128
|
+
promise.reject("NOT_PAUSED", "Recording is not paused")
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
133
|
+
mediaRecorder?.resume()
|
|
134
|
+
isPaused = false
|
|
135
|
+
promise.resolve(null)
|
|
136
|
+
} else {
|
|
137
|
+
promise.reject("NOT_SUPPORTED", "Resume is not supported on this API level")
|
|
138
|
+
}
|
|
139
|
+
} catch (e: Exception) {
|
|
140
|
+
promise.reject("RESUME_ERROR", e.message, e)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
override fun cancelRecording(promise: Promise) {
|
|
145
|
+
try {
|
|
146
|
+
mediaRecorder?.apply {
|
|
147
|
+
try {
|
|
148
|
+
stop()
|
|
149
|
+
} catch (e: Exception) {
|
|
150
|
+
// Might throw if already stopped
|
|
151
|
+
}
|
|
152
|
+
release()
|
|
153
|
+
}
|
|
154
|
+
mediaRecorder = null
|
|
155
|
+
|
|
156
|
+
currentFilePath?.let { filePath ->
|
|
157
|
+
File(filePath).delete()
|
|
158
|
+
}
|
|
159
|
+
currentFilePath = null
|
|
160
|
+
isRecording = false
|
|
161
|
+
isPaused = false
|
|
162
|
+
|
|
163
|
+
promise.resolve(null)
|
|
164
|
+
} catch (e: Exception) {
|
|
165
|
+
promise.reject("CANCEL_ERROR", e.message, e)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
override fun getRecorderStatus(promise: Promise) {
|
|
170
|
+
try {
|
|
171
|
+
val status = WritableNativeMap().apply {
|
|
172
|
+
putBoolean("isRecording", isRecording)
|
|
173
|
+
putDouble("duration", mediaRecorder?.currentPosition?.toDouble() ?: 0.0)
|
|
174
|
+
if (currentFilePath != null) {
|
|
175
|
+
putString("currentFilePath", currentFilePath)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
promise.resolve(status)
|
|
179
|
+
} catch (e: Exception) {
|
|
180
|
+
promise.reject("STATUS_ERROR", e.message, e)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
override fun requestAudioPermission(promise: Promise) {
|
|
185
|
+
try {
|
|
186
|
+
val permission = Manifest.permission.RECORD_AUDIO
|
|
187
|
+
val hasPermission = ContextCompat.checkSelfPermission(
|
|
188
|
+
reactApplicationContext,
|
|
189
|
+
permission
|
|
190
|
+
) == android.content.pm.PackageManager.PERMISSION_GRANTED
|
|
191
|
+
|
|
192
|
+
promise.resolve(hasPermission)
|
|
193
|
+
} catch (e: Exception) {
|
|
194
|
+
promise.reject("PERMISSION_ERROR", e.message, e)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
override fun checkAudioPermission(promise: Promise) {
|
|
199
|
+
try {
|
|
200
|
+
val permission = Manifest.permission.RECORD_AUDIO
|
|
201
|
+
val hasPermission = ContextCompat.checkSelfPermission(
|
|
202
|
+
reactApplicationContext,
|
|
203
|
+
permission
|
|
204
|
+
) == android.content.pm.PackageManager.PERMISSION_GRANTED
|
|
205
|
+
|
|
206
|
+
promise.resolve(hasPermission)
|
|
207
|
+
} catch (e: Exception) {
|
|
208
|
+
promise.reject("PERMISSION_ERROR", e.message, e)
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private fun getRecordingDirectory(): File {
|
|
213
|
+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
214
|
+
File(reactApplicationContext.getExternalFilesDir(null), "recordings")
|
|
215
|
+
} else {
|
|
216
|
+
@Suppress("DEPRECATION")
|
|
217
|
+
File(
|
|
218
|
+
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC),
|
|
219
|
+
"NosniaAudioRecorder"
|
|
220
|
+
)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private fun generateFilename(): String {
|
|
225
|
+
val timestamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(Date())
|
|
226
|
+
return "recording_$timestamp.m4a"
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
companion object {
|
|
230
|
+
const val NAME = "NosniaAudioRecorder"
|
|
231
|
+
}
|
|
232
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
package com.nosniaaudiorecorder
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
8
|
+
import java.util.HashMap
|
|
9
|
+
|
|
10
|
+
class NosniaAudioRecorderPackage : BaseReactPackage() {
|
|
11
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
12
|
+
return if (name == NosniaAudioRecorderModule.NAME) {
|
|
13
|
+
NosniaAudioRecorderModule(reactContext)
|
|
14
|
+
} else {
|
|
15
|
+
null
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
|
|
20
|
+
return ReactModuleInfoProvider {
|
|
21
|
+
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
|
|
22
|
+
moduleInfos[NosniaAudioRecorderModule.NAME] = ReactModuleInfo(
|
|
23
|
+
NosniaAudioRecorderModule.NAME,
|
|
24
|
+
NosniaAudioRecorderModule.NAME,
|
|
25
|
+
false, // canOverrideExistingModule
|
|
26
|
+
false, // needsEagerInit
|
|
27
|
+
false, // isCxxModule
|
|
28
|
+
true // isTurboModule
|
|
29
|
+
)
|
|
30
|
+
moduleInfos
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|