@octane-xplat/audio 0.17.1 → 0.19.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 +56 -8
- package/package.json +102 -98
- package/platforms/android/audio.aar +0 -0
- package/platforms/android/java/com/xplat/audio/AudioBridge.java +16 -8
- package/platforms/android/java/com/xplat/audio/AudioPlaybackService.java +63 -10
- package/platforms/macos/include/XplatAudioPlayer.h +3 -0
- package/platforms/macos/src/XplatAudioPlayer.m +27 -1
- package/src/audio.android.ts +51 -4
- package/src/audio.ios.ts +102 -4
- package/src/audio.macos.ts +96 -6
- package/src/audio.ts +1 -0
- package/src/audio.web.ts +135 -21
- package/src/audio.windows.ts +7 -0
- package/src/queue.ts +13 -0
- package/src/settings.ts +7 -0
- package/src/types.ts +64 -2
- package/types/generated/.tsrx-typegen-manifest.json +17 -0
- package/types/generated/audio.android.d.ts +3 -0
- package/types/generated/audio.d.ts +3 -0
- package/types/generated/audio.ios.d.ts +3 -0
- package/types/generated/audio.macos.d.ts +4 -0
- package/types/generated/audio.web.d.ts +3 -0
- package/types/generated/audio.windows.d.ts +4 -0
- package/types/generated/index.d.ts +2 -0
- package/types/generated/index.macos.d.ts +2 -0
- package/types/generated/index.web.d.ts +2 -0
- package/types/generated/queue.d.ts +3 -0
- package/types/generated/settings.d.ts +2 -0
- package/types/generated/types.d.ts +90 -0
- package/LICENSE +0 -21
- package/types/index.d.ts +0 -1
- package/types/types.d.ts +0 -36
package/README.md
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
# `@octane-xplat/audio`
|
|
2
2
|
|
|
3
|
-
```sh
|
|
4
|
-
pnpm add @octane-xplat/audio
|
|
5
|
-
```
|
|
6
|
-
|
|
7
3
|
Long-form audio playback for Octane xplat apps — a queue player, not a
|
|
8
4
|
sound-effect bank. iOS uses `AVPlayer`/`AVAudioSession` (interruption
|
|
9
5
|
handling included) and Android uses a package-owned Media3 playback service.
|
|
10
6
|
macOS uses a package-owned AVPlayer bridge. Web uses an
|
|
11
7
|
`HTMLAudioElement` plus the Media Session API for system controls.
|
|
12
8
|
|
|
9
|
+
```sh
|
|
10
|
+
bun add @octane-xplat/audio
|
|
11
|
+
```
|
|
12
|
+
|
|
13
13
|
```ts
|
|
14
14
|
import { createAudioPlayer } from '@octane-xplat/audio'
|
|
15
15
|
|
|
16
16
|
const player = createAudioPlayer()
|
|
17
17
|
await player.setQueue([{ id: 'ep1', source: 'https://example.com/ep1.mp3' }])
|
|
18
|
+
await player.setPlaybackRate(1.5)
|
|
19
|
+
await player.setVolume(0.4)
|
|
20
|
+
await player.setMuted(false)
|
|
18
21
|
await player.play()
|
|
19
22
|
|
|
20
23
|
const off = player.subscribe((snapshot) => {
|
|
@@ -61,9 +64,54 @@ export async function playTrack(source: string) {
|
|
|
61
64
|
}
|
|
62
65
|
```
|
|
63
66
|
|
|
64
|
-
For short UI sounds use [`@octane-xplat/sounds`](
|
|
67
|
+
For short UI sounds use [`@octane-xplat/sounds`](https://github.com/octane-xplat/octane-xplat/blob/main/packages/sounds/README.md).
|
|
68
|
+
|
|
69
|
+
Native Windows selects an unavailable leaf without loading mobile backends.
|
|
70
|
+
Creating a player throws; this import boundary does not provide Windows playback.
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { createAudioPlayer } from '@octane-xplat/audio'
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const candidate = createAudioPlayer()
|
|
77
|
+
candidate.dispose()
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.log('Audio unavailable:', String(error))
|
|
80
|
+
}
|
|
81
|
+
```
|
|
65
82
|
|
|
66
|
-
Guide: [Media services](
|
|
67
|
-
availability: [platform notes](
|
|
83
|
+
Guide: [Media services](https://octane-xplat.goddardai.org/media-services); per-target
|
|
84
|
+
availability: [platform notes](https://github.com/octane-xplat/octane-xplat/blob/main/.agents/docs/notes/platform-notes.md). Exercised by
|
|
68
85
|
the harness `MediaServices` screen
|
|
69
|
-
([`packages/app/src/MediaServices.tsrx`](
|
|
86
|
+
([`packages/app/src/MediaServices.tsrx`](https://github.com/octane-xplat/octane-xplat/blob/main/packages/app/src/MediaServices.tsrx)).
|
|
87
|
+
|
|
88
|
+
## Foreground verification
|
|
89
|
+
|
|
90
|
+
The maintained silent-WAV fixture measures observable player state against
|
|
91
|
+
fixed budgets. Run it on an owned build host and iOS simulator (or Android
|
|
92
|
+
emulator with `--target android`). Its local HTTP addresses target virtual
|
|
93
|
+
devices; physical media acceptance needs a reachable server and a separate run:
|
|
94
|
+
|
|
95
|
+
```sh
|
|
96
|
+
bun run turbo run test:browser --filter=@octane-xplat/audio -- --foreground-budgets
|
|
97
|
+
bun run turbo run test:runtime --filter=@octane-xplat/audio -- --foreground-budgets --target ios --device OWNED_UDID
|
|
98
|
+
bun run turbo run test:macos-budgets --filter=@octane-xplat/audio
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
| Measurement | Fixture ceiling |
|
|
102
|
+
| --- | --- |
|
|
103
|
+
| First observed progress after loading | 10 seconds |
|
|
104
|
+
| Preserving queue update | 250 milliseconds |
|
|
105
|
+
| Position lost during that update | 0.15 seconds |
|
|
106
|
+
| Observable seek position | 1.5 seconds |
|
|
107
|
+
| Estimated extra boundary delay | 1.5 seconds |
|
|
108
|
+
| Appended waiting queue resumes | 2 seconds |
|
|
109
|
+
| Sampled AppKit host peak RSS | 384 MiB |
|
|
110
|
+
|
|
111
|
+
These are initial fixture ceilings, not application performance guarantees.
|
|
112
|
+
Boundary timing comes from media positions and polling; it does not measure
|
|
113
|
+
an audible gap. RSS samples cover the owned AppKit process, excluding GPU and
|
|
114
|
+
other processes. Startup, leaks, durable progress, background execution and
|
|
115
|
+
physical output need separate acceptance. The
|
|
116
|
+
[packed listening/settings check](https://github.com/octane-xplat/octane-xplat/blob/main/tools/native-readiness/README.md)
|
|
117
|
+
exercises these services together in an external mobile consumer.
|
package/package.json
CHANGED
|
@@ -1,99 +1,103 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
2
|
+
"name": "@octane-xplat/audio",
|
|
3
|
+
"version": "0.19.0",
|
|
4
|
+
"description": "Long-form audio playback for Octane xplat",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"android",
|
|
7
|
+
"audio",
|
|
8
|
+
"audio-player",
|
|
9
|
+
"cross-platform",
|
|
10
|
+
"ios",
|
|
11
|
+
"music",
|
|
12
|
+
"nativescript",
|
|
13
|
+
"octane",
|
|
14
|
+
"playback",
|
|
15
|
+
"web",
|
|
16
|
+
"xplat"
|
|
17
|
+
],
|
|
18
|
+
"homepage": "https://github.com/octane-xplat/octane-xplat/tree/main/packages/audio",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/octane-xplat/octane-xplat",
|
|
23
|
+
"directory": "packages/audio"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"types/generated",
|
|
27
|
+
"src",
|
|
28
|
+
"platforms",
|
|
29
|
+
"!src/**/*.test.*",
|
|
30
|
+
"!src/**/*.spec.*"
|
|
31
|
+
],
|
|
32
|
+
"type": "module",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./types/generated/index.d.ts",
|
|
36
|
+
"macos": "./src/index.macos.ts",
|
|
37
|
+
"web": "./src/index.web.ts",
|
|
38
|
+
"native": "./src/index.ts",
|
|
39
|
+
"default": "./src/index.ts"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"test": "node --test tests/*.test.mjs",
|
|
47
|
+
"typegen": "tsrx-typegen --target audio",
|
|
48
|
+
"prepack": "bun run typegen && tsrx-typegen --pack-check",
|
|
49
|
+
"test:packed": "node tests/packed-consumer.mjs",
|
|
50
|
+
"pack:check": "tsrx-typegen --pack-check",
|
|
51
|
+
"test:macos-runtime": "node tests/packed-consumer.mjs --macos-runtime",
|
|
52
|
+
"typecheck": "node ../../scripts/typecheck.ts",
|
|
53
|
+
"test:runtime": "node tests/runtime.mjs",
|
|
54
|
+
"test:browser": "node tests/browser.mjs",
|
|
55
|
+
"test:macos-budgets": "node tests/profile-macos.mjs"
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"@octane-xplat/platform": "0.19.0"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@nativescript-community/octane": "0.2.5",
|
|
62
|
+
"@nativescript/core": "9.1.3",
|
|
63
|
+
"@octanejs/vite-plugin": "0.5.0",
|
|
64
|
+
"octane": "0.16.3",
|
|
65
|
+
"tsrx-typegen": "0.5.0",
|
|
66
|
+
"typescript": "5.9.3",
|
|
67
|
+
"vite": "8.3.0",
|
|
68
|
+
"playwright": "1.63.0",
|
|
69
|
+
"@tsrx/typescript-plugin": "0.4.11"
|
|
70
|
+
},
|
|
71
|
+
"peerDependencies": {
|
|
72
|
+
"@nativescript-community/octane": ">=0.2.1 <1",
|
|
73
|
+
"@nativescript/core": ">=9.1.0 <10",
|
|
74
|
+
"octane": ">=0.6.3 <1"
|
|
75
|
+
},
|
|
76
|
+
"peerDependenciesMeta": {
|
|
77
|
+
"@nativescript-community/octane": {
|
|
78
|
+
"optional": true
|
|
79
|
+
},
|
|
80
|
+
"@nativescript/core": {
|
|
81
|
+
"optional": true
|
|
82
|
+
},
|
|
83
|
+
"octane": {
|
|
84
|
+
"optional": true
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
"nativescript": {
|
|
88
|
+
"platforms": {
|
|
89
|
+
"android": "9.1.0",
|
|
90
|
+
"ios": "9.1.0"
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
"xplat": {
|
|
94
|
+
"macos": {
|
|
95
|
+
"frameworks": [
|
|
96
|
+
"Foundation",
|
|
97
|
+
"AVFoundation",
|
|
98
|
+
"CoreMedia"
|
|
99
|
+
]
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
"types": "./types/generated/index.d.ts"
|
|
103
|
+
}
|
|
Binary file
|
|
@@ -6,7 +6,11 @@ import android.util.Log;
|
|
|
6
6
|
|
|
7
7
|
/** Commands cross the JS/service boundary as intents so they remain ordered. */
|
|
8
8
|
public final class AudioBridge {
|
|
9
|
+
public static final String ACTION_UPDATE = "com.xplat.audio.UPDATE";
|
|
9
10
|
public static final String ACTION_QUEUE = "com.xplat.audio.QUEUE";
|
|
11
|
+
public static final String ACTION_RATE = "com.xplat.audio.RATE";
|
|
12
|
+
public static final String ACTION_VOLUME = "com.xplat.audio.VOLUME";
|
|
13
|
+
public static final String ACTION_MUTED = "com.xplat.audio.MUTED";
|
|
10
14
|
public static final String ACTION_PLAY = "com.xplat.audio.PLAY";
|
|
11
15
|
public static final String ACTION_PAUSE = "com.xplat.audio.PAUSE";
|
|
12
16
|
public static final String ACTION_SEEK = "com.xplat.audio.SEEK";
|
|
@@ -17,7 +21,7 @@ public final class AudioBridge {
|
|
|
17
21
|
// app cannot observe or forge.
|
|
18
22
|
private static final String COMMAND_TOKEN = java.util.UUID.randomUUID().toString();
|
|
19
23
|
private static volatile String snapshot = "{\"state\":\"idle\",\"currentTime\":0,\"duration\":0}";
|
|
20
|
-
private static
|
|
24
|
+
private static final java.util.concurrent.ConcurrentHashMap<String, String> pendingQueues = new java.util.concurrent.ConcurrentHashMap<>();
|
|
21
25
|
|
|
22
26
|
private AudioBridge() {}
|
|
23
27
|
|
|
@@ -27,7 +31,12 @@ public final class AudioBridge {
|
|
|
27
31
|
intent.putExtra("commandToken", COMMAND_TOKEN);
|
|
28
32
|
// The queue JSON can exceed the ~1MB Binder extra limit — hand it to
|
|
29
33
|
// the service in-process and keep the intent small.
|
|
30
|
-
|
|
34
|
+
String queueKey = null;
|
|
35
|
+
if (payload != null && (ACTION_QUEUE.equals(action) || ACTION_UPDATE.equals(action))) {
|
|
36
|
+
queueKey = java.util.UUID.randomUUID().toString();
|
|
37
|
+
pendingQueues.put(queueKey, payload);
|
|
38
|
+
intent.putExtra("queueKey", queueKey);
|
|
39
|
+
}
|
|
31
40
|
intent.putExtra("index", index);
|
|
32
41
|
intent.putExtra("seconds", seconds);
|
|
33
42
|
try {
|
|
@@ -36,7 +45,7 @@ public final class AudioBridge {
|
|
|
36
45
|
// A JS-initiated command while the app is backgrounded throws
|
|
37
46
|
// IllegalStateException — surface it through snapshot.error and
|
|
38
47
|
// rethrow so the caller's promise rejects.
|
|
39
|
-
|
|
48
|
+
if (queueKey != null) pendingQueues.remove(queueKey);
|
|
40
49
|
Log.e(TAG, "Audio command dispatch failed", failure);
|
|
41
50
|
publish("{\"state\":\"error\",\"currentTime\":0,\"duration\":0,\"error\":\"audio command dispatch failed\"}");
|
|
42
51
|
throw failure;
|
|
@@ -47,12 +56,11 @@ public final class AudioBridge {
|
|
|
47
56
|
return intent != null && COMMAND_TOKEN.equals(intent.getStringExtra("commandToken"));
|
|
48
57
|
}
|
|
49
58
|
|
|
50
|
-
static String takeQueue() {
|
|
51
|
-
String
|
|
52
|
-
|
|
53
|
-
return queue;
|
|
59
|
+
static String takeQueue(Intent intent) {
|
|
60
|
+
String key = intent.getStringExtra("queueKey");
|
|
61
|
+
return key == null ? null : pendingQueues.remove(key);
|
|
54
62
|
}
|
|
55
63
|
|
|
56
64
|
public static void publish(String value) { snapshot = value; }
|
|
57
|
-
public static String snapshot() { return snapshot; }
|
|
65
|
+
public static String snapshot() { AudioPlaybackService.refreshSnapshot(); return snapshot; }
|
|
58
66
|
}
|
|
@@ -20,13 +20,18 @@ import org.json.JSONObject;
|
|
|
20
20
|
|
|
21
21
|
public final class AudioPlaybackService extends MediaSessionService {
|
|
22
22
|
private static final String TAG = "XplatAudio";
|
|
23
|
+
private static AudioPlaybackService active;
|
|
23
24
|
private ExoPlayer player;
|
|
24
25
|
private MediaSession session;
|
|
25
26
|
private Player.Listener listener;
|
|
26
27
|
private String error;
|
|
28
|
+
private float volume = 1;
|
|
29
|
+
private boolean muted;
|
|
30
|
+
private boolean queueComplete = true;
|
|
27
31
|
|
|
28
32
|
@Override public void onCreate() {
|
|
29
33
|
super.onCreate();
|
|
34
|
+
active = this;
|
|
30
35
|
player = new ExoPlayer.Builder(this).build();
|
|
31
36
|
player.setAudioAttributes(AudioAttributes.DEFAULT, true);
|
|
32
37
|
listener = new Player.Listener() {
|
|
@@ -56,7 +61,21 @@ public final class AudioPlaybackService extends MediaSessionService {
|
|
|
56
61
|
try {
|
|
57
62
|
switch (intent.getAction()) {
|
|
58
63
|
case AudioBridge.ACTION_QUEUE:
|
|
59
|
-
setQueue(AudioBridge.takeQueue(), intent.getIntExtra("index", 0));
|
|
64
|
+
setQueue(AudioBridge.takeQueue(intent), intent.getIntExtra("index", 0), false);
|
|
65
|
+
break;
|
|
66
|
+
case AudioBridge.ACTION_UPDATE:
|
|
67
|
+
setQueue(AudioBridge.takeQueue(intent), 0, true);
|
|
68
|
+
break;
|
|
69
|
+
case AudioBridge.ACTION_RATE:
|
|
70
|
+
player.setPlaybackParameters(new androidx.media3.common.PlaybackParameters((float) intent.getDoubleExtra("seconds", 1), 1));
|
|
71
|
+
break;
|
|
72
|
+
case AudioBridge.ACTION_VOLUME:
|
|
73
|
+
volume = (float) intent.getDoubleExtra("seconds", 1);
|
|
74
|
+
player.setVolume(muted ? 0 : volume);
|
|
75
|
+
break;
|
|
76
|
+
case AudioBridge.ACTION_MUTED:
|
|
77
|
+
muted = intent.getDoubleExtra("seconds", 0) != 0;
|
|
78
|
+
player.setVolume(muted ? 0 : volume);
|
|
60
79
|
break;
|
|
61
80
|
case AudioBridge.ACTION_PLAY: player.play(); break;
|
|
62
81
|
case AudioBridge.ACTION_PAUSE: player.pause(); break;
|
|
@@ -75,32 +94,65 @@ public final class AudioPlaybackService extends MediaSessionService {
|
|
|
75
94
|
return result;
|
|
76
95
|
}
|
|
77
96
|
|
|
78
|
-
private void setQueue(String encoded, int startAt) throws Exception {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
97
|
+
private void setQueue(String encoded, int startAt, boolean update) throws Exception {
|
|
98
|
+
if (encoded == null) throw new IllegalStateException("Missing audio queue command");
|
|
99
|
+
JSONObject payload = new JSONObject(encoded);
|
|
100
|
+
JSONArray queue = payload.getJSONArray("tracks");
|
|
101
|
+
java.util.List<MediaItem> items = new java.util.ArrayList<>();
|
|
82
102
|
for (int i = 0; i < queue.length(); i++) {
|
|
83
103
|
JSONObject track = queue.getJSONObject(i);
|
|
84
104
|
String source = track.getString("source");
|
|
85
105
|
MediaMetadata metadata = new MediaMetadata.Builder()
|
|
86
|
-
.setTitle(track.optString("title", track.
|
|
106
|
+
.setTitle(track.optString("title", track.getString("id")))
|
|
87
107
|
.setArtist(track.optString("artist", ""))
|
|
88
108
|
.setAlbumTitle(track.optString("album", ""))
|
|
89
109
|
.build();
|
|
90
110
|
Uri uri = source.startsWith("/") ? Uri.fromFile(new File(source)) : Uri.parse(source);
|
|
91
|
-
|
|
111
|
+
items.add(new MediaItem.Builder().setMediaId(track.getString("id")).setUri(uri).setMediaMetadata(metadata).build());
|
|
112
|
+
}
|
|
113
|
+
MediaItem current = player.getCurrentMediaItem();
|
|
114
|
+
int retained = -1;
|
|
115
|
+
if (update && current != null) {
|
|
116
|
+
for (int i = 0; i < items.size(); i++) {
|
|
117
|
+
MediaItem item = items.get(i);
|
|
118
|
+
if (item.mediaId.equals(current.mediaId) && item.localConfiguration.uri.equals(current.localConfiguration.uri)) retained = i;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
boolean waiting = !queueComplete && player.getPlaybackState() == Player.STATE_ENDED;
|
|
122
|
+
queueComplete = payload.optBoolean("complete", true);
|
|
123
|
+
error = null;
|
|
124
|
+
if (retained >= 0) {
|
|
125
|
+
// Edit around the current native MediaItem; never replace its source.
|
|
126
|
+
int oldIndex = player.getCurrentMediaItemIndex();
|
|
127
|
+
player.removeMediaItems(oldIndex + 1, player.getMediaItemCount());
|
|
128
|
+
player.removeMediaItems(0, oldIndex);
|
|
129
|
+
player.addMediaItems(0, items.subList(0, retained));
|
|
130
|
+
player.addMediaItems(items.subList(retained + 1, items.size()));
|
|
131
|
+
player.replaceMediaItem(retained, current.buildUpon().setMediaMetadata(items.get(retained).mediaMetadata).build());
|
|
132
|
+
if (waiting && player.getPlayWhenReady() && retained + 1 < items.size()) {
|
|
133
|
+
player.seekTo(retained + 1, 0);
|
|
134
|
+
player.prepare();
|
|
135
|
+
}
|
|
136
|
+
} else {
|
|
137
|
+
player.pause();
|
|
138
|
+
player.setMediaItems(items, items.isEmpty() ? 0 : Math.max(0, Math.min(startAt, items.size() - 1)), 0);
|
|
139
|
+
player.prepare();
|
|
92
140
|
}
|
|
93
|
-
if (queue.length() > 0) player.seekTo(Math.max(0, Math.min(startAt, queue.length() - 1)), 0);
|
|
94
|
-
player.prepare();
|
|
95
141
|
publishSnapshot();
|
|
96
142
|
}
|
|
97
143
|
|
|
144
|
+
static void refreshSnapshot() {
|
|
145
|
+
AudioPlaybackService service = active;
|
|
146
|
+
if (service != null && android.os.Looper.myLooper() == android.os.Looper.getMainLooper()) service.publishSnapshot();
|
|
147
|
+
}
|
|
148
|
+
|
|
98
149
|
private void publishSnapshot() {
|
|
99
150
|
try {
|
|
100
151
|
JSONObject value = new JSONObject();
|
|
101
152
|
int index = player == null ? -1 : player.getCurrentMediaItemIndex();
|
|
102
|
-
value.put("state", error != null ? "error" : player == null || player.getMediaItemCount() == 0 ? "idle" : player.getPlaybackState() == Player.STATE_ENDED ? "ended" : player.isPlaying() ? "playing" : player.getPlaybackState() == Player.STATE_BUFFERING ? "loading" : "paused");
|
|
153
|
+
value.put("state", error != null ? "error" : player == null || player.getMediaItemCount() == 0 ? "idle" : player.getPlaybackState() == Player.STATE_ENDED ? (queueComplete ? "ended" : player.getPlayWhenReady() ? "waiting" : "paused") : player.isPlaying() ? "playing" : player.getPlaybackState() == Player.STATE_BUFFERING ? "loading" : "paused");
|
|
103
154
|
value.put("index", index);
|
|
155
|
+
if (player.getCurrentMediaItem() != null) value.put("id", player.getCurrentMediaItem().mediaId);
|
|
104
156
|
value.put("currentTime", player == null ? 0 : Math.max(0, player.getCurrentPosition()) / 1000.0);
|
|
105
157
|
value.put("duration", player == null || player.getDuration() < 0 ? 0 : player.getDuration() / 1000.0);
|
|
106
158
|
if (error != null) value.put("error", error);
|
|
@@ -109,6 +161,7 @@ public final class AudioPlaybackService extends MediaSessionService {
|
|
|
109
161
|
}
|
|
110
162
|
|
|
111
163
|
@Override public void onDestroy() {
|
|
164
|
+
if (active == this) active = null;
|
|
112
165
|
if (player != null) {
|
|
113
166
|
player.removeListener(listener);
|
|
114
167
|
player.release();
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
// One instance owns one AVPlayer. Calls and notifications run on the host main thread.
|
|
4
4
|
@interface XplatAudioPlayer : NSObject
|
|
5
5
|
- (void)setSource:(NSString *)source;
|
|
6
|
+
- (void)setPlaybackRate:(double)rate;
|
|
7
|
+
- (void)setVolume:(double)volume;
|
|
8
|
+
- (void)setMuted:(BOOL)muted;
|
|
6
9
|
- (void)play;
|
|
7
10
|
- (void)pause;
|
|
8
11
|
- (void)seekSeconds:(double)seconds;
|
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
@property(nonatomic, strong) NSMutableArray *observers;
|
|
9
9
|
@property(nonatomic, copy) NSString *state;
|
|
10
10
|
@property(nonatomic, copy) NSString *failure;
|
|
11
|
+
@property(nonatomic) double playbackRate;
|
|
12
|
+
@property(nonatomic) double volume;
|
|
13
|
+
@property(nonatomic) BOOL muted;
|
|
11
14
|
@property(nonatomic) BOOL disposed;
|
|
12
15
|
@property(nonatomic) BOOL wantsPlayback;
|
|
13
16
|
@property(nonatomic) NSTimeInterval loadingSince;
|
|
@@ -17,6 +20,8 @@
|
|
|
17
20
|
- (instancetype)init {
|
|
18
21
|
if ((self = [super init])) {
|
|
19
22
|
_state = @"idle";
|
|
23
|
+
_playbackRate = 1;
|
|
24
|
+
_volume = 1;
|
|
20
25
|
_observers = [NSMutableArray new];
|
|
21
26
|
}
|
|
22
27
|
return self;
|
|
@@ -58,7 +63,10 @@
|
|
|
58
63
|
self.state = @"loading";
|
|
59
64
|
self.loadingSince = NSProcessInfo.processInfo.systemUptime;
|
|
60
65
|
self.item = [AVPlayerItem playerItemWithURL:url];
|
|
66
|
+
self.item.audioTimePitchAlgorithm = AVAudioTimePitchAlgorithmTimeDomain;
|
|
61
67
|
self.player = [AVPlayer playerWithPlayerItem:self.item];
|
|
68
|
+
self.player.volume = self.volume;
|
|
69
|
+
self.player.muted = self.muted;
|
|
62
70
|
__weak XplatAudioPlayer *owner = self;
|
|
63
71
|
AVPlayerItem *item = self.item;
|
|
64
72
|
NSNotificationCenter *center = NSNotificationCenter.defaultCenter;
|
|
@@ -106,10 +114,28 @@
|
|
|
106
114
|
self.state = @"paused";
|
|
107
115
|
}
|
|
108
116
|
self.wantsPlayback = YES;
|
|
109
|
-
|
|
117
|
+
// Submit one playback transition. play followed by setRate can freeze at
|
|
118
|
+
// time zero during rapid pause/resume on macOS 26 (see readiness record).
|
|
119
|
+
[self.player playImmediatelyAtRate:self.playbackRate];
|
|
110
120
|
[self refresh];
|
|
111
121
|
}
|
|
112
122
|
|
|
123
|
+
- (void)setPlaybackRate:(double)rate {
|
|
124
|
+
if (self.disposed || !isfinite(rate)) return;
|
|
125
|
+
_playbackRate = fmax(0.5, fmin(3, rate));
|
|
126
|
+
if (self.wantsPlayback) self.player.rate = _playbackRate;
|
|
127
|
+
}
|
|
128
|
+
- (void)setVolume:(double)volume {
|
|
129
|
+
if (self.disposed || !isfinite(volume)) return;
|
|
130
|
+
_volume = fmax(0, fmin(1, volume));
|
|
131
|
+
self.player.volume = _volume;
|
|
132
|
+
}
|
|
133
|
+
- (void)setMuted:(BOOL)muted {
|
|
134
|
+
if (self.disposed) return;
|
|
135
|
+
_muted = muted;
|
|
136
|
+
self.player.muted = muted;
|
|
137
|
+
}
|
|
138
|
+
|
|
113
139
|
- (void)pause {
|
|
114
140
|
if (!self.player || self.disposed) return;
|
|
115
141
|
self.wantsPlayback = NO;
|
package/src/audio.android.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { copyQueue } from './queue'
|
|
2
|
+
import { bounded } from './settings'
|
|
1
3
|
import { Utils } from '@nativescript/core'
|
|
2
4
|
import type { AudioCapabilities, AudioPlayer, AudioSnapshot, Track } from './types'
|
|
3
5
|
import { android } from '@octane-xplat/platform/android'
|
|
@@ -8,10 +10,14 @@ declare const com: {
|
|
|
8
10
|
audio: {
|
|
9
11
|
AudioBridge: {
|
|
10
12
|
ACTION_QUEUE: string
|
|
13
|
+
ACTION_UPDATE: string
|
|
11
14
|
ACTION_PLAY: string
|
|
12
15
|
ACTION_PAUSE: string
|
|
13
16
|
ACTION_SEEK: string
|
|
14
17
|
ACTION_RELEASE: string
|
|
18
|
+
ACTION_RATE: string
|
|
19
|
+
ACTION_VOLUME: string
|
|
20
|
+
ACTION_MUTED: string
|
|
15
21
|
dispatch(
|
|
16
22
|
context: android.content.Context,
|
|
17
23
|
action: string,
|
|
@@ -25,6 +31,7 @@ declare const com: {
|
|
|
25
31
|
}
|
|
26
32
|
}
|
|
27
33
|
|
|
34
|
+
/** Create an independent player on web/iOS/macOS; Android owns one app playback service. */
|
|
28
35
|
export const createAudioPlayer = (): AudioPlayer => {
|
|
29
36
|
const actions = com.xplat.audio.AudioBridge
|
|
30
37
|
// `Utils.android` is a {} stub in the pinned Windows core build.
|
|
@@ -33,12 +40,17 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
33
40
|
let queue: Track[] = []
|
|
34
41
|
let index = -1
|
|
35
42
|
let disposed = false
|
|
43
|
+
let playbackRate = 1
|
|
44
|
+
let volume = 1
|
|
45
|
+
let muted = false
|
|
46
|
+
let queueComplete = true
|
|
36
47
|
let poll: ReturnType<typeof setInterval> | undefined
|
|
37
48
|
|
|
38
49
|
const snapshot = (): AudioSnapshot => {
|
|
39
50
|
const value = JSON.parse(actions.snapshot()) as {
|
|
40
51
|
state: AudioSnapshot['state']
|
|
41
52
|
index?: number
|
|
53
|
+
id?: string
|
|
42
54
|
currentTime?: number
|
|
43
55
|
duration?: number
|
|
44
56
|
error?: string
|
|
@@ -50,7 +62,11 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
50
62
|
|
|
51
63
|
return {
|
|
52
64
|
state: value.state,
|
|
53
|
-
|
|
65
|
+
playbackRate,
|
|
66
|
+
volume,
|
|
67
|
+
muted,
|
|
68
|
+
queueComplete,
|
|
69
|
+
track: value.id ? queue.find((track) => track.id === value.id) : queue[index],
|
|
54
70
|
currentTime: value.currentTime ?? 0,
|
|
55
71
|
duration: value.duration ?? 0,
|
|
56
72
|
error: value.error ? new Error(value.error) : undefined,
|
|
@@ -101,12 +117,13 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
101
117
|
ensurePolling()
|
|
102
118
|
}
|
|
103
119
|
},
|
|
104
|
-
setQueue: async (tracks, startAt = 0) => {
|
|
120
|
+
setQueue: async (tracks, startAt = 0, options = {}) => {
|
|
105
121
|
if (disposed) {
|
|
106
122
|
throw new Error('AudioPlayer is disposed')
|
|
107
123
|
}
|
|
108
124
|
|
|
109
|
-
queue =
|
|
125
|
+
queue = copyQueue(tracks)
|
|
126
|
+
queueComplete = options.complete ?? true
|
|
110
127
|
index = queue.length
|
|
111
128
|
? Math.max(
|
|
112
129
|
0,
|
|
@@ -114,7 +131,11 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
114
131
|
)
|
|
115
132
|
: -1
|
|
116
133
|
|
|
117
|
-
dispatch(
|
|
134
|
+
dispatch(
|
|
135
|
+
actions.ACTION_QUEUE,
|
|
136
|
+
JSON.stringify({ tracks: queue, complete: queueComplete }),
|
|
137
|
+
index,
|
|
138
|
+
)
|
|
118
139
|
emit()
|
|
119
140
|
},
|
|
120
141
|
play: async () => dispatch(actions.ACTION_PLAY),
|
|
@@ -126,6 +147,32 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
126
147
|
|
|
127
148
|
emit()
|
|
128
149
|
},
|
|
150
|
+
|
|
151
|
+
updateQueue: async (tracks, options = {}) => {
|
|
152
|
+
const next = copyQueue(tracks)
|
|
153
|
+
const complete = options.complete ?? queueComplete
|
|
154
|
+
dispatch(actions.ACTION_UPDATE, JSON.stringify({ tracks: next, complete }))
|
|
155
|
+
queue = next
|
|
156
|
+
queueComplete = complete
|
|
157
|
+
emit()
|
|
158
|
+
},
|
|
159
|
+
setPlaybackRate: async (rate) => {
|
|
160
|
+
const next = bounded(rate, 0.5, 3)
|
|
161
|
+
dispatch(actions.ACTION_RATE, '', 0, next)
|
|
162
|
+
playbackRate = next
|
|
163
|
+
emit()
|
|
164
|
+
},
|
|
165
|
+
setVolume: async (gain) => {
|
|
166
|
+
const next = bounded(gain, 0, 1)
|
|
167
|
+
dispatch(actions.ACTION_VOLUME, '', 0, next)
|
|
168
|
+
volume = next
|
|
169
|
+
emit()
|
|
170
|
+
},
|
|
171
|
+
setMuted: async (value) => {
|
|
172
|
+
dispatch(actions.ACTION_MUTED, '', 0, value ? 1 : 0)
|
|
173
|
+
muted = value
|
|
174
|
+
emit()
|
|
175
|
+
},
|
|
129
176
|
dispose: () => {
|
|
130
177
|
if (disposed) {
|
|
131
178
|
return
|