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/src/index.tsx ADDED
@@ -0,0 +1,140 @@
1
+ import NativeModule, {
2
+ type RecorderOptions,
3
+ type RecorderStatus,
4
+ } from './NativeNosniaAudioRecorder';
5
+
6
+ export type { RecorderOptions, RecorderStatus };
7
+
8
+ class AudioRecorder {
9
+ private static instance: AudioRecorder | null = null;
10
+
11
+ private constructor() {}
12
+
13
+ static getInstance(): AudioRecorder {
14
+ if (!AudioRecorder.instance) {
15
+ AudioRecorder.instance = new AudioRecorder();
16
+ }
17
+ return AudioRecorder.instance;
18
+ }
19
+
20
+ /**
21
+ * Request audio recording permission from the user
22
+ * @returns Promise that resolves to true if permission is granted
23
+ */
24
+ async requestPermission(): Promise<boolean> {
25
+ try {
26
+ return await NativeModule.requestAudioPermission();
27
+ } catch (error) {
28
+ console.error('Error requesting audio permission:', error);
29
+ throw error;
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Check if audio recording permission is already granted
35
+ * @returns Promise that resolves to true if permission is granted
36
+ */
37
+ async checkPermission(): Promise<boolean> {
38
+ try {
39
+ return await NativeModule.checkAudioPermission();
40
+ } catch (error) {
41
+ console.error('Error checking audio permission:', error);
42
+ throw error;
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Start recording audio with optional configuration
48
+ * @param options Configuration options for the recording (bitrate, channels, sample rate, filename)
49
+ * @returns Promise that resolves when recording starts
50
+ */
51
+ async startRecording(options?: RecorderOptions): Promise<void> {
52
+ try {
53
+ const hasPermission = await this.checkPermission();
54
+ if (!hasPermission) {
55
+ throw new Error(
56
+ 'Audio recording permission not granted. Request permission first.'
57
+ );
58
+ }
59
+
60
+ const config: RecorderOptions = {
61
+ bitrate: 128000, // 128 kbps
62
+ channels: 1, // Mono
63
+ sampleRate: 44100, // 44.1 kHz
64
+ ...options,
65
+ };
66
+
67
+ return await NativeModule.startRecording(config);
68
+ } catch (error) {
69
+ console.error('Error starting recording:', error);
70
+ throw error;
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Stop recording and save the file
76
+ * @returns Promise that resolves to the file path of the recorded audio
77
+ */
78
+ async stopRecording(): Promise<string> {
79
+ try {
80
+ return await NativeModule.stopRecording();
81
+ } catch (error) {
82
+ console.error('Error stopping recording:', error);
83
+ throw error;
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Pause the current recording
89
+ * @returns Promise that resolves when recording is paused
90
+ */
91
+ async pauseRecording(): Promise<void> {
92
+ try {
93
+ return await NativeModule.pauseRecording();
94
+ } catch (error) {
95
+ console.error('Error pausing recording:', error);
96
+ throw error;
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Resume a paused recording
102
+ * @returns Promise that resolves when recording resumes
103
+ */
104
+ async resumeRecording(): Promise<void> {
105
+ try {
106
+ return await NativeModule.resumeRecording();
107
+ } catch (error) {
108
+ console.error('Error resuming recording:', error);
109
+ throw error;
110
+ }
111
+ }
112
+
113
+ /**
114
+ * Cancel the current recording and discard the file
115
+ * @returns Promise that resolves when recording is cancelled
116
+ */
117
+ async cancelRecording(): Promise<void> {
118
+ try {
119
+ return await NativeModule.cancelRecording();
120
+ } catch (error) {
121
+ console.error('Error cancelling recording:', error);
122
+ throw error;
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Get the current status of the recorder
128
+ * @returns Promise that resolves to the current recorder status
129
+ */
130
+ async getStatus(): Promise<RecorderStatus> {
131
+ try {
132
+ return await NativeModule.getRecorderStatus();
133
+ } catch (error) {
134
+ console.error('Error getting recorder status:', error);
135
+ throw error;
136
+ }
137
+ }
138
+ }
139
+
140
+ export const NosniaAudioRecorder = AudioRecorder.getInstance();