@capgo/native-audio 6.3.3 → 6.4.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.
package/README.md CHANGED
@@ -114,7 +114,7 @@ import {NativeAudio} from '@capgo/native-audio'
114
114
 
115
115
  /**
116
116
  * This method will load more optimized audio files for background into memory.
117
- * @param assetPath - relative path of the file or absolute url (file://)
117
+ * @param assetPath - relative path of the file, absolute url (file://) or remote url (https://)
118
118
  * assetId - unique identifier of the file
119
119
  * audioChannelNum - number of audio channels
120
120
  * isUrl - pass true if assetPath is a `file://` url
@@ -169,7 +169,7 @@ NativeAudio.unload({
169
169
  /**
170
170
  * This method will set the new volume for a audio file.
171
171
  * @param assetId - identifier of the asset
172
- * volume - numerical value of the volume between 0.1 - 1.0
172
+ * volume - numerical value of the volume between 0.1 - 1.0 default 1.0
173
173
  * @returns void
174
174
  */
175
175
  NativeAudio.setVolume({
@@ -225,10 +225,14 @@ NativeAudio.isPlaying({
225
225
  configure(options: ConfigureOptions) => Promise<void>
226
226
  ```
227
227
 
228
+ Configure the audio player
229
+
228
230
  | Param | Type |
229
231
  | ------------- | ------------------------------------------------------------- |
230
232
  | **`options`** | <code><a href="#configureoptions">ConfigureOptions</a></code> |
231
233
 
234
+ **Since:** 5.0.0
235
+
232
236
  --------------------
233
237
 
234
238
 
@@ -238,10 +242,33 @@ configure(options: ConfigureOptions) => Promise<void>
238
242
  preload(options: PreloadOptions) => Promise<void>
239
243
  ```
240
244
 
245
+ Load an audio file
246
+
247
+ | Param | Type |
248
+ | ------------- | --------------------------------------------------------- |
249
+ | **`options`** | <code><a href="#preloadoptions">PreloadOptions</a></code> |
250
+
251
+ **Since:** 5.0.0
252
+
253
+ --------------------
254
+
255
+
256
+ ### isPreloaded(...)
257
+
258
+ ```typescript
259
+ isPreloaded(options: PreloadOptions) => Promise<boolean>
260
+ ```
261
+
262
+ Check if an audio file is preloaded
263
+
241
264
  | Param | Type |
242
265
  | ------------- | --------------------------------------------------------- |
243
266
  | **`options`** | <code><a href="#preloadoptions">PreloadOptions</a></code> |
244
267
 
268
+ **Returns:** <code>Promise&lt;boolean&gt;</code>
269
+
270
+ **Since:** 6.1.0
271
+
245
272
  --------------------
246
273
 
247
274
 
@@ -251,22 +278,30 @@ preload(options: PreloadOptions) => Promise<void>
251
278
  play(options: { assetId: string; time?: number; delay?: number; }) => Promise<void>
252
279
  ```
253
280
 
281
+ Play an audio file
282
+
254
283
  | Param | Type |
255
284
  | ------------- | ---------------------------------------------------------------- |
256
285
  | **`options`** | <code>{ assetId: string; time?: number; delay?: number; }</code> |
257
286
 
287
+ **Since:** 5.0.0
288
+
258
289
  --------------------
259
290
 
260
291
 
261
292
  ### pause(...)
262
293
 
263
294
  ```typescript
264
- pause(options: { assetId: string; }) => Promise<void>
295
+ pause(options: Assets) => Promise<void>
265
296
  ```
266
297
 
267
- | Param | Type |
268
- | ------------- | --------------------------------- |
269
- | **`options`** | <code>{ assetId: string; }</code> |
298
+ Pause an audio file
299
+
300
+ | Param | Type |
301
+ | ------------- | ----------------------------------------- |
302
+ | **`options`** | <code><a href="#assets">Assets</a></code> |
303
+
304
+ **Since:** 5.0.0
270
305
 
271
306
  --------------------
272
307
 
@@ -274,12 +309,16 @@ pause(options: { assetId: string; }) => Promise<void>
274
309
  ### resume(...)
275
310
 
276
311
  ```typescript
277
- resume(options: { assetId: string; }) => Promise<void>
312
+ resume(options: Assets) => Promise<void>
278
313
  ```
279
314
 
280
- | Param | Type |
281
- | ------------- | --------------------------------- |
282
- | **`options`** | <code>{ assetId: string; }</code> |
315
+ Resume an audio file
316
+
317
+ | Param | Type |
318
+ | ------------- | ----------------------------------------- |
319
+ | **`options`** | <code><a href="#assets">Assets</a></code> |
320
+
321
+ **Since:** 5.0.0
283
322
 
284
323
  --------------------
285
324
 
@@ -287,12 +326,16 @@ resume(options: { assetId: string; }) => Promise<void>
287
326
  ### loop(...)
288
327
 
289
328
  ```typescript
290
- loop(options: { assetId: string; }) => Promise<void>
329
+ loop(options: Assets) => Promise<void>
291
330
  ```
292
331
 
293
- | Param | Type |
294
- | ------------- | --------------------------------- |
295
- | **`options`** | <code>{ assetId: string; }</code> |
332
+ Stop an audio file
333
+
334
+ | Param | Type |
335
+ | ------------- | ----------------------------------------- |
336
+ | **`options`** | <code><a href="#assets">Assets</a></code> |
337
+
338
+ **Since:** 5.0.0
296
339
 
297
340
  --------------------
298
341
 
@@ -300,12 +343,16 @@ loop(options: { assetId: string; }) => Promise<void>
300
343
  ### stop(...)
301
344
 
302
345
  ```typescript
303
- stop(options: { assetId: string; }) => Promise<void>
346
+ stop(options: Assets) => Promise<void>
304
347
  ```
305
348
 
306
- | Param | Type |
307
- | ------------- | --------------------------------- |
308
- | **`options`** | <code>{ assetId: string; }</code> |
349
+ Stop an audio file
350
+
351
+ | Param | Type |
352
+ | ------------- | ----------------------------------------- |
353
+ | **`options`** | <code><a href="#assets">Assets</a></code> |
354
+
355
+ **Since:** 5.0.0
309
356
 
310
357
  --------------------
311
358
 
@@ -313,12 +360,16 @@ stop(options: { assetId: string; }) => Promise<void>
313
360
  ### unload(...)
314
361
 
315
362
  ```typescript
316
- unload(options: { assetId: string; }) => Promise<void>
363
+ unload(options: Assets) => Promise<void>
317
364
  ```
318
365
 
319
- | Param | Type |
320
- | ------------- | --------------------------------- |
321
- | **`options`** | <code>{ assetId: string; }</code> |
366
+ Unload an audio file
367
+
368
+ | Param | Type |
369
+ | ------------- | ----------------------------------------- |
370
+ | **`options`** | <code><a href="#assets">Assets</a></code> |
371
+
372
+ **Since:** 5.0.0
322
373
 
323
374
  --------------------
324
375
 
@@ -329,10 +380,14 @@ unload(options: { assetId: string; }) => Promise<void>
329
380
  setVolume(options: { assetId: string; volume: number; }) => Promise<void>
330
381
  ```
331
382
 
383
+ Set the volume of an audio file
384
+
332
385
  | Param | Type |
333
386
  | ------------- | ------------------------------------------------- |
334
387
  | **`options`** | <code>{ assetId: string; volume: number; }</code> |
335
388
 
389
+ **Since:** 5.0.0
390
+
336
391
  --------------------
337
392
 
338
393
 
@@ -342,10 +397,14 @@ setVolume(options: { assetId: string; volume: number; }) => Promise<void>
342
397
  setRate(options: { assetId: string; rate: number; }) => Promise<void>
343
398
  ```
344
399
 
400
+ Set the rate of an audio file
401
+
345
402
  | Param | Type |
346
403
  | ------------- | ----------------------------------------------- |
347
404
  | **`options`** | <code>{ assetId: string; rate: number; }</code> |
348
405
 
406
+ **Since:** 5.0.0
407
+
349
408
  --------------------
350
409
 
351
410
 
@@ -355,42 +414,54 @@ setRate(options: { assetId: string; rate: number; }) => Promise<void>
355
414
  getCurrentTime(options: { assetId: string; }) => Promise<{ currentTime: number; }>
356
415
  ```
357
416
 
417
+ Set the current time of an audio file
418
+
358
419
  | Param | Type |
359
420
  | ------------- | --------------------------------- |
360
421
  | **`options`** | <code>{ assetId: string; }</code> |
361
422
 
362
423
  **Returns:** <code>Promise&lt;{ currentTime: number; }&gt;</code>
363
424
 
425
+ **Since:** 5.0.0
426
+
364
427
  --------------------
365
428
 
366
429
 
367
430
  ### getDuration(...)
368
431
 
369
432
  ```typescript
370
- getDuration(options: { assetId: string; }) => Promise<{ duration: number; }>
433
+ getDuration(options: Assets) => Promise<{ duration: number; }>
371
434
  ```
372
435
 
373
- | Param | Type |
374
- | ------------- | --------------------------------- |
375
- | **`options`** | <code>{ assetId: string; }</code> |
436
+ Get the duration of an audio file
437
+
438
+ | Param | Type |
439
+ | ------------- | ----------------------------------------- |
440
+ | **`options`** | <code><a href="#assets">Assets</a></code> |
376
441
 
377
442
  **Returns:** <code>Promise&lt;{ duration: number; }&gt;</code>
378
443
 
444
+ **Since:** 5.0.0
445
+
379
446
  --------------------
380
447
 
381
448
 
382
449
  ### isPlaying(...)
383
450
 
384
451
  ```typescript
385
- isPlaying(options: { assetId: string; }) => Promise<{ isPlaying: boolean; }>
452
+ isPlaying(options: Assets) => Promise<{ isPlaying: boolean; }>
386
453
  ```
387
454
 
388
- | Param | Type |
389
- | ------------- | --------------------------------- |
390
- | **`options`** | <code>{ assetId: string; }</code> |
455
+ Check if an audio file is playing
456
+
457
+ | Param | Type |
458
+ | ------------- | ----------------------------------------- |
459
+ | **`options`** | <code><a href="#assets">Assets</a></code> |
391
460
 
392
461
  **Returns:** <code>Promise&lt;{ isPlaying: boolean; }&gt;</code>
393
462
 
463
+ **Since:** 5.0.0
464
+
394
465
  --------------------
395
466
 
396
467
 
@@ -410,6 +481,7 @@ Listen for complete event
410
481
  **Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>
411
482
 
412
483
  **Since:** 5.0.0
484
+ return {@link CompletedEvent}
413
485
 
414
486
  --------------------
415
487
 
@@ -419,22 +491,29 @@ Listen for complete event
419
491
 
420
492
  #### ConfigureOptions
421
493
 
422
- | Prop | Type |
423
- | ---------------- | -------------------- |
424
- | **`fade`** | <code>boolean</code> |
425
- | **`focus`** | <code>boolean</code> |
426
- | **`background`** | <code>boolean</code> |
494
+ | Prop | Type | Description |
495
+ | ---------------- | -------------------- | ------------------------------------------------------- |
496
+ | **`fade`** | <code>boolean</code> | Play the audio with Fade effect, only available for IOS |
497
+ | **`focus`** | <code>boolean</code> | focus the audio with Audio Focus |
498
+ | **`background`** | <code>boolean</code> | Play the audio in the background |
427
499
 
428
500
 
429
501
  #### PreloadOptions
430
502
 
431
- | Prop | Type |
432
- | --------------------- | -------------------- |
433
- | **`assetPath`** | <code>string</code> |
434
- | **`assetId`** | <code>string</code> |
435
- | **`volume`** | <code>number</code> |
436
- | **`audioChannelNum`** | <code>number</code> |
437
- | **`isUrl`** | <code>boolean</code> |
503
+ | Prop | Type | Description |
504
+ | --------------------- | -------------------- | -------------------------------------------------------------------------------------------------- |
505
+ | **`assetPath`** | <code>string</code> | Path to the audio file, relative path of the file, absolute url (file://) or remote url (https://) |
506
+ | **`assetId`** | <code>string</code> | Asset Id, unique identifier of the file |
507
+ | **`volume`** | <code>number</code> | Volume of the audio, between 0.1 and 1.0 |
508
+ | **`audioChannelNum`** | <code>number</code> | Audio channel number, default is 1 |
509
+ | **`isUrl`** | <code>boolean</code> | Is the audio file a URL, pass true if assetPath is a `file://` url |
510
+
511
+
512
+ #### Assets
513
+
514
+ | Prop | Type | Description |
515
+ | ------------- | ------------------- | --------------------------------------- |
516
+ | **`assetId`** | <code>string</code> | Asset Id, unique identifier of the file |
438
517
 
439
518
 
440
519
  #### PluginListenerHandle
@@ -3,18 +3,16 @@ package ee.forgr.audio;
3
3
  import android.content.res.AssetFileDescriptor;
4
4
  import android.os.Build;
5
5
  import androidx.annotation.RequiresApi;
6
- import com.getcapacitor.JSObject;
7
6
  import java.util.ArrayList;
8
- import java.util.concurrent.Callable;
9
7
 
10
8
  public class AudioAsset {
11
9
 
12
10
  private final String TAG = "AudioAsset";
13
11
 
14
- private ArrayList<AudioDispatcher> audioList;
12
+ private final ArrayList<AudioDispatcher> audioList;
15
13
  private int playIndex = 0;
16
- private String assetId;
17
- private NativeAudio owner;
14
+ private final String assetId;
15
+ private final NativeAudio owner;
18
16
 
19
17
  AudioAsset(
20
18
  NativeAudio owner,
@@ -91,7 +89,7 @@ public class AudioAsset {
91
89
  }
92
90
 
93
91
  public void resume() throws Exception {
94
- if (audioList.size() > 0) {
92
+ if (!audioList.isEmpty()) {
95
93
  AudioDispatcher audio = audioList.get(0);
96
94
 
97
95
  if (audio != null) {
@@ -1,11 +1,18 @@
1
1
  package ee.forgr.audio;
2
2
 
3
+ import static ee.forgr.audio.Constant.INVALID;
4
+ import static ee.forgr.audio.Constant.LOOPING;
5
+ import static ee.forgr.audio.Constant.PAUSE;
6
+ import static ee.forgr.audio.Constant.PENDING_LOOP;
7
+ import static ee.forgr.audio.Constant.PENDING_PLAY;
8
+ import static ee.forgr.audio.Constant.PLAYING;
9
+ import static ee.forgr.audio.Constant.PREPARED;
10
+
3
11
  import android.content.res.AssetFileDescriptor;
4
12
  import android.media.AudioAttributes;
5
13
  import android.media.MediaPlayer;
6
14
  import android.os.Build;
7
15
  import android.util.Log;
8
- import java.util.concurrent.Callable;
9
16
 
10
17
  public class AudioDispatcher
11
18
  implements
@@ -14,16 +21,7 @@ public class AudioDispatcher
14
21
  MediaPlayer.OnSeekCompleteListener {
15
22
 
16
23
  private final String TAG = "AudioDispatcher";
17
-
18
- private final int INVALID = 0;
19
- private final int PREPARED = 1;
20
- private final int PENDING_PLAY = 2;
21
- private final int PLAYING = 3;
22
- private final int PENDING_LOOP = 4;
23
- private final int LOOPING = 5;
24
- private final int PAUSE = 6;
25
-
26
- private MediaPlayer mediaPlayer;
24
+ private final MediaPlayer mediaPlayer;
27
25
  private int mediaState;
28
26
  private AudioAsset owner;
29
27
 
@@ -63,7 +61,7 @@ public class AudioDispatcher
63
61
  }
64
62
 
65
63
  public void play(Double time) throws Exception {
66
- invokePlay(time, false);
64
+ invokePlay(time);
67
65
  }
68
66
 
69
67
  public boolean pause() throws Exception {
@@ -159,23 +157,23 @@ public class AudioDispatcher
159
157
  }
160
158
  }
161
159
 
162
- private void invokePlay(Double time, Boolean loop) {
160
+ private void invokePlay(Double time) {
163
161
  try {
164
162
  boolean playing = mediaPlayer.isPlaying();
165
163
 
166
164
  if (playing) {
167
165
  mediaPlayer.pause();
168
- mediaPlayer.setLooping(loop);
166
+ mediaPlayer.setLooping(false);
169
167
  mediaState = PENDING_PLAY;
170
168
  seek(time);
171
169
  } else {
172
170
  if (mediaState == PREPARED) {
173
- mediaState = (loop ? PENDING_LOOP : PENDING_PLAY);
171
+ mediaState = (PENDING_PLAY);
174
172
  onPrepared(mediaPlayer);
175
173
  seek(time);
176
174
  } else {
177
- mediaState = (loop ? PENDING_LOOP : PENDING_PLAY);
178
- mediaPlayer.setLooping(loop);
175
+ mediaState = (PENDING_PLAY);
176
+ mediaPlayer.setLooping(false);
179
177
  seek(time);
180
178
  }
181
179
  }
@@ -17,4 +17,11 @@ public class Constant {
17
17
  public static final String RATE = "rate";
18
18
  public static final String AUDIO_CHANNEL_NUM = "audioChannelNum";
19
19
  public static final String LOOP = "loop";
20
+ public static final int INVALID = 0;
21
+ public static final int PREPARED = 1;
22
+ public static final int PENDING_PLAY = 2;
23
+ public static final int PLAYING = 3;
24
+ public static final int PENDING_LOOP = 4;
25
+ public static final int LOOPING = 5;
26
+ public static final int PAUSE = 6;
20
27
  }
@@ -9,13 +9,11 @@ import static ee.forgr.audio.Constant.ERROR_AUDIO_ASSET_MISSING;
9
9
  import static ee.forgr.audio.Constant.ERROR_AUDIO_EXISTS;
10
10
  import static ee.forgr.audio.Constant.ERROR_AUDIO_ID_MISSING;
11
11
  import static ee.forgr.audio.Constant.LOOP;
12
- import static ee.forgr.audio.Constant.OPT_FADE_MUSIC;
13
12
  import static ee.forgr.audio.Constant.OPT_FOCUS_AUDIO;
14
13
  import static ee.forgr.audio.Constant.RATE;
15
14
  import static ee.forgr.audio.Constant.VOLUME;
16
15
 
17
16
  import android.Manifest;
18
- import android.app.Application;
19
17
  import android.content.Context;
20
18
  import android.content.res.AssetFileDescriptor;
21
19
  import android.content.res.AssetManager;
@@ -50,7 +48,6 @@ public class NativeAudio
50
48
 
51
49
  private static HashMap<String, AudioAsset> audioAssetList;
52
50
  private static ArrayList<AudioAsset> resumeList;
53
- private boolean fadeMusic = false;
54
51
  private AudioManager audioManager;
55
52
 
56
53
  @Override
@@ -125,24 +122,45 @@ public class NativeAudio
125
122
  public void configure(PluginCall call) {
126
123
  initSoundPool();
127
124
 
128
- if (call.hasOption(OPT_FADE_MUSIC)) this.fadeMusic = call.getBoolean(
129
- OPT_FADE_MUSIC
130
- );
125
+ if (this.audioManager == null) {
126
+ call.resolve();
127
+ return;
128
+ }
131
129
 
132
- if (call.hasOption(OPT_FOCUS_AUDIO) && this.audioManager != null) {
133
- if (call.getBoolean(OPT_FOCUS_AUDIO)) {
134
- this.audioManager.requestAudioFocus(
135
- this,
136
- AudioManager.STREAM_MUSIC,
137
- AudioManager.AUDIOFOCUS_GAIN
138
- );
139
- } else {
140
- this.audioManager.abandonAudioFocus(this);
141
- }
130
+ if (Boolean.TRUE.equals(call.getBoolean(OPT_FOCUS_AUDIO, false))) {
131
+ this.audioManager.requestAudioFocus(
132
+ this,
133
+ AudioManager.STREAM_MUSIC,
134
+ AudioManager.AUDIOFOCUS_GAIN
135
+ );
136
+ } else {
137
+ this.audioManager.abandonAudioFocus(this);
142
138
  }
143
139
  call.resolve();
144
140
  }
145
141
 
142
+ @PluginMethod
143
+ public void isPreloaded(final PluginCall call) {
144
+ new Thread(
145
+ new Runnable() {
146
+ @Override
147
+ public void run() {
148
+ initSoundPool();
149
+
150
+ String audioId = call.getString(ASSET_ID);
151
+
152
+ if (!isStringValid(audioId)) {
153
+ call.reject(ERROR_AUDIO_ID_MISSING + " - " + audioId);
154
+ return;
155
+ }
156
+ call.resolve(
157
+ new JSObject().put("found", audioAssetList.containsKey(audioId))
158
+ );
159
+ }
160
+ }
161
+ ).start();
162
+ }
163
+
146
164
  @PluginMethod
147
165
  public void preload(final PluginCall call) {
148
166
  new Thread(
@@ -348,7 +366,7 @@ public class NativeAudio
348
366
  initSoundPool();
349
367
 
350
368
  String audioId = call.getString(ASSET_ID);
351
- float volume = call.getFloat(VOLUME);
369
+ float volume = call.getFloat(VOLUME, 1F);
352
370
 
353
371
  if (audioAssetList.containsKey(audioId)) {
354
372
  AudioAsset asset = audioAssetList.get(audioId);
@@ -372,7 +390,7 @@ public class NativeAudio
372
390
  initSoundPool();
373
391
 
374
392
  String audioId = call.getString(ASSET_ID);
375
- float rate = call.getFloat(RATE);
393
+ float rate = call.getFloat(RATE, 1F);
376
394
 
377
395
  if (audioAssetList.containsKey(audioId)) {
378
396
  AudioAsset asset = audioAssetList.get(audioId);
@@ -421,7 +439,7 @@ public class NativeAudio
421
439
  }
422
440
 
423
441
  private void preloadAsset(PluginCall call) {
424
- double volume = 1.0;
442
+ float volume = 1F;
425
443
  int audioChannelNum = 1;
426
444
  JSObject status = new JSObject();
427
445
  status.put("STATUS", "OK");
@@ -450,17 +468,8 @@ public class NativeAudio
450
468
 
451
469
  String fullPath = assetPath; //"raw/".concat(assetPath);
452
470
 
453
- if (call.getDouble(VOLUME) == null) {
454
- volume = 1.0;
455
- } else {
456
- volume = call.getDouble(VOLUME, 0.5);
457
- }
458
-
459
- if (call.getInt(AUDIO_CHANNEL_NUM) == null) {
460
- audioChannelNum = 1;
461
- } else {
462
- audioChannelNum = call.getInt(AUDIO_CHANNEL_NUM);
463
- }
471
+ volume = call.getFloat(VOLUME, 1F);
472
+ audioChannelNum = call.getInt(AUDIO_CHANNEL_NUM, 1);
464
473
 
465
474
  AssetFileDescriptor assetFileDescriptor;
466
475
  if (isLocalUrl) {
@@ -484,7 +493,7 @@ public class NativeAudio
484
493
  audioId,
485
494
  uri,
486
495
  audioChannelNum,
487
- (float) volume
496
+ volume
488
497
  );
489
498
  audioAssetList.put(audioId, remoteAudioAsset);
490
499
  call.resolve(status);
@@ -511,7 +520,7 @@ public class NativeAudio
511
520
  audioId,
512
521
  assetFileDescriptor,
513
522
  audioChannelNum,
514
- (float) volume
523
+ volume
515
524
  );
516
525
  audioAssetList.put(audioId, asset);
517
526