@capgo/native-audio 5.1.3 → 5.1.10

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