@capgo/native-audio 6.3.4 → 6.4.2

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -6,9 +6,8 @@
6
6
  </a>
7
7
 
8
8
  <div align="center">
9
- <h2>
10
- <a href="https://capgo.app/">Check out: Capgo Instant updates for capacitor</a>
11
- </h2>
9
+ <h2><a href="https://capgo.app/?ref=awesome-list"> ➡️ Get Instant updates for your App with Capgo 🚀</a></h2>
10
+ <h2><a href="https://capgo.app/consulting/"> Fix your annoying bug now, Hire a Capacitor expert 💪</a></h2>
12
11
  </div>
13
12
 
14
13
  <h3 align="center">Native Audio</h3>
@@ -114,7 +113,7 @@ import {NativeAudio} from '@capgo/native-audio'
114
113
 
115
114
  /**
116
115
  * This method will load more optimized audio files for background into memory.
117
- * @param assetPath - relative path of the file or absolute url (file://) or remote URL (https://)
116
+ * @param assetPath - relative path of the file, absolute url (file://) or remote url (https://)
118
117
  * assetId - unique identifier of the file
119
118
  * audioChannelNum - number of audio channels
120
119
  * isUrl - pass true if assetPath is a `file://` url
@@ -169,7 +168,7 @@ NativeAudio.unload({
169
168
  /**
170
169
  * This method will set the new volume for a audio file.
171
170
  * @param assetId - identifier of the asset
172
- * volume - numerical value of the volume between 0.1 - 1.0
171
+ * volume - numerical value of the volume between 0.1 - 1.0 default 1.0
173
172
  * @returns void
174
173
  */
175
174
  NativeAudio.setVolume({
@@ -225,10 +224,14 @@ NativeAudio.isPlaying({
225
224
  configure(options: ConfigureOptions) => Promise<void>
226
225
  ```
227
226
 
227
+ Configure the audio player
228
+
228
229
  | Param | Type |
229
230
  | ------------- | ------------------------------------------------------------- |
230
231
  | **`options`** | <code><a href="#configureoptions">ConfigureOptions</a></code> |
231
232
 
233
+ **Since:** 5.0.0
234
+
232
235
  --------------------
233
236
 
234
237
 
@@ -238,10 +241,33 @@ configure(options: ConfigureOptions) => Promise<void>
238
241
  preload(options: PreloadOptions) => Promise<void>
239
242
  ```
240
243
 
244
+ Load an audio file
245
+
246
+ | Param | Type |
247
+ | ------------- | --------------------------------------------------------- |
248
+ | **`options`** | <code><a href="#preloadoptions">PreloadOptions</a></code> |
249
+
250
+ **Since:** 5.0.0
251
+
252
+ --------------------
253
+
254
+
255
+ ### isPreloaded(...)
256
+
257
+ ```typescript
258
+ isPreloaded(options: PreloadOptions) => Promise<boolean>
259
+ ```
260
+
261
+ Check if an audio file is preloaded
262
+
241
263
  | Param | Type |
242
264
  | ------------- | --------------------------------------------------------- |
243
265
  | **`options`** | <code><a href="#preloadoptions">PreloadOptions</a></code> |
244
266
 
267
+ **Returns:** <code>Promise&lt;boolean&gt;</code>
268
+
269
+ **Since:** 6.1.0
270
+
245
271
  --------------------
246
272
 
247
273
 
@@ -251,22 +277,30 @@ preload(options: PreloadOptions) => Promise<void>
251
277
  play(options: { assetId: string; time?: number; delay?: number; }) => Promise<void>
252
278
  ```
253
279
 
280
+ Play an audio file
281
+
254
282
  | Param | Type |
255
283
  | ------------- | ---------------------------------------------------------------- |
256
284
  | **`options`** | <code>{ assetId: string; time?: number; delay?: number; }</code> |
257
285
 
286
+ **Since:** 5.0.0
287
+
258
288
  --------------------
259
289
 
260
290
 
261
291
  ### pause(...)
262
292
 
263
293
  ```typescript
264
- pause(options: { assetId: string; }) => Promise<void>
294
+ pause(options: Assets) => Promise<void>
265
295
  ```
266
296
 
267
- | Param | Type |
268
- | ------------- | --------------------------------- |
269
- | **`options`** | <code>{ assetId: string; }</code> |
297
+ Pause an audio file
298
+
299
+ | Param | Type |
300
+ | ------------- | ----------------------------------------- |
301
+ | **`options`** | <code><a href="#assets">Assets</a></code> |
302
+
303
+ **Since:** 5.0.0
270
304
 
271
305
  --------------------
272
306
 
@@ -274,12 +308,16 @@ pause(options: { assetId: string; }) => Promise<void>
274
308
  ### resume(...)
275
309
 
276
310
  ```typescript
277
- resume(options: { assetId: string; }) => Promise<void>
311
+ resume(options: Assets) => Promise<void>
278
312
  ```
279
313
 
280
- | Param | Type |
281
- | ------------- | --------------------------------- |
282
- | **`options`** | <code>{ assetId: string; }</code> |
314
+ Resume an audio file
315
+
316
+ | Param | Type |
317
+ | ------------- | ----------------------------------------- |
318
+ | **`options`** | <code><a href="#assets">Assets</a></code> |
319
+
320
+ **Since:** 5.0.0
283
321
 
284
322
  --------------------
285
323
 
@@ -287,12 +325,16 @@ resume(options: { assetId: string; }) => Promise<void>
287
325
  ### loop(...)
288
326
 
289
327
  ```typescript
290
- loop(options: { assetId: string; }) => Promise<void>
328
+ loop(options: Assets) => Promise<void>
291
329
  ```
292
330
 
293
- | Param | Type |
294
- | ------------- | --------------------------------- |
295
- | **`options`** | <code>{ assetId: string; }</code> |
331
+ Stop an audio file
332
+
333
+ | Param | Type |
334
+ | ------------- | ----------------------------------------- |
335
+ | **`options`** | <code><a href="#assets">Assets</a></code> |
336
+
337
+ **Since:** 5.0.0
296
338
 
297
339
  --------------------
298
340
 
@@ -300,12 +342,16 @@ loop(options: { assetId: string; }) => Promise<void>
300
342
  ### stop(...)
301
343
 
302
344
  ```typescript
303
- stop(options: { assetId: string; }) => Promise<void>
345
+ stop(options: Assets) => Promise<void>
304
346
  ```
305
347
 
306
- | Param | Type |
307
- | ------------- | --------------------------------- |
308
- | **`options`** | <code>{ assetId: string; }</code> |
348
+ Stop an audio file
349
+
350
+ | Param | Type |
351
+ | ------------- | ----------------------------------------- |
352
+ | **`options`** | <code><a href="#assets">Assets</a></code> |
353
+
354
+ **Since:** 5.0.0
309
355
 
310
356
  --------------------
311
357
 
@@ -313,12 +359,16 @@ stop(options: { assetId: string; }) => Promise<void>
313
359
  ### unload(...)
314
360
 
315
361
  ```typescript
316
- unload(options: { assetId: string; }) => Promise<void>
362
+ unload(options: Assets) => Promise<void>
317
363
  ```
318
364
 
319
- | Param | Type |
320
- | ------------- | --------------------------------- |
321
- | **`options`** | <code>{ assetId: string; }</code> |
365
+ Unload an audio file
366
+
367
+ | Param | Type |
368
+ | ------------- | ----------------------------------------- |
369
+ | **`options`** | <code><a href="#assets">Assets</a></code> |
370
+
371
+ **Since:** 5.0.0
322
372
 
323
373
  --------------------
324
374
 
@@ -329,10 +379,14 @@ unload(options: { assetId: string; }) => Promise<void>
329
379
  setVolume(options: { assetId: string; volume: number; }) => Promise<void>
330
380
  ```
331
381
 
382
+ Set the volume of an audio file
383
+
332
384
  | Param | Type |
333
385
  | ------------- | ------------------------------------------------- |
334
386
  | **`options`** | <code>{ assetId: string; volume: number; }</code> |
335
387
 
388
+ **Since:** 5.0.0
389
+
336
390
  --------------------
337
391
 
338
392
 
@@ -342,10 +396,14 @@ setVolume(options: { assetId: string; volume: number; }) => Promise<void>
342
396
  setRate(options: { assetId: string; rate: number; }) => Promise<void>
343
397
  ```
344
398
 
399
+ Set the rate of an audio file
400
+
345
401
  | Param | Type |
346
402
  | ------------- | ----------------------------------------------- |
347
403
  | **`options`** | <code>{ assetId: string; rate: number; }</code> |
348
404
 
405
+ **Since:** 5.0.0
406
+
349
407
  --------------------
350
408
 
351
409
 
@@ -355,42 +413,54 @@ setRate(options: { assetId: string; rate: number; }) => Promise<void>
355
413
  getCurrentTime(options: { assetId: string; }) => Promise<{ currentTime: number; }>
356
414
  ```
357
415
 
416
+ Set the current time of an audio file
417
+
358
418
  | Param | Type |
359
419
  | ------------- | --------------------------------- |
360
420
  | **`options`** | <code>{ assetId: string; }</code> |
361
421
 
362
422
  **Returns:** <code>Promise&lt;{ currentTime: number; }&gt;</code>
363
423
 
424
+ **Since:** 5.0.0
425
+
364
426
  --------------------
365
427
 
366
428
 
367
429
  ### getDuration(...)
368
430
 
369
431
  ```typescript
370
- getDuration(options: { assetId: string; }) => Promise<{ duration: number; }>
432
+ getDuration(options: Assets) => Promise<{ duration: number; }>
371
433
  ```
372
434
 
373
- | Param | Type |
374
- | ------------- | --------------------------------- |
375
- | **`options`** | <code>{ assetId: string; }</code> |
435
+ Get the duration of an audio file
436
+
437
+ | Param | Type |
438
+ | ------------- | ----------------------------------------- |
439
+ | **`options`** | <code><a href="#assets">Assets</a></code> |
376
440
 
377
441
  **Returns:** <code>Promise&lt;{ duration: number; }&gt;</code>
378
442
 
443
+ **Since:** 5.0.0
444
+
379
445
  --------------------
380
446
 
381
447
 
382
448
  ### isPlaying(...)
383
449
 
384
450
  ```typescript
385
- isPlaying(options: { assetId: string; }) => Promise<{ isPlaying: boolean; }>
451
+ isPlaying(options: Assets) => Promise<{ isPlaying: boolean; }>
386
452
  ```
387
453
 
388
- | Param | Type |
389
- | ------------- | --------------------------------- |
390
- | **`options`** | <code>{ assetId: string; }</code> |
454
+ Check if an audio file is playing
455
+
456
+ | Param | Type |
457
+ | ------------- | ----------------------------------------- |
458
+ | **`options`** | <code><a href="#assets">Assets</a></code> |
391
459
 
392
460
  **Returns:** <code>Promise&lt;{ isPlaying: boolean; }&gt;</code>
393
461
 
462
+ **Since:** 5.0.0
463
+
394
464
  --------------------
395
465
 
396
466
 
@@ -410,6 +480,7 @@ Listen for complete event
410
480
  **Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>
411
481
 
412
482
  **Since:** 5.0.0
483
+ return {@link CompletedEvent}
413
484
 
414
485
  --------------------
415
486
 
@@ -419,22 +490,29 @@ Listen for complete event
419
490
 
420
491
  #### ConfigureOptions
421
492
 
422
- | Prop | Type |
423
- | ---------------- | -------------------- |
424
- | **`fade`** | <code>boolean</code> |
425
- | **`focus`** | <code>boolean</code> |
426
- | **`background`** | <code>boolean</code> |
493
+ | Prop | Type | Description |
494
+ | ---------------- | -------------------- | ------------------------------------------------------- |
495
+ | **`fade`** | <code>boolean</code> | Play the audio with Fade effect, only available for IOS |
496
+ | **`focus`** | <code>boolean</code> | focus the audio with Audio Focus |
497
+ | **`background`** | <code>boolean</code> | Play the audio in the background |
427
498
 
428
499
 
429
500
  #### PreloadOptions
430
501
 
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> |
502
+ | Prop | Type | Description |
503
+ | --------------------- | -------------------- | -------------------------------------------------------------------------------------------------- |
504
+ | **`assetPath`** | <code>string</code> | Path to the audio file, relative path of the file, absolute url (file://) or remote url (https://) |
505
+ | **`assetId`** | <code>string</code> | Asset Id, unique identifier of the file |
506
+ | **`volume`** | <code>number</code> | Volume of the audio, between 0.1 and 1.0 |
507
+ | **`audioChannelNum`** | <code>number</code> | Audio channel number, default is 1 |
508
+ | **`isUrl`** | <code>boolean</code> | Is the audio file a URL, pass true if assetPath is a `file://` url |
509
+
510
+
511
+ #### Assets
512
+
513
+ | Prop | Type | Description |
514
+ | ------------- | ------------------- | --------------------------------------- |
515
+ | **`assetId`** | <code>string</code> | Asset Id, unique identifier of the file |
438
516
 
439
517
 
440
518
  #### 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