@octane-xplat/audio 0.8.0 → 0.9.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 ADDED
@@ -0,0 +1,37 @@
1
+ # `@octane-xplat/audio`
2
+
3
+ Long-form audio playback for Octane xplat apps — a queue player, not a
4
+ sound-effect bank. iOS drives `AVPlayer`/`AVAudioSession` (interruption
5
+ handling included) and Android `MediaPlayer`, both directly on
6
+ `@nativescript/core` with no plugin dependency; web drives an
7
+ `HTMLAudioElement` plus the Media Session API for system controls.
8
+
9
+ ```sh
10
+ pnpm add @octane-xplat/audio
11
+ ```
12
+
13
+ ```ts
14
+ import { createAudioPlayer } from '@octane-xplat/audio'
15
+
16
+ const player = createAudioPlayer()
17
+ await player.setQueue([{ id: 'ep1', source: 'https://example.com/ep1.mp3' }])
18
+ await player.play()
19
+
20
+ const off = player.subscribe((snapshot) => {
21
+ // { state, currentTime, duration } — poll or subscribe, your choice
22
+ })
23
+ ```
24
+
25
+ The `capabilities()` report is the honest part: `supported`,
26
+ `backgroundPlayback`, `systemControls`, `userGestureRequired`, and
27
+ `interruptions` vary by target and runtime state, so check them before
28
+ promising behavior like lock-screen controls. The native entry requires
29
+ iOS or Android — other targets get the web implementation or a thrown
30
+ error, never a silent no-op.
31
+
32
+ For short UI sounds use [`@octane-xplat/sounds`](../sounds/README.md).
33
+
34
+ Guide: [Media services](../../docs/media-services.md); per-target
35
+ availability: [platform notes](../../docs/platform-notes.md). Exercised by
36
+ the harness `MediaServices` screen
37
+ ([`packages/app/src/MediaServices.tsrx`](../app/src/MediaServices.tsrx)).
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "version": 1,
3
- "buildId": "23a80796-778e-4816-86ff-0f76f0caff17",
3
+ "buildId": "88e2d26d-87db-4f8d-8fc6-eff681f40299",
4
4
  "mode": "production",
5
5
  "capabilities": {
6
6
  "independentHydration": false
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "version": 1,
3
- "buildId": "e536dabe-c4ed-490d-8c24-faa74f2f819f",
3
+ "buildId": "7095855b-93bb-4465-832a-bc3e7427572e",
4
4
  "mode": "production",
5
5
  "capabilities": {
6
6
  "independentHydration": false
package/package.json CHANGED
@@ -1,25 +1,25 @@
1
1
  {
2
2
  "name": "@octane-xplat/audio",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "Long-form audio playback for Octane xplat",
5
5
  "keywords": [
6
- "octane",
7
- "xplat",
8
- "cross-platform",
6
+ "android",
9
7
  "audio",
10
8
  "audio-player",
11
- "playback",
9
+ "cross-platform",
10
+ "ios",
12
11
  "music",
13
12
  "nativescript",
14
- "ios",
15
- "android",
16
- "web"
13
+ "octane",
14
+ "playback",
15
+ "web",
16
+ "xplat"
17
17
  ],
18
- "license": "MIT",
19
18
  "homepage": "https://github.com/octane-xplat/octane-xplat/tree/main/packages/audio",
19
+ "license": "MIT",
20
20
  "repository": {
21
21
  "type": "git",
22
- "url": "git+https://github.com/octane-xplat/octane-xplat.git",
22
+ "url": "https://github.com/octane-xplat/octane-xplat",
23
23
  "directory": "packages/audio"
24
24
  },
25
25
  "files": [
@@ -45,7 +45,7 @@
45
45
  "@nativescript/core": "9.1.2",
46
46
  "@octanejs/vite-plugin": "0.1.61",
47
47
  "octane": "0.6.3",
48
- "tsrx-typegen": "0.2.0",
48
+ "tsrx-typegen": "0.2.1",
49
49
  "typescript": "5.9.3",
50
50
  "vite": "8.3.0"
51
51
  },
@@ -1,113 +1,144 @@
1
- import { Utils } from "@nativescript/core";
2
- import type {
3
- AudioCapabilities,
4
- AudioPlayer,
5
- AudioSnapshot,
6
- Track,
7
- } from "./types";
1
+ import { Utils } from '@nativescript/core'
2
+ import type { AudioCapabilities, AudioPlayer, AudioSnapshot, Track } from './types'
8
3
 
9
4
  // The plugin's Java bridge is exposed by NativeScript's runtime metadata.
10
5
  declare const com: {
11
- xplat: { audio: { AudioBridge: {
12
- ACTION_QUEUE: string;
13
- ACTION_PLAY: string;
14
- ACTION_PAUSE: string;
15
- ACTION_SEEK: string;
16
- ACTION_RELEASE: string;
17
- dispatch(context: android.content.Context, action: string, payload: string, index: number, seconds: number): void;
18
- snapshot(): string;
19
- } } };
20
- };
6
+ xplat: {
7
+ audio: {
8
+ AudioBridge: {
9
+ ACTION_QUEUE: string
10
+ ACTION_PLAY: string
11
+ ACTION_PAUSE: string
12
+ ACTION_SEEK: string
13
+ ACTION_RELEASE: string
14
+ dispatch(
15
+ context: android.content.Context,
16
+ action: string,
17
+ payload: string,
18
+ index: number,
19
+ seconds: number,
20
+ ): void
21
+ snapshot(): string
22
+ }
23
+ }
24
+ }
25
+ }
21
26
 
22
27
  export const createAudioPlayer = (): AudioPlayer => {
23
- const actions = com.xplat.audio.AudioBridge;
24
- const context = Utils.android.getApplicationContext();
25
- const listeners = new Set<(snapshot: AudioSnapshot) => void>();
26
- let queue: Track[] = [];
27
- let index = -1;
28
- let disposed = false;
29
- let poll: ReturnType<typeof setInterval> | undefined;
30
-
31
- const snapshot = (): AudioSnapshot => {
32
- const value = JSON.parse(actions.snapshot()) as {
33
- state: AudioSnapshot["state"];
34
- index?: number;
35
- currentTime?: number;
36
- duration?: number;
37
- error?: string;
38
- };
39
-
40
- if (Number.isInteger(value.index) && value.index! >= 0) {index = value.index!;}
41
- return {
42
- state: value.state,
43
- track: queue[index],
44
- currentTime: value.currentTime ?? 0,
45
- duration: value.duration ?? 0,
46
- error: value.error ? new Error(value.error) : undefined,
47
- };
48
- };
49
-
50
- const emit = () => {
51
- const value = snapshot();
52
- for (const listener of listeners) {listener(value);}
53
- };
54
-
55
- const dispatch = (action: string, payload = "", at = 0, seconds = 0) => {
56
- if (disposed) {throw new Error("AudioPlayer is disposed");}
57
- actions.dispatch(context, action, payload, at, seconds);
58
- };
59
-
60
- const ensurePolling = () => {
61
- if (listeners.size && !poll) {poll = setInterval(emit, 250);}
62
- if (!listeners.size && poll) {
63
- clearInterval(poll);
64
- poll = undefined;
65
- }
66
- };
67
-
68
- return {
69
- capabilities: (): AudioCapabilities => ({
70
- supported: true,
71
- backgroundPlayback: true,
72
- systemControls: true,
73
- userGestureRequired: false,
74
- interruptions: true,
75
- }),
76
- snapshot,
77
- subscribe: (listener) => {
78
- listeners.add(listener);
79
- listener(snapshot());
80
- ensurePolling();
81
- return () => {
82
- listeners.delete(listener);
83
- ensurePolling();
84
- };
85
- },
86
- setQueue: async (tracks, startAt = 0) => {
87
- if (disposed) {throw new Error("AudioPlayer is disposed");}
88
- queue = [...tracks];
89
- index = queue.length
90
- ? Math.max(0, Math.min(Number.isFinite(startAt) ? Math.floor(startAt) : 0, queue.length - 1))
91
- : -1;
92
-
93
- dispatch(actions.ACTION_QUEUE, JSON.stringify(queue), index);
94
- emit();
95
- },
96
- play: async () => dispatch(actions.ACTION_PLAY),
97
- pause: async () => dispatch(actions.ACTION_PAUSE),
98
- seek: async (seconds) => {
99
- if (Number.isFinite(seconds)) {dispatch(actions.ACTION_SEEK, "", 0, Math.max(0, seconds));}
100
- emit();
101
- },
102
- dispose: () => {
103
- if (disposed) {return;}
104
- disposed = true;
105
- actions.dispatch(context, actions.ACTION_RELEASE, "", 0, 0);
106
- if (poll) {clearInterval(poll);}
107
- poll = undefined;
108
- listeners.clear();
109
- queue = [];
110
- index = -1;
111
- },
112
- };
113
- };
28
+ const actions = com.xplat.audio.AudioBridge
29
+ const context = Utils.android.getApplicationContext()
30
+ const listeners = new Set<(snapshot: AudioSnapshot) => void>()
31
+ let queue: Track[] = []
32
+ let index = -1
33
+ let disposed = false
34
+ let poll: ReturnType<typeof setInterval> | undefined
35
+
36
+ const snapshot = (): AudioSnapshot => {
37
+ const value = JSON.parse(actions.snapshot()) as {
38
+ state: AudioSnapshot['state']
39
+ index?: number
40
+ currentTime?: number
41
+ duration?: number
42
+ error?: string
43
+ }
44
+
45
+ if (Number.isInteger(value.index) && value.index! >= 0) {
46
+ index = value.index!
47
+ }
48
+
49
+ return {
50
+ state: value.state,
51
+ track: queue[index],
52
+ currentTime: value.currentTime ?? 0,
53
+ duration: value.duration ?? 0,
54
+ error: value.error ? new Error(value.error) : undefined,
55
+ }
56
+ }
57
+
58
+ const emit = () => {
59
+ const value = snapshot()
60
+ for (const listener of listeners) {
61
+ listener(value)
62
+ }
63
+ }
64
+
65
+ const dispatch = (action: string, payload = '', at = 0, seconds = 0) => {
66
+ if (disposed) {
67
+ throw new Error('AudioPlayer is disposed')
68
+ }
69
+
70
+ actions.dispatch(context, action, payload, at, seconds)
71
+ }
72
+
73
+ const ensurePolling = () => {
74
+ if (listeners.size && !poll) {
75
+ poll = setInterval(emit, 250)
76
+ }
77
+
78
+ if (!listeners.size && poll) {
79
+ clearInterval(poll)
80
+ poll = undefined
81
+ }
82
+ }
83
+
84
+ return {
85
+ capabilities: (): AudioCapabilities => ({
86
+ supported: true,
87
+ backgroundPlayback: true,
88
+ systemControls: true,
89
+ userGestureRequired: false,
90
+ interruptions: true,
91
+ }),
92
+ snapshot,
93
+ subscribe: (listener) => {
94
+ listeners.add(listener)
95
+ listener(snapshot())
96
+ ensurePolling()
97
+ return () => {
98
+ listeners.delete(listener)
99
+ ensurePolling()
100
+ }
101
+ },
102
+ setQueue: async (tracks, startAt = 0) => {
103
+ if (disposed) {
104
+ throw new Error('AudioPlayer is disposed')
105
+ }
106
+
107
+ queue = [...tracks]
108
+ index = queue.length
109
+ ? Math.max(
110
+ 0,
111
+ Math.min(Number.isFinite(startAt) ? Math.floor(startAt) : 0, queue.length - 1),
112
+ )
113
+ : -1
114
+
115
+ dispatch(actions.ACTION_QUEUE, JSON.stringify(queue), index)
116
+ emit()
117
+ },
118
+ play: async () => dispatch(actions.ACTION_PLAY),
119
+ pause: async () => dispatch(actions.ACTION_PAUSE),
120
+ seek: async (seconds) => {
121
+ if (Number.isFinite(seconds)) {
122
+ dispatch(actions.ACTION_SEEK, '', 0, Math.max(0, seconds))
123
+ }
124
+
125
+ emit()
126
+ },
127
+ dispose: () => {
128
+ if (disposed) {
129
+ return
130
+ }
131
+
132
+ disposed = true
133
+ actions.dispatch(context, actions.ACTION_RELEASE, '', 0, 0)
134
+ if (poll) {
135
+ clearInterval(poll)
136
+ }
137
+
138
+ poll = undefined
139
+ listeners.clear()
140
+ queue = []
141
+ index = -1
142
+ },
143
+ }
144
+ }