@capgo/native-audio 8.0.1 → 8.1.0
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 +124 -0
- package/android/src/main/java/ee/forgr/audio/NativeAudio.java +347 -97
- package/dist/docs.json +156 -0
- package/dist/esm/definitions.d.ts +107 -0
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.d.ts +3 -1
- package/dist/esm/web.js +58 -0
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +58 -0
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +58 -0
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/NativeAudioPlugin/AudioAsset.swift +20 -7
- package/ios/Sources/NativeAudioPlugin/Plugin.swift +363 -8
- package/ios/Sources/NativeAudioPlugin/RemoteAudioAsset.swift +31 -2
- package/ios/Tests/NativeAudioPluginTests/PluginTests.swift +371 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -243,6 +243,71 @@ The media control buttons automatically handle:
|
|
|
243
243
|
- iOS: Uses MPNowPlayingInfoCenter with MPRemoteCommandCenter
|
|
244
244
|
- Android: Uses MediaSession with NotificationCompat.MediaStyle
|
|
245
245
|
|
|
246
|
+
## Play Once (Fire-and-Forget) @since 7.11.0
|
|
247
|
+
|
|
248
|
+
For simple one-shot audio playback (sound effects, notifications, etc.), use `playOnce()` which handles the entire asset lifecycle automatically:
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
// Basic usage - automatic cleanup after playback
|
|
252
|
+
await NativeAudio.playOnce({
|
|
253
|
+
assetPath: 'audio/notification.mp3',
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
// With volume control
|
|
257
|
+
await NativeAudio.playOnce({
|
|
258
|
+
assetPath: 'audio/beep.wav',
|
|
259
|
+
volume: 0.8,
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// Remote audio with notification metadata
|
|
263
|
+
await NativeAudio.playOnce({
|
|
264
|
+
assetPath: 'https://example.com/audio.mp3',
|
|
265
|
+
isUrl: true,
|
|
266
|
+
autoPlay: true,
|
|
267
|
+
notificationMetadata: {
|
|
268
|
+
title: 'Song Name',
|
|
269
|
+
artist: 'Artist Name',
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
// Temporary file with automatic deletion
|
|
274
|
+
await NativeAudio.playOnce({
|
|
275
|
+
assetPath: 'file:///path/to/temp-audio.wav',
|
|
276
|
+
deleteAfterPlay: true, // File deleted after playback completes
|
|
277
|
+
volume: 0.5,
|
|
278
|
+
});
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
#### Advanced: Manual Control
|
|
282
|
+
|
|
283
|
+
If you need to control playback timing, set `autoPlay: false` and use the returned `assetId`:
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
const { assetId } = await NativeAudio.playOnce({
|
|
287
|
+
assetPath: 'audio/sound.wav',
|
|
288
|
+
autoPlay: false,
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
// Play later when needed
|
|
292
|
+
await NativeAudio.play({ assetId });
|
|
293
|
+
|
|
294
|
+
// Stop if needed (will auto-cleanup)
|
|
295
|
+
await NativeAudio.stop({ assetId });
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
**Key Features:**
|
|
299
|
+
- ✅ Automatic asset loading and unloading
|
|
300
|
+
- ✅ Cleanup on completion or error
|
|
301
|
+
- ✅ Optional file deletion after playback
|
|
302
|
+
- ✅ Notification metadata support
|
|
303
|
+
- ✅ Works with local files and remote URLs
|
|
304
|
+
|
|
305
|
+
**Notes:**
|
|
306
|
+
- Assets are automatically cleaned up after playback completes or on error
|
|
307
|
+
- `deleteAfterPlay` only works for local `file://` URLs, not remote URLs
|
|
308
|
+
- The returned `assetId` can be used with `play()`, `stop()`, `unload()` methods
|
|
309
|
+
- Manual cleanup via `stop()` or `unload()` is optional but supported
|
|
310
|
+
|
|
246
311
|
## Example app
|
|
247
312
|
|
|
248
313
|
This repository now ships with an interactive Capacitor project under `example/` that exercises the main APIs on web, iOS, and Android shells.
|
|
@@ -403,6 +468,45 @@ Load an audio file
|
|
|
403
468
|
--------------------
|
|
404
469
|
|
|
405
470
|
|
|
471
|
+
### playOnce(...)
|
|
472
|
+
|
|
473
|
+
```typescript
|
|
474
|
+
playOnce(options: PlayOnceOptions) => Promise<PlayOnceResult>
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
Play an audio file once with automatic cleanup
|
|
478
|
+
|
|
479
|
+
Method designed for simple, single-shot audio playback,
|
|
480
|
+
such as notification sounds, UI feedback, or other short audio clips
|
|
481
|
+
that don't require manual state management.
|
|
482
|
+
|
|
483
|
+
**Key Features:**
|
|
484
|
+
- **Fire-and-forget**: No need to manually preload, play, stop, or unload
|
|
485
|
+
- **Auto-cleanup**: Asset is automatically unloaded after playback completes
|
|
486
|
+
- **Optional file deletion**: Can delete local files after playback (useful for temp files)
|
|
487
|
+
- **Returns assetId**: Can still control playback if needed (pause, stop, etc.)
|
|
488
|
+
|
|
489
|
+
**Use Cases:**
|
|
490
|
+
- Notification sounds
|
|
491
|
+
- UI sound effects (button clicks, alerts)
|
|
492
|
+
- Short audio clips that play once
|
|
493
|
+
- Temporary audio files that should be cleaned up
|
|
494
|
+
|
|
495
|
+
**Comparison with regular play():**
|
|
496
|
+
- `play()`: Requires manual preload, play, and unload steps
|
|
497
|
+
- `playOnce()`: Handles everything automatically with a single call
|
|
498
|
+
|
|
499
|
+
| Param | Type |
|
|
500
|
+
| ------------- | ----------------------------------------------------------- |
|
|
501
|
+
| **`options`** | <code><a href="#playonceoptions">PlayOnceOptions</a></code> |
|
|
502
|
+
|
|
503
|
+
**Returns:** <code>Promise<<a href="#playonceresult">PlayOnceResult</a>></code>
|
|
504
|
+
|
|
505
|
+
**Since:** 7.11.0
|
|
506
|
+
|
|
507
|
+
--------------------
|
|
508
|
+
|
|
509
|
+
|
|
406
510
|
### isPreloaded(...)
|
|
407
511
|
|
|
408
512
|
```typescript
|
|
@@ -760,6 +864,26 @@ behavior details about audio mixing on iOS.
|
|
|
760
864
|
| **`artworkUrl`** | <code>string</code> | URL or local path to the artwork/album art image |
|
|
761
865
|
|
|
762
866
|
|
|
867
|
+
#### PlayOnceResult
|
|
868
|
+
|
|
869
|
+
| Prop | Type | Description |
|
|
870
|
+
| ------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------- |
|
|
871
|
+
| **`assetId`** | <code>string</code> | The internally generated asset ID for this playback Can be used to control playback (pause, stop, etc.) before completion |
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
#### PlayOnceOptions
|
|
875
|
+
|
|
876
|
+
| Prop | Type | Description | Default | Since |
|
|
877
|
+
| -------------------------- | --------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ | ------ |
|
|
878
|
+
| **`assetPath`** | <code>string</code> | Path to the audio file, relative path of the file, absolute url (file://) or remote url (https://) Supported formats: - MP3, WAV (all platforms) - M3U8/HLS streams (iOS and Android) | | |
|
|
879
|
+
| **`volume`** | <code>number</code> | Volume of the audio, between 0.1 and 1.0 | <code>1.0</code> | |
|
|
880
|
+
| **`isUrl`** | <code>boolean</code> | Is the audio file a URL, pass true if assetPath is a `file://` url or a streaming URL (m3u8) | <code>false</code> | |
|
|
881
|
+
| **`autoPlay`** | <code>boolean</code> | Automatically start playback after loading | <code>true</code> | |
|
|
882
|
+
| **`deleteAfterPlay`** | <code>boolean</code> | Delete the audio file from disk after playback completes Only works for local files (file:// URLs), ignored for remote URLs | <code>false</code> | 7.11.0 |
|
|
883
|
+
| **`notificationMetadata`** | <code><a href="#notificationmetadata">NotificationMetadata</a></code> | Metadata to display in the notification center when audio is playing. Only used when `showNotification: true` is set in `configure()`. See {@link <a href="#configureoptions">ConfigureOptions.showNotification</a>} for important details about how this affects audio mixing behavior on iOS. | | 7.10.0 |
|
|
884
|
+
| **`headers`** | <code><a href="#record">Record</a><string, string></code> | Custom HTTP headers to include when fetching remote audio files. Only used when isUrl is true and assetPath is a remote URL (http/https). Example: { 'x-api-key': 'abc123', 'Authorization': 'Bearer token' } | | 7.10.0 |
|
|
885
|
+
|
|
886
|
+
|
|
763
887
|
#### Assets
|
|
764
888
|
|
|
765
889
|
| Prop | Type | Description |
|
|
@@ -50,8 +50,11 @@ import java.net.URI;
|
|
|
50
50
|
import java.net.URL;
|
|
51
51
|
import java.util.ArrayList;
|
|
52
52
|
import java.util.HashMap;
|
|
53
|
+
import java.util.HashSet;
|
|
53
54
|
import java.util.Iterator;
|
|
54
55
|
import java.util.Map;
|
|
56
|
+
import java.util.Set;
|
|
57
|
+
import java.util.UUID;
|
|
55
58
|
|
|
56
59
|
@UnstableApi
|
|
57
60
|
@CapacitorPlugin(
|
|
@@ -84,6 +87,13 @@ public class NativeAudio extends Plugin implements AudioManager.OnAudioFocusChan
|
|
|
84
87
|
private static final int NOTIFICATION_ID = 1001;
|
|
85
88
|
private static final String CHANNEL_ID = "native_audio_channel";
|
|
86
89
|
|
|
90
|
+
// Track playOnce assets for automatic cleanup
|
|
91
|
+
private Set<String> playOnceAssets = new HashSet<>();
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Initializes plugin runtime state by obtaining the system {@link AudioManager}, preparing the asset map,
|
|
95
|
+
* and recording the device's original audio mode without requesting audio focus.
|
|
96
|
+
*/
|
|
87
97
|
@Override
|
|
88
98
|
public void load() {
|
|
89
99
|
super.load();
|
|
@@ -243,6 +253,11 @@ public class NativeAudio extends Plugin implements AudioManager.OnAudioFocusChan
|
|
|
243
253
|
.start();
|
|
244
254
|
}
|
|
245
255
|
|
|
256
|
+
/**
|
|
257
|
+
* Initiates preloading of an audio asset described by the plugin call.
|
|
258
|
+
*
|
|
259
|
+
* @param call the PluginCall containing preload options (for example `assetId`, `assetPath`, `isUrl`, `isComplex`, headers, and optional notification metadata); the call will be resolved or rejected when the preload operation completes.
|
|
260
|
+
*/
|
|
246
261
|
@PluginMethod
|
|
247
262
|
public void preload(final PluginCall call) {
|
|
248
263
|
this.getActivity().runOnUiThread(
|
|
@@ -255,6 +270,192 @@ public class NativeAudio extends Plugin implements AudioManager.OnAudioFocusChan
|
|
|
255
270
|
);
|
|
256
271
|
}
|
|
257
272
|
|
|
273
|
+
/**
|
|
274
|
+
* Play an audio asset a single time and automatically remove its resources when finished.
|
|
275
|
+
*
|
|
276
|
+
* <p>Preloads the specified asset, optionally starts playback immediately, and ensures the
|
|
277
|
+
* asset is unloaded and any associated notification metadata are cleared after completion or on
|
|
278
|
+
* error. Supports local file paths and remote URLs, HLS streams when available, custom HTTP
|
|
279
|
+
* headers for remote requests, and optional deletion of local source files after playback.
|
|
280
|
+
*
|
|
281
|
+
* @param call Capacitor PluginCall containing options:
|
|
282
|
+
* <ul>
|
|
283
|
+
* <li><code>assetPath</code> (required): path or URL to the audio file;</li>
|
|
284
|
+
* <li><code>volume</code> (optional): playback volume (0.1–1.0), default 1.0;</li>
|
|
285
|
+
* <li><code>isUrl</code> (optional): treat <code>assetPath</code> as a URL when true, default false;</li>
|
|
286
|
+
* <li><code>autoPlay</code> (optional): start playback immediately when true, default true;</li>
|
|
287
|
+
* <li><code>deleteAfterPlay</code> (optional): delete the local file after playback when true, default false;</li>
|
|
288
|
+
* <li><code>headers</code> (optional): JS object of HTTP headers for remote requests;</li>
|
|
289
|
+
* <li><code>notificationMetadata</code> (optional): object with <code>title</code>, <code>artist</code>,
|
|
290
|
+
* <code>album</code>, <code>artworkUrl</code> for notification display.</li>
|
|
291
|
+
* </ul>
|
|
292
|
+
*/
|
|
293
|
+
@PluginMethod
|
|
294
|
+
public void playOnce(final PluginCall call) {
|
|
295
|
+
this.getActivity().runOnUiThread(
|
|
296
|
+
new Runnable() {
|
|
297
|
+
/**
|
|
298
|
+
* Preloads a temporary audio asset, optionally plays it one time, and schedules automatic cleanup when playback completes.
|
|
299
|
+
*
|
|
300
|
+
* <p>The method generates a unique temporary assetId, validates options (path, volume, local/remote, headers),
|
|
301
|
+
* loads the asset into the plugin's asset map, registers completion listeners to dispatch the completion event
|
|
302
|
+
* and to unload/remove notification metadata and tracking state, and optionally deletes the source file from
|
|
303
|
+
* safe application directories after playback. If configured, it also updates the media notification and returns
|
|
304
|
+
* the generated `assetId` to the caller.
|
|
305
|
+
*/
|
|
306
|
+
@Override
|
|
307
|
+
public void run() {
|
|
308
|
+
try {
|
|
309
|
+
NativeAudio.this.initSoundPool();
|
|
310
|
+
|
|
311
|
+
// Generate unique temporary asset ID
|
|
312
|
+
final String assetId =
|
|
313
|
+
"playOnce_" + System.currentTimeMillis() + "_" + UUID.randomUUID().toString().substring(0, 8);
|
|
314
|
+
|
|
315
|
+
// Extract options
|
|
316
|
+
String assetPath = call.getString("assetPath");
|
|
317
|
+
if (!NativeAudio.this.isStringValid(assetPath)) {
|
|
318
|
+
call.reject("Asset Path is missing - " + assetPath);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
boolean autoPlay = call.getBoolean("autoPlay", true);
|
|
323
|
+
final boolean deleteAfterPlay = call.getBoolean("deleteAfterPlay", false);
|
|
324
|
+
float volume = call.getFloat(VOLUME, 1F);
|
|
325
|
+
boolean isLocalUrl = call.getBoolean("isUrl", false);
|
|
326
|
+
int audioChannelNum = 1; // Single channel for playOnce
|
|
327
|
+
|
|
328
|
+
// Track this as a playOnce asset
|
|
329
|
+
NativeAudio.this.playOnceAssets.add(assetId);
|
|
330
|
+
|
|
331
|
+
// Store notification metadata if provided
|
|
332
|
+
JSObject metadata = call.getObject(NOTIFICATION_METADATA);
|
|
333
|
+
if (metadata != null) {
|
|
334
|
+
Map<String, String> metadataMap = new HashMap<>();
|
|
335
|
+
if (metadata.has("title")) metadataMap.put("title", metadata.getString("title"));
|
|
336
|
+
if (metadata.has("artist")) metadataMap.put("artist", metadata.getString("artist"));
|
|
337
|
+
if (metadata.has("album")) metadataMap.put("album", metadata.getString("album"));
|
|
338
|
+
if (metadata.has("artworkUrl")) metadataMap.put("artworkUrl", metadata.getString("artworkUrl"));
|
|
339
|
+
if (!metadataMap.isEmpty()) {
|
|
340
|
+
NativeAudio.this.notificationMetadataMap.put(assetId, metadataMap);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// Preload the asset using the helper method
|
|
345
|
+
try {
|
|
346
|
+
// Check if asset already exists
|
|
347
|
+
if (NativeAudio.this.audioAssetList.containsKey(assetId)) {
|
|
348
|
+
call.reject(ERROR_AUDIO_EXISTS + " - " + assetId);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// Load the asset using the helper method
|
|
352
|
+
JSObject headersObj = call.getObject("headers");
|
|
353
|
+
AudioAsset asset = NativeAudio.this.loadAudioAsset(
|
|
354
|
+
assetId,
|
|
355
|
+
assetPath,
|
|
356
|
+
isLocalUrl,
|
|
357
|
+
volume,
|
|
358
|
+
audioChannelNum,
|
|
359
|
+
headersObj
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
// Add to asset list; completion listener is set below with cleanup
|
|
363
|
+
NativeAudio.this.audioAssetList.put(assetId, asset);
|
|
364
|
+
|
|
365
|
+
// Store the file path if we need to delete it later
|
|
366
|
+
// Only delete local file:// URLs, not remote streaming URLs
|
|
367
|
+
final String filePathToDelete = (deleteAfterPlay && assetPath.startsWith("file://")) ? assetPath : null;
|
|
368
|
+
|
|
369
|
+
// Set up completion listener for automatic cleanup
|
|
370
|
+
asset.setCompletionListener(
|
|
371
|
+
new AudioCompletionListener() {
|
|
372
|
+
@Override
|
|
373
|
+
public void onCompletion(String completedAssetId) {
|
|
374
|
+
// Call the original completion dispatcher first
|
|
375
|
+
NativeAudio.this.dispatchComplete(completedAssetId);
|
|
376
|
+
|
|
377
|
+
// Then perform cleanup
|
|
378
|
+
NativeAudio.this.getActivity().runOnUiThread(() -> {
|
|
379
|
+
try {
|
|
380
|
+
// Unload the asset
|
|
381
|
+
AudioAsset assetToUnload = NativeAudio.this.audioAssetList.get(assetId);
|
|
382
|
+
if (assetToUnload != null) {
|
|
383
|
+
assetToUnload.unload();
|
|
384
|
+
NativeAudio.this.audioAssetList.remove(assetId);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Remove from tracking sets
|
|
388
|
+
NativeAudio.this.playOnceAssets.remove(assetId);
|
|
389
|
+
NativeAudio.this.notificationMetadataMap.remove(assetId);
|
|
390
|
+
|
|
391
|
+
// Clear notification if this was the currently playing asset
|
|
392
|
+
if (assetId.equals(NativeAudio.this.currentlyPlayingAssetId)) {
|
|
393
|
+
NativeAudio.this.clearNotification();
|
|
394
|
+
NativeAudio.this.currentlyPlayingAssetId = null;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// Delete file if requested
|
|
398
|
+
if (filePathToDelete != null) {
|
|
399
|
+
try {
|
|
400
|
+
File fileToDelete = new File(URI.create(filePathToDelete));
|
|
401
|
+
if (fileToDelete.exists() && fileToDelete.delete()) {
|
|
402
|
+
Log.d(TAG, "Deleted file after playOnce: " + filePathToDelete);
|
|
403
|
+
}
|
|
404
|
+
} catch (Exception e) {
|
|
405
|
+
Log.e(TAG, "Error deleting file after playOnce: " + filePathToDelete, e);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
} catch (Exception e) {
|
|
409
|
+
Log.e(TAG, "Error during playOnce cleanup: " + e.getMessage());
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
// Auto-play if requested
|
|
417
|
+
if (autoPlay) {
|
|
418
|
+
asset.play(0.0);
|
|
419
|
+
|
|
420
|
+
// Update notification if enabled
|
|
421
|
+
if (showNotification) {
|
|
422
|
+
currentlyPlayingAssetId = assetId;
|
|
423
|
+
updateNotification(assetId);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// Return the generated assetId
|
|
428
|
+
JSObject result = new JSObject();
|
|
429
|
+
result.put(ASSET_ID, assetId);
|
|
430
|
+
call.resolve(result);
|
|
431
|
+
} catch (Exception ex) {
|
|
432
|
+
// Cleanup on failure
|
|
433
|
+
NativeAudio.this.playOnceAssets.remove(assetId);
|
|
434
|
+
NativeAudio.this.notificationMetadataMap.remove(assetId);
|
|
435
|
+
AudioAsset failedAsset = NativeAudio.this.audioAssetList.get(assetId);
|
|
436
|
+
if (failedAsset != null) {
|
|
437
|
+
failedAsset.unload();
|
|
438
|
+
NativeAudio.this.audioAssetList.remove(assetId);
|
|
439
|
+
}
|
|
440
|
+
call.reject("Failed to load asset for playOnce: " + ex.getMessage());
|
|
441
|
+
}
|
|
442
|
+
} catch (Exception ex) {
|
|
443
|
+
call.reject(ex.getMessage());
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Starts playback of a preloaded audio asset on the main (UI) thread.
|
|
452
|
+
*
|
|
453
|
+
* The PluginCall must include:
|
|
454
|
+
* - "assetId" (String): identifier of the preloaded asset to play.
|
|
455
|
+
* - Optional "time" (number): start position in seconds.
|
|
456
|
+
*
|
|
457
|
+
* @param call the PluginCall containing playback parameters
|
|
458
|
+
*/
|
|
258
459
|
@PluginMethod
|
|
259
460
|
public void play(final PluginCall call) {
|
|
260
461
|
this.getActivity().runOnUiThread(
|
|
@@ -581,6 +782,14 @@ public class NativeAudio extends Plugin implements AudioManager.OnAudioFocusChan
|
|
|
581
782
|
notifyListeners("complete", ret);
|
|
582
783
|
}
|
|
583
784
|
|
|
785
|
+
/**
|
|
786
|
+
* Emits a "currentTime" event for the given asset with the playback position rounded to the nearest 0.1 second.
|
|
787
|
+
*
|
|
788
|
+
* The emitted event payload contains `assetId` and `currentTime` (in seconds, rounded to the nearest 0.1).
|
|
789
|
+
*
|
|
790
|
+
* @param assetId the identifier of the audio asset
|
|
791
|
+
* @param currentTime the current playback time in seconds (will be rounded to nearest 0.1)
|
|
792
|
+
*/
|
|
584
793
|
public void notifyCurrentTime(String assetId, double currentTime) {
|
|
585
794
|
// Round to nearest 100ms
|
|
586
795
|
double roundedTime = Math.round(currentTime * 10.0) / 10.0;
|
|
@@ -590,6 +799,133 @@ public class NativeAudio extends Plugin implements AudioManager.OnAudioFocusChan
|
|
|
590
799
|
notifyListeners("currentTime", ret);
|
|
591
800
|
}
|
|
592
801
|
|
|
802
|
+
/**
|
|
803
|
+
* Create an AudioAsset for the given identifier and path, supporting remote URLs (including HLS),
|
|
804
|
+
* local file URIs, and assets in the app's public folder.
|
|
805
|
+
*
|
|
806
|
+
* @param assetId unique identifier for the asset
|
|
807
|
+
* @param assetPath file path or URL to the audio resource
|
|
808
|
+
* @param isLocalUrl true when assetPath is a URL (http/https/file), false when it refers to a public asset path
|
|
809
|
+
* @param volume initial playback volume (expected range: 0.1 to 1.0)
|
|
810
|
+
* @param audioChannelNum number of audio channels to configure for the asset
|
|
811
|
+
* @param headersObj optional HTTP headers for remote requests (may be null)
|
|
812
|
+
* @return an initialized AudioAsset instance for the provided path
|
|
813
|
+
* @throws Exception if the asset cannot be located or initialized (includes missing file, invalid path, or other load errors)
|
|
814
|
+
*/
|
|
815
|
+
private AudioAsset loadAudioAsset(
|
|
816
|
+
String assetId,
|
|
817
|
+
String assetPath,
|
|
818
|
+
boolean isLocalUrl,
|
|
819
|
+
float volume,
|
|
820
|
+
int audioChannelNum,
|
|
821
|
+
JSObject headersObj
|
|
822
|
+
) throws Exception {
|
|
823
|
+
if (isLocalUrl) {
|
|
824
|
+
Uri uri = Uri.parse(assetPath);
|
|
825
|
+
if (uri.getScheme() != null && (uri.getScheme().equals("http") || uri.getScheme().equals("https"))) {
|
|
826
|
+
// Remote URL
|
|
827
|
+
Map<String, String> requestHeaders = null;
|
|
828
|
+
if (headersObj != null) {
|
|
829
|
+
requestHeaders = new HashMap<>();
|
|
830
|
+
for (Iterator<String> it = headersObj.keys(); it.hasNext(); ) {
|
|
831
|
+
String key = it.next();
|
|
832
|
+
try {
|
|
833
|
+
String value = headersObj.getString(key);
|
|
834
|
+
if (value != null) {
|
|
835
|
+
requestHeaders.put(key, value);
|
|
836
|
+
}
|
|
837
|
+
} catch (Exception e) {
|
|
838
|
+
Log.w("AudioPlugin", "Skipping non-string header: " + key);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
if (assetPath.endsWith(".m3u8")) {
|
|
844
|
+
// HLS Stream - check if HLS support is available
|
|
845
|
+
if (!HlsAvailabilityChecker.isHlsAvailable()) {
|
|
846
|
+
throw new Exception(
|
|
847
|
+
"HLS streaming (.m3u8) is not available. " + "Set 'hls: true' in capacitor.config.ts and run 'npx cap sync'."
|
|
848
|
+
);
|
|
849
|
+
}
|
|
850
|
+
AudioAsset streamAudioAsset = createStreamAudioAsset(assetId, uri, volume, requestHeaders);
|
|
851
|
+
if (streamAudioAsset == null) {
|
|
852
|
+
throw new Exception("Failed to create HLS stream player. HLS may not be configured.");
|
|
853
|
+
}
|
|
854
|
+
return streamAudioAsset;
|
|
855
|
+
} else {
|
|
856
|
+
RemoteAudioAsset remoteAudioAsset = new RemoteAudioAsset(this, assetId, uri, audioChannelNum, volume, requestHeaders);
|
|
857
|
+
return remoteAudioAsset;
|
|
858
|
+
}
|
|
859
|
+
} else if (uri.getScheme() != null && uri.getScheme().equals("file")) {
|
|
860
|
+
File file = new File(uri.getPath());
|
|
861
|
+
if (!file.exists()) {
|
|
862
|
+
throw new Exception(ERROR_ASSET_PATH_MISSING + " - " + assetPath);
|
|
863
|
+
}
|
|
864
|
+
ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
|
|
865
|
+
AssetFileDescriptor afd = new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH);
|
|
866
|
+
AudioAsset asset = new AudioAsset(this, assetId, afd, audioChannelNum, volume);
|
|
867
|
+
return asset;
|
|
868
|
+
} else {
|
|
869
|
+
// Handle unexpected URI schemes by attempting to treat as local file
|
|
870
|
+
try {
|
|
871
|
+
File file = new File(uri.getPath());
|
|
872
|
+
if (!file.exists()) {
|
|
873
|
+
throw new Exception(ERROR_ASSET_PATH_MISSING + " - " + assetPath);
|
|
874
|
+
}
|
|
875
|
+
ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
|
|
876
|
+
AssetFileDescriptor afd = new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH);
|
|
877
|
+
AudioAsset asset = new AudioAsset(this, assetId, afd, audioChannelNum, volume);
|
|
878
|
+
Log.w(TAG, "Unexpected URI scheme '" + uri.getScheme() + "' treated as local file: " + assetPath);
|
|
879
|
+
return asset;
|
|
880
|
+
} catch (Exception e) {
|
|
881
|
+
throw new Exception(
|
|
882
|
+
"Failed to load asset with unexpected URI scheme '" +
|
|
883
|
+
uri.getScheme() +
|
|
884
|
+
"' (expected 'http', 'https', or 'file'). Asset path: " +
|
|
885
|
+
assetPath +
|
|
886
|
+
". Error: " +
|
|
887
|
+
e.getMessage()
|
|
888
|
+
);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
} else {
|
|
892
|
+
// Handle asset in public folder
|
|
893
|
+
String finalAssetPath = assetPath;
|
|
894
|
+
if (!assetPath.startsWith("public/")) {
|
|
895
|
+
finalAssetPath = "public/" + assetPath;
|
|
896
|
+
}
|
|
897
|
+
Context ctx = getContext().getApplicationContext();
|
|
898
|
+
AssetManager am = ctx.getResources().getAssets();
|
|
899
|
+
AssetFileDescriptor assetFileDescriptor = am.openFd(finalAssetPath);
|
|
900
|
+
AudioAsset asset = new AudioAsset(this, assetId, assetFileDescriptor, audioChannelNum, volume);
|
|
901
|
+
return asset;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/**
|
|
906
|
+
* Preloads an audio asset into the plugin's asset list.
|
|
907
|
+
*
|
|
908
|
+
* <p>The provided PluginCall must include:
|
|
909
|
+
* <ul>
|
|
910
|
+
* <li>`assetId` (string) — identifier for the asset</li>
|
|
911
|
+
* <li>`assetPath` (string) — path or URL to the audio resource</li>
|
|
912
|
+
* </ul>
|
|
913
|
+
* Optional keys on the call:
|
|
914
|
+
* <ul>
|
|
915
|
+
* <li>`isUrl` (boolean) — true when `assetPath` is a remote URL</li>
|
|
916
|
+
* <li>`isComplex` (boolean) — when true, `volume` and `audioChannelNum` may be provided</li>
|
|
917
|
+
* <li>`volume` (number) — initial playback volume (default 1.0)</li>
|
|
918
|
+
* <li>`audioChannelNum` (int) — audio channel count (default 1)</li>
|
|
919
|
+
* <li>`headers` (object) — HTTP headers for remote requests</li>
|
|
920
|
+
* <li>`notificationMetadata` (object) — optional metadata (`title`, `artist`, `album`, `artworkUrl`) to attach to the asset</li>
|
|
921
|
+
* </ul>
|
|
922
|
+
*
|
|
923
|
+
* <p>On success the call is resolved with a status indicating success. The method rejects the call
|
|
924
|
+
* when required parameters are missing, when an asset with the same id already exists, or when
|
|
925
|
+
* the asset cannot be loaded.
|
|
926
|
+
*
|
|
927
|
+
* @param call the PluginCall containing asset parameters and options
|
|
928
|
+
*/
|
|
593
929
|
private void preloadAsset(PluginCall call) {
|
|
594
930
|
float volume = 1F;
|
|
595
931
|
int audioChannelNum = 1;
|
|
@@ -639,105 +975,19 @@ public class NativeAudio extends Plugin implements AudioManager.OnAudioFocusChan
|
|
|
639
975
|
}
|
|
640
976
|
}
|
|
641
977
|
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
if (uri.getScheme() != null && (uri.getScheme().equals("http") || uri.getScheme().equals("https"))) {
|
|
646
|
-
// Remote URL
|
|
647
|
-
Log.d("AudioPlugin", "Debug: Remote URL detected: " + uri.toString());
|
|
648
|
-
|
|
649
|
-
// Extract headers if provided
|
|
650
|
-
Map<String, String> requestHeaders = null;
|
|
651
|
-
JSObject headersObj = call.getObject("headers");
|
|
652
|
-
if (headersObj != null) {
|
|
653
|
-
requestHeaders = new HashMap<>();
|
|
654
|
-
for (Iterator<String> it = headersObj.keys(); it.hasNext(); ) {
|
|
655
|
-
String key = it.next();
|
|
656
|
-
try {
|
|
657
|
-
String value = headersObj.getString(key);
|
|
658
|
-
if (value != null) {
|
|
659
|
-
requestHeaders.put(key, value);
|
|
660
|
-
}
|
|
661
|
-
} catch (Exception e) {
|
|
662
|
-
Log.w("AudioPlugin", "Skipping non-string header: " + key);
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
}
|
|
978
|
+
// Use the helper method to load the asset
|
|
979
|
+
JSObject headersObj = call.getObject("headers");
|
|
980
|
+
AudioAsset asset = loadAudioAsset(audioId, assetPath, isLocalUrl, volume, audioChannelNum, headersObj);
|
|
666
981
|
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
call.reject(
|
|
671
|
-
"HLS streaming (.m3u8) is not available. " +
|
|
672
|
-
"The media3-exoplayer-hls dependency is not included. " +
|
|
673
|
-
"To enable HLS support, set 'hls: true' in capacitor.config.ts under NativeAudio plugin config " +
|
|
674
|
-
"and run 'npx cap sync'. This will increase APK size by ~4MB."
|
|
675
|
-
);
|
|
676
|
-
return;
|
|
677
|
-
}
|
|
678
|
-
// HLS Stream - create via reflection to allow compile-time exclusion
|
|
679
|
-
AudioAsset streamAudioAsset = createStreamAudioAsset(audioId, uri, volume, requestHeaders);
|
|
680
|
-
if (streamAudioAsset == null) {
|
|
681
|
-
call.reject("Failed to create HLS stream player. HLS support may not be properly configured.");
|
|
682
|
-
return;
|
|
683
|
-
}
|
|
684
|
-
audioAssetList.put(audioId, streamAudioAsset);
|
|
685
|
-
call.resolve(status);
|
|
686
|
-
} else {
|
|
687
|
-
// Regular remote audio
|
|
688
|
-
RemoteAudioAsset remoteAudioAsset = new RemoteAudioAsset(
|
|
689
|
-
this,
|
|
690
|
-
audioId,
|
|
691
|
-
uri,
|
|
692
|
-
audioChannelNum,
|
|
693
|
-
volume,
|
|
694
|
-
requestHeaders
|
|
695
|
-
);
|
|
696
|
-
remoteAudioAsset.setCompletionListener(this::dispatchComplete);
|
|
697
|
-
audioAssetList.put(audioId, remoteAudioAsset);
|
|
698
|
-
call.resolve(status);
|
|
699
|
-
}
|
|
700
|
-
} else if (uri.getScheme() != null && uri.getScheme().equals("file")) {
|
|
701
|
-
// Local file URL
|
|
702
|
-
Log.d("AudioPlugin", "Debug: Local file URL detected");
|
|
703
|
-
File file = new File(uri.getPath());
|
|
704
|
-
if (!file.exists()) {
|
|
705
|
-
Log.e("AudioPlugin", "Error: File does not exist - " + file.getAbsolutePath());
|
|
706
|
-
call.reject(ERROR_ASSET_PATH_MISSING + " - " + assetPath);
|
|
707
|
-
return;
|
|
708
|
-
}
|
|
709
|
-
ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
|
|
710
|
-
AssetFileDescriptor afd = new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH);
|
|
711
|
-
AudioAsset asset = new AudioAsset(this, audioId, afd, audioChannelNum, volume);
|
|
712
|
-
asset.setCompletionListener(this::dispatchComplete);
|
|
713
|
-
audioAssetList.put(audioId, asset);
|
|
714
|
-
call.resolve(status);
|
|
715
|
-
} else {
|
|
716
|
-
throw new IllegalArgumentException("Invalid URL scheme: " + uri.getScheme());
|
|
717
|
-
}
|
|
718
|
-
} catch (Exception e) {
|
|
719
|
-
Log.e("AudioPlugin", "Error handling URL", e);
|
|
720
|
-
call.reject("Error handling URL: " + e.getMessage());
|
|
721
|
-
}
|
|
722
|
-
} else {
|
|
723
|
-
// Handle asset in public folder
|
|
724
|
-
Log.d("AudioPlugin", "Debug: Handling asset in public folder");
|
|
725
|
-
if (!assetPath.startsWith("public/")) {
|
|
726
|
-
assetPath = "public/" + assetPath;
|
|
727
|
-
}
|
|
728
|
-
try {
|
|
729
|
-
Context ctx = getContext().getApplicationContext();
|
|
730
|
-
AssetManager am = ctx.getResources().getAssets();
|
|
731
|
-
AssetFileDescriptor assetFileDescriptor = am.openFd(assetPath);
|
|
732
|
-
AudioAsset asset = new AudioAsset(this, audioId, assetFileDescriptor, audioChannelNum, volume);
|
|
733
|
-
asset.setCompletionListener(this::dispatchComplete);
|
|
734
|
-
audioAssetList.put(audioId, asset);
|
|
735
|
-
call.resolve(status);
|
|
736
|
-
} catch (IOException e) {
|
|
737
|
-
Log.e("AudioPlugin", "Error opening asset: " + assetPath, e);
|
|
738
|
-
call.reject(ERROR_ASSET_PATH_MISSING + " - " + assetPath);
|
|
739
|
-
}
|
|
982
|
+
if (asset == null) {
|
|
983
|
+
call.reject("Failed to load asset");
|
|
984
|
+
return;
|
|
740
985
|
}
|
|
986
|
+
|
|
987
|
+
// Set completion listener and add to asset list
|
|
988
|
+
asset.setCompletionListener(this::dispatchComplete);
|
|
989
|
+
audioAssetList.put(audioId, asset);
|
|
990
|
+
call.resolve(status);
|
|
741
991
|
} catch (Exception ex) {
|
|
742
992
|
Log.e("AudioPlugin", "Error in preloadAsset", ex);
|
|
743
993
|
call.reject("Error in preloadAsset: " + ex.getMessage());
|