@scr2em/capacitor-plugin-recorder 0.0.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.
@@ -0,0 +1,302 @@
1
+ package com.capacitor.recorderplayer;
2
+
3
+ import android.Manifest;
4
+
5
+ import com.getcapacitor.JSObject;
6
+ import com.getcapacitor.Plugin;
7
+ import com.getcapacitor.PluginCall;
8
+ import com.getcapacitor.PluginMethod;
9
+ import com.getcapacitor.annotation.CapacitorPlugin;
10
+ import com.getcapacitor.annotation.Permission;
11
+ import com.getcapacitor.annotation.PermissionCallback;
12
+ import com.getcapacitor.PermissionState;
13
+
14
+ @CapacitorPlugin(
15
+ name = "RecorderPlayer",
16
+ permissions = {
17
+ @Permission(
18
+ alias = "microphone",
19
+ strings = { Manifest.permission.RECORD_AUDIO }
20
+ )
21
+ }
22
+ )
23
+ public class RecorderPlayerPlugin extends Plugin implements RecorderPlayer.RecorderPlayerListener {
24
+
25
+ private RecorderPlayer implementation;
26
+
27
+ @Override
28
+ public void load() {
29
+ implementation = new RecorderPlayer(getContext());
30
+ implementation.setListener(this);
31
+ }
32
+
33
+ @PluginMethod
34
+ public void requestPermission(PluginCall call) {
35
+ if (getPermissionState("microphone") == PermissionState.GRANTED) {
36
+ JSObject ret = new JSObject();
37
+ ret.put("microphone", "granted");
38
+ call.resolve(ret);
39
+ } else {
40
+ requestPermissionForAlias("microphone", call, "requestPermissionCallback");
41
+ }
42
+ }
43
+
44
+ @PermissionCallback
45
+ private void requestPermissionCallback(PluginCall call) {
46
+ JSObject ret = new JSObject();
47
+ if (getPermissionState("microphone") == PermissionState.GRANTED) {
48
+ ret.put("microphone", "granted");
49
+ } else {
50
+ ret.put("microphone", "denied");
51
+ }
52
+ call.resolve(ret);
53
+ }
54
+
55
+ @PluginMethod
56
+ public void checkPermission(PluginCall call) {
57
+ JSObject ret = new JSObject();
58
+ PermissionState state = getPermissionState("microphone");
59
+
60
+ if (state == PermissionState.GRANTED) {
61
+ ret.put("microphone", "granted");
62
+ } else if (state == PermissionState.DENIED) {
63
+ ret.put("microphone", "denied");
64
+ } else {
65
+ ret.put("microphone", "prompt");
66
+ }
67
+
68
+ call.resolve(ret);
69
+ }
70
+
71
+ @PluginMethod
72
+ public void startRecord(PluginCall call) {
73
+ if (getPermissionState("microphone") != PermissionState.GRANTED) {
74
+ requestPermissionForAlias("microphone", call, "microphonePermissionCallback");
75
+ return;
76
+ }
77
+
78
+ String path = call.getString("path");
79
+ try {
80
+ implementation.startRecord(path);
81
+ call.resolve();
82
+ } catch (Exception e) {
83
+ call.reject(e.getMessage());
84
+ }
85
+ }
86
+
87
+ @PermissionCallback
88
+ private void microphonePermissionCallback(PluginCall call) {
89
+ if (getPermissionState("microphone") == PermissionState.GRANTED) {
90
+ String path = call.getString("path");
91
+ try {
92
+ implementation.startRecord(path);
93
+ call.resolve();
94
+ } catch (Exception e) {
95
+ call.reject(e.getMessage());
96
+ }
97
+ } else {
98
+ call.reject("Microphone permission is required to record audio");
99
+ }
100
+ }
101
+
102
+ @PluginMethod
103
+ public void stopRecord(PluginCall call) {
104
+ try {
105
+ RecorderPlayer.RecordResult result = implementation.stopRecord();
106
+ JSObject ret = new JSObject();
107
+ ret.put("path", result.getPath());
108
+ ret.put("duration", result.getDuration());
109
+ call.resolve(ret);
110
+ } catch (Exception e) {
111
+ call.reject(e.getMessage());
112
+ }
113
+ }
114
+
115
+ @PluginMethod
116
+ public void pauseRecord(PluginCall call) {
117
+ try {
118
+ implementation.pauseRecord();
119
+ call.resolve();
120
+ } catch (Exception e) {
121
+ call.reject(e.getMessage());
122
+ }
123
+ }
124
+
125
+ @PluginMethod
126
+ public void resumeRecord(PluginCall call) {
127
+ try {
128
+ implementation.resumeRecord();
129
+ call.resolve();
130
+ } catch (Exception e) {
131
+ call.reject(e.getMessage());
132
+ }
133
+ }
134
+
135
+ @PluginMethod
136
+ public void preparePlay(PluginCall call) {
137
+ String path = call.getString("path");
138
+ if (path == null) {
139
+ call.reject("Path is required");
140
+ return;
141
+ }
142
+
143
+ try {
144
+ RecorderPlayer.PreparePlayResult result = implementation.preparePlay(path);
145
+ JSObject ret = new JSObject();
146
+ ret.put("playerId", result.getPlayerId());
147
+ ret.put("duration", result.getDuration());
148
+ call.resolve(ret);
149
+ } catch (Exception e) {
150
+ call.reject(e.getMessage());
151
+ }
152
+ }
153
+
154
+ @PluginMethod
155
+ public void play(PluginCall call) {
156
+ String playerId = call.getString("playerId");
157
+ if (playerId == null) {
158
+ call.reject("playerId is required");
159
+ return;
160
+ }
161
+
162
+ try {
163
+ implementation.play(playerId);
164
+ call.resolve();
165
+ } catch (Exception e) {
166
+ call.reject(e.getMessage());
167
+ }
168
+ }
169
+
170
+ @PluginMethod
171
+ public void pausePlay(PluginCall call) {
172
+ String playerId = call.getString("playerId");
173
+ if (playerId == null) {
174
+ call.reject("playerId is required");
175
+ return;
176
+ }
177
+
178
+ try {
179
+ implementation.pausePlay(playerId);
180
+ call.resolve();
181
+ } catch (Exception e) {
182
+ call.reject(e.getMessage());
183
+ }
184
+ }
185
+
186
+ @PluginMethod
187
+ public void resumePlay(PluginCall call) {
188
+ String playerId = call.getString("playerId");
189
+ if (playerId == null) {
190
+ call.reject("playerId is required");
191
+ return;
192
+ }
193
+
194
+ try {
195
+ implementation.resumePlay(playerId);
196
+ call.resolve();
197
+ } catch (Exception e) {
198
+ call.reject(e.getMessage());
199
+ }
200
+ }
201
+
202
+ @PluginMethod
203
+ public void stopPlay(PluginCall call) {
204
+ String playerId = call.getString("playerId");
205
+ if (playerId == null) {
206
+ call.reject("playerId is required");
207
+ return;
208
+ }
209
+
210
+ try {
211
+ implementation.stopPlay(playerId);
212
+ call.resolve();
213
+ } catch (Exception e) {
214
+ call.reject(e.getMessage());
215
+ }
216
+ }
217
+
218
+ @PluginMethod
219
+ public void seekTo(PluginCall call) {
220
+ String playerId = call.getString("playerId");
221
+ if (playerId == null) {
222
+ call.reject("playerId is required");
223
+ return;
224
+ }
225
+
226
+ Integer position = call.getInt("position");
227
+ if (position == null) {
228
+ call.reject("Position is required");
229
+ return;
230
+ }
231
+
232
+ try {
233
+ implementation.seekTo(playerId, position);
234
+ call.resolve();
235
+ } catch (Exception e) {
236
+ call.reject(e.getMessage());
237
+ }
238
+ }
239
+
240
+ @PluginMethod
241
+ public void getPlaybackStatus(PluginCall call) {
242
+ String playerId = call.getString("playerId");
243
+ if (playerId == null) {
244
+ call.reject("playerId is required");
245
+ return;
246
+ }
247
+
248
+ RecorderPlayer.PlaybackStatusResult status = implementation.getPlaybackStatus(playerId);
249
+ if (status == null) {
250
+ call.reject("No player found with ID: " + playerId);
251
+ return;
252
+ }
253
+
254
+ JSObject ret = new JSObject();
255
+ ret.put("playerId", status.getPlayerId());
256
+ ret.put("status", status.getStatus());
257
+ ret.put("currentPosition", status.getCurrentPosition());
258
+ ret.put("duration", status.getDuration());
259
+ call.resolve(ret);
260
+ }
261
+
262
+ @PluginMethod
263
+ public void getRecordingStatus(PluginCall call) {
264
+ RecorderPlayer.RecordingStatusResult status = implementation.getRecordingStatus();
265
+ JSObject ret = new JSObject();
266
+ ret.put("status", status.getStatus());
267
+ ret.put("duration", status.getDuration());
268
+ call.resolve(ret);
269
+ }
270
+
271
+ @PluginMethod
272
+ public void destroyPlayer(PluginCall call) {
273
+ String playerId = call.getString("playerId");
274
+ if (playerId == null) {
275
+ call.reject("playerId is required");
276
+ return;
277
+ }
278
+
279
+ implementation.destroyPlayer(playerId);
280
+ call.resolve();
281
+ }
282
+
283
+ // RecorderPlayerListener implementation
284
+
285
+ @Override
286
+ public void onRecordingStatusChange(RecorderPlayer.RecordingStatus status, int duration) {
287
+ JSObject data = new JSObject();
288
+ data.put("status", status.getValue());
289
+ data.put("duration", duration);
290
+ notifyListeners("recordingStatusChange", data);
291
+ }
292
+
293
+ @Override
294
+ public void onPlaybackStatusChange(String playerId, RecorderPlayer.PlaybackStatus status, int currentPosition, int duration) {
295
+ JSObject data = new JSObject();
296
+ data.put("playerId", playerId);
297
+ data.put("status", status.getValue());
298
+ data.put("currentPosition", currentPosition);
299
+ data.put("duration", duration);
300
+ notifyListeners("playbackStatusChange", data);
301
+ }
302
+ }
File without changes