@capgo/native-audio 5.1.3 → 5.1.13

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.
@@ -11,6 +11,7 @@ import static ee.forgr.audio.Constant.ERROR_AUDIO_ID_MISSING;
11
11
  import static ee.forgr.audio.Constant.LOOP;
12
12
  import static ee.forgr.audio.Constant.OPT_FADE_MUSIC;
13
13
  import static ee.forgr.audio.Constant.OPT_FOCUS_AUDIO;
14
+ import static ee.forgr.audio.Constant.RATE;
14
15
  import static ee.forgr.audio.Constant.VOLUME;
15
16
 
16
17
  import android.Manifest;
@@ -18,8 +19,10 @@ import android.content.Context;
18
19
  import android.content.res.AssetFileDescriptor;
19
20
  import android.content.res.AssetManager;
20
21
  import android.media.AudioManager;
22
+ import android.os.Build;
21
23
  import android.os.ParcelFileDescriptor;
22
24
  import android.util.Log;
25
+ import androidx.annotation.RequiresApi;
23
26
  import com.getcapacitor.JSObject;
24
27
  import com.getcapacitor.Plugin;
25
28
  import com.getcapacitor.PluginCall;
@@ -33,458 +36,505 @@ import java.util.HashMap;
33
36
  import java.util.concurrent.Callable;
34
37
 
35
38
  @CapacitorPlugin(
36
- permissions = {
37
- @Permission(strings = { Manifest.permission.MODIFY_AUDIO_SETTINGS }),
38
- @Permission(strings = { Manifest.permission.WRITE_EXTERNAL_STORAGE }),
39
- @Permission(strings = { Manifest.permission.READ_PHONE_STATE })
40
- }
39
+ permissions = {
40
+ @Permission(strings = { Manifest.permission.MODIFY_AUDIO_SETTINGS }),
41
+ @Permission(strings = { Manifest.permission.WRITE_EXTERNAL_STORAGE }),
42
+ @Permission(strings = { Manifest.permission.READ_PHONE_STATE }),
43
+ }
41
44
  )
42
- public class NativeAudio extends Plugin implements AudioManager.OnAudioFocusChangeListener {
43
-
44
- public static final String TAG = "NativeAudio";
45
-
46
- private static HashMap<String, AudioAsset> audioAssetList;
47
- private static ArrayList<AudioAsset> resumeList;
48
- private boolean fadeMusic = false;
49
- private AudioManager audioManager;
50
-
51
- @Override
52
- public void load() {
53
- super.load();
54
-
55
- this.audioManager = (AudioManager) getBridge().getActivity().getSystemService(Context.AUDIO_SERVICE);
56
- }
57
-
58
- @Override
59
- public void onAudioFocusChange(int focusChange) {
60
- if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {} else if (
61
- focusChange == AudioManager.AUDIOFOCUS_LOSS
62
- ) {}
63
- }
64
-
65
- @Override
66
- protected void handleOnPause() {
67
- super.handleOnPause();
68
-
69
- try {
70
- if (audioAssetList != null) {
71
- for (HashMap.Entry<String, AudioAsset> entry : audioAssetList.entrySet()) {
72
- AudioAsset audio = entry.getValue();
73
-
74
- if (audio != null) {
75
- boolean wasPlaying = audio.pause();
76
-
77
- if (wasPlaying) {
78
- resumeList.add(audio);
79
- }
80
- }
81
- }
82
- }
83
- } catch (Exception ex) {
84
- Log.d(TAG, "Exception caught while listening for handleOnPause: " + ex.getLocalizedMessage());
85
- }
86
- }
87
-
88
- @Override
89
- protected void handleOnResume() {
90
- super.handleOnResume();
91
-
92
- try {
93
- if (resumeList != null) {
94
- while (!resumeList.isEmpty()) {
95
- AudioAsset audio = resumeList.remove(0);
96
-
97
- if (audio != null) {
98
- audio.resume();
99
- }
100
- }
45
+ public class NativeAudio
46
+ extends Plugin
47
+ implements AudioManager.OnAudioFocusChangeListener {
48
+
49
+ public static final String TAG = "NativeAudio";
50
+
51
+ private static HashMap<String, AudioAsset> audioAssetList;
52
+ private static ArrayList<AudioAsset> resumeList;
53
+ private boolean fadeMusic = false;
54
+ private AudioManager audioManager;
55
+
56
+ @Override
57
+ public void load() {
58
+ super.load();
59
+
60
+ this.audioManager =
61
+ (AudioManager) getBridge()
62
+ .getActivity()
63
+ .getSystemService(Context.AUDIO_SERVICE);
64
+ }
65
+
66
+ @Override
67
+ public void onAudioFocusChange(int focusChange) {
68
+ if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {} else if (
69
+ focusChange == AudioManager.AUDIOFOCUS_GAIN
70
+ ) {} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {}
71
+ }
72
+
73
+ @Override
74
+ protected void handleOnPause() {
75
+ super.handleOnPause();
76
+
77
+ try {
78
+ if (audioAssetList != null) {
79
+ for (HashMap.Entry<String, AudioAsset> entry : audioAssetList.entrySet()) {
80
+ AudioAsset audio = entry.getValue();
81
+
82
+ if (audio != null) {
83
+ boolean wasPlaying = audio.pause();
84
+
85
+ if (wasPlaying) {
86
+ resumeList.add(audio);
101
87
  }
102
- } catch (Exception ex) {
103
- Log.d(TAG, "Exception caught while listening for handleOnResume: " + ex.getLocalizedMessage());
88
+ }
104
89
  }
90
+ }
91
+ } catch (Exception ex) {
92
+ Log.d(
93
+ TAG,
94
+ "Exception caught while listening for handleOnPause: " +
95
+ ex.getLocalizedMessage()
96
+ );
105
97
  }
98
+ }
106
99
 
107
- @PluginMethod
108
- public void configure(PluginCall call) {
109
- initSoundPool();
100
+ @Override
101
+ protected void handleOnResume() {
102
+ super.handleOnResume();
110
103
 
111
- if (call.hasOption(OPT_FADE_MUSIC)) this.fadeMusic = call.getBoolean(OPT_FADE_MUSIC);
104
+ try {
105
+ if (resumeList != null) {
106
+ while (!resumeList.isEmpty()) {
107
+ AudioAsset audio = resumeList.remove(0);
112
108
 
113
- if (call.hasOption(OPT_FOCUS_AUDIO) && this.audioManager != null) {
114
- if (call.getBoolean(OPT_FOCUS_AUDIO)) {
115
- this.audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
116
- } else {
117
- this.audioManager.abandonAudioFocus(this);
118
- }
109
+ if (audio != null) {
110
+ audio.resume();
111
+ }
119
112
  }
113
+ }
114
+ } catch (Exception ex) {
115
+ Log.d(
116
+ TAG,
117
+ "Exception caught while listening for handleOnResume: " +
118
+ ex.getLocalizedMessage()
119
+ );
120
120
  }
121
-
122
- @PluginMethod
123
- public void preload(final PluginCall call) {
124
- new Thread(
125
- new Runnable() {
126
- @Override
127
- public void run() {
128
- preloadAsset(call);
129
- }
130
- }
131
- )
132
- .start();
121
+ }
122
+
123
+ @PluginMethod
124
+ public void configure(PluginCall call) {
125
+ initSoundPool();
126
+
127
+ if (call.hasOption(OPT_FADE_MUSIC)) this.fadeMusic =
128
+ call.getBoolean(OPT_FADE_MUSIC);
129
+
130
+ if (call.hasOption(OPT_FOCUS_AUDIO) && this.audioManager != null) {
131
+ if (call.getBoolean(OPT_FOCUS_AUDIO)) {
132
+ this.audioManager.requestAudioFocus(
133
+ this,
134
+ AudioManager.STREAM_MUSIC,
135
+ AudioManager.AUDIOFOCUS_GAIN
136
+ );
137
+ } else {
138
+ this.audioManager.abandonAudioFocus(this);
139
+ }
133
140
  }
134
-
135
- @PluginMethod
136
- public void play(final PluginCall call) {
137
- getBridge()
138
- .getActivity()
139
- .runOnUiThread(
140
- new Runnable() {
141
- @Override
142
- public void run() {
143
- playOrLoop("play", call);
144
- }
145
- }
146
- );
141
+ }
142
+
143
+ @PluginMethod
144
+ public void preload(final PluginCall call) {
145
+ new Thread(
146
+ new Runnable() {
147
+ @Override
148
+ public void run() {
149
+ preloadAsset(call);
150
+ }
151
+ }
152
+ )
153
+ .start();
154
+ }
155
+
156
+ @PluginMethod
157
+ public void play(final PluginCall call) {
158
+ getBridge()
159
+ .getActivity()
160
+ .runOnUiThread(
161
+ new Runnable() {
162
+ @Override
163
+ public void run() {
164
+ playOrLoop("play", call);
165
+ }
166
+ }
167
+ );
168
+ }
169
+
170
+ @PluginMethod
171
+ public void getCurrentTime(final PluginCall call) {
172
+ try {
173
+ initSoundPool();
174
+
175
+ String audioId = call.getString(ASSET_ID);
176
+
177
+ if (!isStringValid(audioId)) {
178
+ call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
179
+ return;
180
+ }
181
+
182
+ if (audioAssetList.containsKey(audioId)) {
183
+ AudioAsset asset = audioAssetList.get(audioId);
184
+ if (asset != null) {
185
+ call.resolve(
186
+ new JSObject().put("currentTime", asset.getCurrentPosition())
187
+ );
188
+ }
189
+ } else {
190
+ call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
191
+ }
192
+ } catch (Exception ex) {
193
+ call.reject(ex.getMessage());
147
194
  }
195
+ }
148
196
 
149
- @PluginMethod
150
- public void getCurrentTime(final PluginCall call) {
151
- try {
152
- initSoundPool();
197
+ @PluginMethod
198
+ public void getDuration(final PluginCall call) {
199
+ try {
200
+ initSoundPool();
153
201
 
154
- String audioId = call.getString(ASSET_ID);
202
+ String audioId = call.getString(ASSET_ID);
155
203
 
156
- if (!isStringValid(audioId)) {
157
- call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
158
- return;
159
- }
204
+ if (!isStringValid(audioId)) {
205
+ call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
206
+ return;
207
+ }
160
208
 
161
- if (audioAssetList.containsKey(audioId)) {
162
- AudioAsset asset = audioAssetList.get(audioId);
163
- if (asset != null) {
164
- call.resolve(new JSObject().put("currentTime", asset.getCurrentPosition()));
165
- }
166
- } else {
167
- call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
168
- }
169
- } catch (Exception ex) {
170
- call.reject(ex.getMessage());
209
+ if (audioAssetList.containsKey(audioId)) {
210
+ AudioAsset asset = audioAssetList.get(audioId);
211
+ if (asset != null) {
212
+ call.resolve(new JSObject().put("duration", asset.getDuration()));
171
213
  }
214
+ } else {
215
+ call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
216
+ }
217
+ } catch (Exception ex) {
218
+ call.reject(ex.getMessage());
172
219
  }
173
-
174
- @PluginMethod
175
- public void getDuration(final PluginCall call) {
176
- try {
177
- initSoundPool();
178
-
179
- String audioId = call.getString(ASSET_ID);
180
-
181
- if (!isStringValid(audioId)) {
182
- call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
183
- return;
184
- }
185
-
186
- if (audioAssetList.containsKey(audioId)) {
187
- AudioAsset asset = audioAssetList.get(audioId);
188
- if (asset != null) {
189
- call.resolve(new JSObject().put("duration", asset.getDuration()));
190
- }
191
- } else {
192
- call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
193
- }
194
- } catch (Exception ex) {
195
- call.reject(ex.getMessage());
220
+ }
221
+
222
+ @PluginMethod
223
+ public void loop(final PluginCall call) {
224
+ getBridge()
225
+ .getActivity()
226
+ .runOnUiThread(
227
+ new Runnable() {
228
+ @Override
229
+ public void run() {
230
+ playOrLoop("loop", call);
231
+ }
232
+ }
233
+ );
234
+ }
235
+
236
+ @PluginMethod
237
+ public void pause(PluginCall call) {
238
+ try {
239
+ initSoundPool();
240
+ String audioId = call.getString(ASSET_ID);
241
+
242
+ if (audioAssetList.containsKey(audioId)) {
243
+ AudioAsset asset = audioAssetList.get(audioId);
244
+ if (asset != null) {
245
+ boolean wasPlaying = asset.pause();
246
+
247
+ if (wasPlaying) {
248
+ resumeList.add(asset);
249
+ }
250
+ call.resolve();
251
+ } else {
252
+ call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
196
253
  }
254
+ } else {
255
+ call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
256
+ }
257
+ } catch (Exception ex) {
258
+ call.reject(ex.getMessage());
197
259
  }
198
-
199
- @PluginMethod
200
- public void loop(final PluginCall call) {
201
- getBridge()
202
- .getActivity()
203
- .runOnUiThread(
204
- new Runnable() {
205
- @Override
206
- public void run() {
207
- playOrLoop("loop", call);
208
- }
209
- }
210
- );
260
+ }
261
+
262
+ @PluginMethod
263
+ public void resume(PluginCall call) {
264
+ try {
265
+ initSoundPool();
266
+ String audioId = call.getString(ASSET_ID);
267
+
268
+ if (audioAssetList.containsKey(audioId)) {
269
+ AudioAsset asset = audioAssetList.get(audioId);
270
+ if (asset != null) {
271
+ asset.resume();
272
+ resumeList.add(asset);
273
+ call.resolve();
274
+ } else {
275
+ call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
276
+ }
277
+ } else {
278
+ call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
279
+ }
280
+ } catch (Exception ex) {
281
+ call.reject(ex.getMessage());
211
282
  }
212
-
213
- @PluginMethod
214
- public void pause(PluginCall call) {
215
- try {
216
- initSoundPool();
217
- String audioId = call.getString(ASSET_ID);
218
-
219
- if (audioAssetList.containsKey(audioId)) {
220
- AudioAsset asset = audioAssetList.get(audioId);
221
- if (asset != null) {
222
- boolean wasPlaying = asset.pause();
223
-
224
- if (wasPlaying) {
225
- resumeList.add(asset);
226
- }
227
- call.resolve();
228
- } else {
229
- call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
230
- }
231
- } else {
232
- call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
233
- }
234
- } catch (Exception ex) {
235
- call.reject(ex.getMessage());
283
+ }
284
+
285
+ @PluginMethod
286
+ public void stop(PluginCall call) {
287
+ try {
288
+ initSoundPool();
289
+ String audioId = call.getString(ASSET_ID);
290
+
291
+ if (audioAssetList.containsKey(audioId)) {
292
+ AudioAsset asset = audioAssetList.get(audioId);
293
+ if (asset != null) {
294
+ asset.stop();
295
+ call.resolve();
296
+ } else {
297
+ call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
236
298
  }
299
+ } else {
300
+ call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
301
+ }
302
+ } catch (Exception ex) {
303
+ call.reject(ex.getMessage());
237
304
  }
238
-
239
- @PluginMethod
240
- public void resume(PluginCall call) {
241
- try {
242
- initSoundPool();
243
- String audioId = call.getString(ASSET_ID);
244
-
245
- if (audioAssetList.containsKey(audioId)) {
246
- AudioAsset asset = audioAssetList.get(audioId);
247
- if (asset != null) {
248
- asset.resume();
249
- resumeList.add(asset);
250
- call.resolve();
251
- } else {
252
- call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
253
- }
254
- } else {
255
- call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
256
- }
257
- } catch (Exception ex) {
258
- call.reject(ex.getMessage());
305
+ }
306
+
307
+ @PluginMethod
308
+ public void unload(PluginCall call) {
309
+ try {
310
+ initSoundPool();
311
+ new JSObject();
312
+ JSObject status;
313
+
314
+ if (isStringValid(call.getString(ASSET_ID))) {
315
+ String audioId = call.getString(ASSET_ID);
316
+
317
+ if (audioAssetList.containsKey(audioId)) {
318
+ AudioAsset asset = audioAssetList.get(audioId);
319
+ if (asset != null) {
320
+ asset.unload();
321
+ audioAssetList.remove(audioId);
322
+ status = new JSObject();
323
+ status.put("status", "OK");
324
+ call.resolve(status);
325
+ } else {
326
+ status = new JSObject();
327
+ status.put("status", false);
328
+ call.resolve(status);
329
+ }
330
+ } else {
331
+ status = new JSObject();
332
+ status.put("status", ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
333
+ call.resolve(status);
259
334
  }
335
+ } else {
336
+ status = new JSObject();
337
+ status.put("status", ERROR_AUDIO_ID_MISSING);
338
+ call.resolve(status);
339
+ }
340
+ } catch (Exception ex) {
341
+ call.reject(ex.getMessage());
260
342
  }
261
-
262
- @PluginMethod
263
- public void stop(PluginCall call) {
264
- try {
265
- initSoundPool();
266
- String audioId = call.getString(ASSET_ID);
267
-
268
- if (audioAssetList.containsKey(audioId)) {
269
- AudioAsset asset = audioAssetList.get(audioId);
270
- if (asset != null) {
271
- asset.stop();
272
- call.resolve();
273
- } else {
274
- call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
275
- }
276
- } else {
277
- call.reject(ERROR_ASSET_NOT_LOADED + " - " + audioId);
278
- }
279
- } catch (Exception ex) {
280
- call.reject(ex.getMessage());
343
+ }
344
+
345
+ @PluginMethod
346
+ public void setVolume(PluginCall call) {
347
+ try {
348
+ initSoundPool();
349
+
350
+ String audioId = call.getString(ASSET_ID);
351
+ float volume = call.getFloat(VOLUME);
352
+
353
+ if (audioAssetList.containsKey(audioId)) {
354
+ AudioAsset asset = audioAssetList.get(audioId);
355
+ if (asset != null) {
356
+ asset.setVolume(volume);
357
+ call.resolve();
358
+ } else {
359
+ call.reject(ERROR_AUDIO_ASSET_MISSING);
281
360
  }
361
+ } else {
362
+ call.reject(ERROR_AUDIO_ASSET_MISSING);
363
+ }
364
+ } catch (Exception ex) {
365
+ call.reject(ex.getMessage());
282
366
  }
367
+ }
283
368
 
284
- @PluginMethod
285
- public void unload(PluginCall call) {
286
- try {
287
- initSoundPool();
288
- new JSObject();
289
- JSObject status;
290
-
291
- if (isStringValid(call.getString(ASSET_ID))) {
292
- String audioId = call.getString(ASSET_ID);
293
-
294
- if (audioAssetList.containsKey(audioId)) {
295
- AudioAsset asset = audioAssetList.get(audioId);
296
- if (asset != null) {
297
- asset.unload();
298
- audioAssetList.remove(audioId);
299
- status = new JSObject();
300
- status.put("status", "OK");
301
- call.resolve(status);
302
- } else {
303
- status = new JSObject();
304
- status.put("status", false);
305
- call.resolve(status);
306
- }
307
- } else {
308
- status = new JSObject();
309
- status.put("status", ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
310
- call.resolve(status);
311
- }
312
- } else {
313
- status = new JSObject();
314
- status.put("status", ERROR_AUDIO_ID_MISSING);
315
- call.resolve(status);
316
- }
317
- } catch (Exception ex) {
318
- call.reject(ex.getMessage());
369
+ @RequiresApi(api = Build.VERSION_CODES.M)
370
+ @PluginMethod
371
+ public void setRate(PluginCall call) {
372
+ try {
373
+ initSoundPool();
374
+
375
+ String audioId = call.getString(ASSET_ID);
376
+ float rate = call.getFloat(RATE);
377
+
378
+ if (audioAssetList.containsKey(audioId)) {
379
+ AudioAsset asset = audioAssetList.get(audioId);
380
+ if (asset != null) {
381
+ asset.setRate(rate);
319
382
  }
383
+ } else {
384
+ call.reject(ERROR_AUDIO_ASSET_MISSING);
385
+ }
386
+ } catch (Exception ex) {
387
+ call.reject(ex.getMessage());
320
388
  }
321
-
322
- @PluginMethod
323
- public void setVolume(PluginCall call) {
324
- try {
325
- initSoundPool();
326
-
327
- String audioId = call.getString(ASSET_ID);
328
- float volume = call.getFloat(VOLUME);
329
-
330
- if (audioAssetList.containsKey(audioId)) {
331
- AudioAsset asset = audioAssetList.get(audioId);
332
- if (asset != null) {
333
- asset.setVolume(volume);
334
- call.resolve();
335
- } else {
336
- call.reject(ERROR_AUDIO_ASSET_MISSING);
337
- }
338
- } else {
339
- call.reject(ERROR_AUDIO_ASSET_MISSING);
340
- }
341
- } catch (Exception ex) {
342
- call.reject(ex.getMessage());
389
+ }
390
+
391
+ @PluginMethod
392
+ public void isPlaying(final PluginCall call) {
393
+ try {
394
+ initSoundPool();
395
+
396
+ String audioId = call.getString(ASSET_ID);
397
+
398
+ if (!isStringValid(audioId)) {
399
+ call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
400
+ return;
401
+ }
402
+
403
+ if (audioAssetList.containsKey(audioId)) {
404
+ AudioAsset asset = audioAssetList.get(audioId);
405
+ if (asset != null) {
406
+ call.resolve(new JSObject().put("isPlaying", asset.isPlaying()));
407
+ } else {
408
+ call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
343
409
  }
410
+ } else {
411
+ call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
412
+ }
413
+ } catch (Exception ex) {
414
+ call.reject(ex.getMessage());
344
415
  }
416
+ }
345
417
 
346
- @PluginMethod
347
- public void isPlaying(final PluginCall call) {
348
- try {
349
- initSoundPool();
418
+ public void dispatchComplete(String assetId) {
419
+ JSObject ret = new JSObject();
420
+ ret.put("assetId", assetId);
421
+ notifyListeners("complete", ret);
422
+ }
350
423
 
351
- String audioId = call.getString(ASSET_ID);
424
+ private void preloadAsset(PluginCall call) {
425
+ double volume = 1.0;
426
+ int audioChannelNum = 1;
352
427
 
353
- if (!isStringValid(audioId)) {
354
- call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
355
- return;
356
- }
428
+ try {
429
+ initSoundPool();
357
430
 
358
- if (audioAssetList.containsKey(audioId)) {
359
- AudioAsset asset = audioAssetList.get(audioId);
360
- if (asset != null) {
361
- call.resolve(new JSObject().put("isPlaying", asset.isPlaying()));
362
- } else {
363
- call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
364
- }
365
- } else {
366
- call.reject(ERROR_AUDIO_ASSET_MISSING + " - " + audioId);
367
- }
368
- } catch (Exception ex) {
369
- call.reject(ex.getMessage());
370
- }
371
- }
372
-
373
- public void dispatchComplete(String assetId) {
374
- JSObject ret = new JSObject();
375
- ret.put("assetId", assetId);
376
- notifyListeners("complete", ret);
377
- }
431
+ String audioId = call.getString(ASSET_ID);
378
432
 
379
- private void preloadAsset(PluginCall call) {
380
- double volume = 1.0;
381
- int audioChannelNum = 1;
433
+ boolean isUrl = call.getBoolean("isUrl", false);
382
434
 
383
- try {
384
- initSoundPool();
435
+ if (!isStringValid(audioId)) {
436
+ call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
437
+ return;
438
+ }
385
439
 
386
- String audioId = call.getString(ASSET_ID);
440
+ if (!audioAssetList.containsKey(audioId)) {
441
+ String assetPath = call.getString(ASSET_PATH);
387
442
 
388
- boolean isUrl = call.getBoolean("isUrl", false);
443
+ if (!isStringValid(assetPath)) {
444
+ call.reject(
445
+ ERROR_ASSET_PATH_MISSING + " - " + audioId + " - " + assetPath
446
+ );
447
+ return;
448
+ }
389
449
 
390
- if (!isStringValid(audioId)) {
391
- call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
392
- return;
393
- }
450
+ String fullPath = assetPath; //"raw/".concat(assetPath);
394
451
 
395
- if (!audioAssetList.containsKey(audioId)) {
396
- String assetPath = call.getString(ASSET_PATH);
397
-
398
- if (!isStringValid(assetPath)) {
399
- call.reject(ERROR_ASSET_PATH_MISSING + " - " + audioId + " - " + assetPath);
400
- return;
401
- }
402
-
403
- String fullPath = assetPath; //"raw/".concat(assetPath);
404
-
405
- if (call.getDouble(VOLUME) == null) {
406
- volume = 1.0;
407
- } else {
408
- volume = call.getDouble(VOLUME, 0.5);
409
- }
410
-
411
- if (call.getInt(AUDIO_CHANNEL_NUM) == null) {
412
- audioChannelNum = 1;
413
- } else {
414
- audioChannelNum = call.getInt(AUDIO_CHANNEL_NUM);
415
- }
416
-
417
- AssetFileDescriptor assetFileDescriptor;
418
- if (isUrl) {
419
- File f = new File(new URI(fullPath));
420
- ParcelFileDescriptor p = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
421
- assetFileDescriptor = new AssetFileDescriptor(p, 0, -1);
422
- } else {
423
- // if fullPath dont start with public/ add it
424
- if (!fullPath.startsWith("public/")) {
425
- fullPath = "public/".concat(fullPath);
426
- }
427
- Context ctx = getBridge().getActivity().getApplicationContext();
428
- AssetManager am = ctx.getResources().getAssets();
429
- assetFileDescriptor = am.openFd(fullPath);
430
- }
431
-
432
- AudioAsset asset = new AudioAsset(this, audioId, assetFileDescriptor, audioChannelNum, (float) volume);
433
- audioAssetList.put(audioId, asset);
434
-
435
- JSObject status = new JSObject();
436
- status.put("STATUS", "OK");
437
- call.resolve(status);
438
- } else {
439
- call.reject(ERROR_AUDIO_EXISTS);
440
- }
441
- } catch (Exception ex) {
442
- call.reject(ex.getMessage());
452
+ if (call.getDouble(VOLUME) == null) {
453
+ volume = 1.0;
454
+ } else {
455
+ volume = call.getDouble(VOLUME, 0.5);
443
456
  }
444
- }
445
457
 
446
- private void playOrLoop(String action, final PluginCall call) {
447
- try {
448
- initSoundPool();
449
-
450
- final String audioId = call.getString(ASSET_ID);
451
- final Double time = call.getDouble("time", 0.0);
452
- if (audioAssetList.containsKey(audioId)) {
453
- AudioAsset asset = audioAssetList.get(audioId);
454
- if (LOOP.equals(action) && asset != null) {
455
- asset.loop();
456
- call.resolve();
457
- } else if (asset != null) {
458
- asset.play(
459
- time,
460
- new Callable<Void>() {
461
- @Override
462
- public Void call() throws Exception {
463
- call.resolve(new JSObject().put(ASSET_ID, audioId));
464
- return null;
465
- }
466
- }
467
- );
468
- } else {
469
- call.reject("Error with asset");
470
- }
471
- }
472
- } catch (Exception ex) {
473
- call.reject(ex.getMessage());
458
+ if (call.getInt(AUDIO_CHANNEL_NUM) == null) {
459
+ audioChannelNum = 1;
460
+ } else {
461
+ audioChannelNum = call.getInt(AUDIO_CHANNEL_NUM);
474
462
  }
475
- }
476
463
 
477
- private void initSoundPool() {
478
- if (audioAssetList == null) {
479
- audioAssetList = new HashMap<>();
464
+ AssetFileDescriptor assetFileDescriptor;
465
+ if (isUrl) {
466
+ File f = new File(new URI(fullPath));
467
+ ParcelFileDescriptor p = ParcelFileDescriptor.open(
468
+ f,
469
+ ParcelFileDescriptor.MODE_READ_ONLY
470
+ );
471
+ assetFileDescriptor = new AssetFileDescriptor(p, 0, -1);
472
+ } else {
473
+ // if fullPath dont start with public/ add it
474
+ if (!fullPath.startsWith("public/")) {
475
+ fullPath = "public/".concat(fullPath);
476
+ }
477
+ Context ctx = getBridge().getActivity().getApplicationContext();
478
+ AssetManager am = ctx.getResources().getAssets();
479
+ assetFileDescriptor = am.openFd(fullPath);
480
480
  }
481
481
 
482
- if (resumeList == null) {
483
- resumeList = new ArrayList<>();
482
+ AudioAsset asset = new AudioAsset(
483
+ this,
484
+ audioId,
485
+ assetFileDescriptor,
486
+ audioChannelNum,
487
+ (float) volume
488
+ );
489
+ audioAssetList.put(audioId, asset);
490
+
491
+ JSObject status = new JSObject();
492
+ status.put("STATUS", "OK");
493
+ call.resolve(status);
494
+ } else {
495
+ call.reject(ERROR_AUDIO_EXISTS);
496
+ }
497
+ } catch (Exception ex) {
498
+ call.reject(ex.getMessage());
499
+ }
500
+ }
501
+
502
+ private void playOrLoop(String action, final PluginCall call) {
503
+ try {
504
+ initSoundPool();
505
+
506
+ final String audioId = call.getString(ASSET_ID);
507
+ final Double time = call.getDouble("time", 0.0);
508
+ if (audioAssetList.containsKey(audioId)) {
509
+ AudioAsset asset = audioAssetList.get(audioId);
510
+ if (LOOP.equals(action) && asset != null) {
511
+ asset.loop();
512
+ call.resolve();
513
+ } else if (asset != null) {
514
+ asset.play(time);
515
+ call.resolve();
516
+ } else {
517
+ call.reject("Error with asset");
484
518
  }
519
+ } else {
520
+ call.reject("Error with asset");
521
+ }
522
+ } catch (Exception ex) {
523
+ call.reject(ex.getMessage());
485
524
  }
525
+ }
486
526
 
487
- private boolean isStringValid(String value) {
488
- return (value != null && !value.isEmpty() && !value.equals("null"));
527
+ private void initSoundPool() {
528
+ if (audioAssetList == null) {
529
+ audioAssetList = new HashMap<>();
489
530
  }
531
+
532
+ if (resumeList == null) {
533
+ resumeList = new ArrayList<>();
534
+ }
535
+ }
536
+
537
+ private boolean isStringValid(String value) {
538
+ return (value != null && !value.isEmpty() && !value.equals("null"));
539
+ }
490
540
  }