@octane-xplat/audio 0.8.0 → 0.10.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 +45 -0
- package/dist/native/index2.js +1 -1
- package/dist/native/octane-client-build.json +1 -1
- package/dist/web/index2.js +3 -1
- package/dist/web/octane-client-build.json +1 -1
- package/package.json +11 -11
- package/src/audio.android.ts +140 -108
- package/src/audio.ios.ts +321 -266
- package/src/audio.ts +14 -8
- package/src/audio.web.ts +181 -184
- package/src/index.ts +2 -2
- package/src/index.web.ts +2 -2
- package/src/types.ts +31 -32
- package/types/index.d.ts +1 -1
- package/types/types.d.ts +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# `@octane-xplat/audio`
|
|
2
|
+
|
|
3
|
+
```sh
|
|
4
|
+
pnpm add @octane-xplat/audio
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
Long-form audio playback for Octane xplat apps — a queue player, not a
|
|
8
|
+
sound-effect bank. iOS drives `AVPlayer`/`AVAudioSession` (interruption
|
|
9
|
+
handling included) and Android `MediaPlayer`, both directly on
|
|
10
|
+
`@nativescript/core` with no plugin dependency; web drives an
|
|
11
|
+
`HTMLAudioElement` plus the Media Session API for system controls.
|
|
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
|
+
```ts
|
|
33
|
+
// Continue with the player created above.
|
|
34
|
+
const capabilities = player.capabilities()
|
|
35
|
+
console.log(capabilities.backgroundPlayback, capabilities.systemControls)
|
|
36
|
+
off()
|
|
37
|
+
player.dispose()
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
For short UI sounds use [`@octane-xplat/sounds`](../sounds/README.md).
|
|
41
|
+
|
|
42
|
+
Guide: [Media services](../../docs/platform/media-services.md); per-target
|
|
43
|
+
availability: [platform notes](../../docs/notes/platform-notes.md). Exercised by
|
|
44
|
+
the harness `MediaServices` screen
|
|
45
|
+
([`packages/app/src/MediaServices.tsrx`](../app/src/MediaServices.tsrx)).
|
package/dist/native/index2.js
CHANGED
|
@@ -198,7 +198,7 @@ var createAudioPlayer = () => {
|
|
|
198
198
|
if (timer) clearInterval(timer);
|
|
199
199
|
timer = void 0;
|
|
200
200
|
if (nowPlaying) nowPlaying.nowPlayingInfo = null;
|
|
201
|
-
session.setActiveWithOptionsError(false, notifyOthersOnDeactivation,
|
|
201
|
+
session.setActiveWithOptionsError(false, notifyOthersOnDeactivation, null);
|
|
202
202
|
listeners.clear();
|
|
203
203
|
queue = [];
|
|
204
204
|
index = -1;
|
package/dist/web/index2.js
CHANGED
|
@@ -73,12 +73,14 @@ var createAudioPlayer = () => {
|
|
|
73
73
|
"timeupdate",
|
|
74
74
|
"durationchange",
|
|
75
75
|
"play",
|
|
76
|
+
"playing",
|
|
76
77
|
"pause",
|
|
77
78
|
"ended",
|
|
78
79
|
"waiting",
|
|
79
80
|
"error"
|
|
80
81
|
]) audio.addEventListener(event, () => {
|
|
81
|
-
if (event === "play") state = "
|
|
82
|
+
if (event === "play") state = "loading";
|
|
83
|
+
else if (event === "playing") state = "playing";
|
|
82
84
|
else if (event === "pause" && state !== "ended") state = "paused";
|
|
83
85
|
else if (event === "ended") {
|
|
84
86
|
state = "ended";
|
package/package.json
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octane-xplat/audio",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Long-form audio playback for Octane xplat",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"
|
|
7
|
-
"xplat",
|
|
8
|
-
"cross-platform",
|
|
6
|
+
"android",
|
|
9
7
|
"audio",
|
|
10
8
|
"audio-player",
|
|
11
|
-
"
|
|
9
|
+
"cross-platform",
|
|
10
|
+
"ios",
|
|
12
11
|
"music",
|
|
13
12
|
"nativescript",
|
|
14
|
-
"
|
|
15
|
-
"
|
|
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": "
|
|
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.
|
|
48
|
+
"tsrx-typegen": "0.2.2",
|
|
49
49
|
"typescript": "5.9.3",
|
|
50
50
|
"vite": "8.3.0"
|
|
51
51
|
},
|
package/src/audio.android.ts
CHANGED
|
@@ -1,113 +1,145 @@
|
|
|
1
|
-
import { Utils } from
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
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
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
28
|
+
const actions = com.xplat.audio.AudioBridge
|
|
29
|
+
// `Utils.android` is a {} stub in the pinned Windows core build.
|
|
30
|
+
const context = (Utils.android as any).getApplicationContext()
|
|
31
|
+
const listeners = new Set<(snapshot: AudioSnapshot) => void>()
|
|
32
|
+
let queue: Track[] = []
|
|
33
|
+
let index = -1
|
|
34
|
+
let disposed = false
|
|
35
|
+
let poll: ReturnType<typeof setInterval> | undefined
|
|
36
|
+
|
|
37
|
+
const snapshot = (): AudioSnapshot => {
|
|
38
|
+
const value = JSON.parse(actions.snapshot()) as {
|
|
39
|
+
state: AudioSnapshot['state']
|
|
40
|
+
index?: number
|
|
41
|
+
currentTime?: number
|
|
42
|
+
duration?: number
|
|
43
|
+
error?: string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (Number.isInteger(value.index) && value.index! >= 0) {
|
|
47
|
+
index = value.index!
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
state: value.state,
|
|
52
|
+
track: queue[index],
|
|
53
|
+
currentTime: value.currentTime ?? 0,
|
|
54
|
+
duration: value.duration ?? 0,
|
|
55
|
+
error: value.error ? new Error(value.error) : undefined,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const emit = () => {
|
|
60
|
+
const value = snapshot()
|
|
61
|
+
for (const listener of listeners) {
|
|
62
|
+
listener(value)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const dispatch = (action: string, payload = '', at = 0, seconds = 0) => {
|
|
67
|
+
if (disposed) {
|
|
68
|
+
throw new Error('AudioPlayer is disposed')
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
actions.dispatch(context, action, payload, at, seconds)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const ensurePolling = () => {
|
|
75
|
+
if (listeners.size && !poll) {
|
|
76
|
+
poll = setInterval(emit, 250)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (!listeners.size && poll) {
|
|
80
|
+
clearInterval(poll)
|
|
81
|
+
poll = undefined
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
capabilities: (): AudioCapabilities => ({
|
|
87
|
+
supported: true,
|
|
88
|
+
backgroundPlayback: true,
|
|
89
|
+
systemControls: true,
|
|
90
|
+
userGestureRequired: false,
|
|
91
|
+
interruptions: true,
|
|
92
|
+
}),
|
|
93
|
+
snapshot,
|
|
94
|
+
subscribe: (listener) => {
|
|
95
|
+
listeners.add(listener)
|
|
96
|
+
listener(snapshot())
|
|
97
|
+
ensurePolling()
|
|
98
|
+
return () => {
|
|
99
|
+
listeners.delete(listener)
|
|
100
|
+
ensurePolling()
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
setQueue: async (tracks, startAt = 0) => {
|
|
104
|
+
if (disposed) {
|
|
105
|
+
throw new Error('AudioPlayer is disposed')
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
queue = [...tracks]
|
|
109
|
+
index = queue.length
|
|
110
|
+
? Math.max(
|
|
111
|
+
0,
|
|
112
|
+
Math.min(Number.isFinite(startAt) ? Math.floor(startAt) : 0, queue.length - 1),
|
|
113
|
+
)
|
|
114
|
+
: -1
|
|
115
|
+
|
|
116
|
+
dispatch(actions.ACTION_QUEUE, JSON.stringify(queue), index)
|
|
117
|
+
emit()
|
|
118
|
+
},
|
|
119
|
+
play: async () => dispatch(actions.ACTION_PLAY),
|
|
120
|
+
pause: async () => dispatch(actions.ACTION_PAUSE),
|
|
121
|
+
seek: async (seconds) => {
|
|
122
|
+
if (Number.isFinite(seconds)) {
|
|
123
|
+
dispatch(actions.ACTION_SEEK, '', 0, Math.max(0, seconds))
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
emit()
|
|
127
|
+
},
|
|
128
|
+
dispose: () => {
|
|
129
|
+
if (disposed) {
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
disposed = true
|
|
134
|
+
actions.dispatch(context, actions.ACTION_RELEASE, '', 0, 0)
|
|
135
|
+
if (poll) {
|
|
136
|
+
clearInterval(poll)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
poll = undefined
|
|
140
|
+
listeners.clear()
|
|
141
|
+
queue = []
|
|
142
|
+
index = -1
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
}
|