@octane-xplat/audio 0.7.3 → 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 +37 -0
- package/dist/native/index2.js +18 -14
- package/dist/native/octane-client-build.json +1 -1
- package/dist/web/index2.js +10 -1
- package/dist/web/octane-client-build.json +1 -1
- package/package.json +17 -12
- package/src/audio.android.ts +142 -98
- package/src/audio.ios.ts +319 -253
- package/src/audio.ts +14 -8
- package/src/audio.web.ts +178 -181
- 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,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)).
|
package/dist/native/index2.js
CHANGED
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
var createAudioPlayer = () => {
|
|
3
3
|
const listeners = /* @__PURE__ */ new Set();
|
|
4
4
|
const session = AVAudioSession.sharedInstance();
|
|
5
|
-
const
|
|
5
|
+
const interruptionBegan = 1;
|
|
6
|
+
const interruptionShouldResume = 1;
|
|
7
|
+
const notifyOthersOnDeactivation = 1;
|
|
8
|
+
const hasMediaPlayer = typeof MPRemoteCommandCenter !== "undefined" && typeof MPNowPlayingInfoCenter !== "undefined" && typeof globalThis.MPMediaItemPropertyPlaybackDuration !== "undefined" && typeof globalThis.MPRemoteCommandHandlerStatus !== "undefined";
|
|
6
9
|
const commandCenter = hasMediaPlayer ? MPRemoteCommandCenter.sharedCommandCenter() : void 0;
|
|
7
10
|
const nowPlaying = hasMediaPlayer ? MPNowPlayingInfoCenter.defaultCenter() : void 0;
|
|
8
11
|
let queue = [];
|
|
@@ -43,7 +46,7 @@ var createAudioPlayer = () => {
|
|
|
43
46
|
info.setObjectForKey(track.title ?? track.id, MPMediaItemPropertyTitle);
|
|
44
47
|
if (track.artist) info.setObjectForKey(track.artist, MPMediaItemPropertyArtist);
|
|
45
48
|
if (track.album) info.setObjectForKey(track.album, MPMediaItemPropertyAlbumTitle);
|
|
46
|
-
info.setObjectForKey(snapshot().duration,
|
|
49
|
+
info.setObjectForKey(snapshot().duration, globalThis.MPMediaItemPropertyPlaybackDuration);
|
|
47
50
|
info.setObjectForKey(snapshot().currentTime, MPNowPlayingInfoPropertyElapsedPlaybackTime);
|
|
48
51
|
info.setObjectForKey(player?.rate ?? 0, MPNowPlayingInfoPropertyPlaybackRate);
|
|
49
52
|
nowPlaying.nowPlayingInfo = info;
|
|
@@ -98,17 +101,23 @@ var createAudioPlayer = () => {
|
|
|
98
101
|
const play = async () => {
|
|
99
102
|
if (!player || disposed) return;
|
|
100
103
|
const category = AVAudioSessionCategoryPlayback;
|
|
101
|
-
session.setCategoryModeOptionsError(category, AVAudioSessionModeDefault, 0,
|
|
102
|
-
session.setActiveWithOptionsError(true, 0,
|
|
104
|
+
session.setCategoryModeOptionsError(category, AVAudioSessionModeDefault, 0, void 0);
|
|
105
|
+
session.setActiveWithOptionsError(true, 0, void 0);
|
|
103
106
|
player.play();
|
|
104
107
|
state = "playing";
|
|
105
108
|
updateNowPlaying();
|
|
106
109
|
emit();
|
|
107
110
|
};
|
|
111
|
+
const pause = async () => {
|
|
112
|
+
player?.pause();
|
|
113
|
+
state = "paused";
|
|
114
|
+
updateNowPlaying();
|
|
115
|
+
emit();
|
|
116
|
+
};
|
|
108
117
|
const addRemoteTarget = (command, handler) => {
|
|
109
118
|
const token = command.addTargetWithHandler((event) => {
|
|
110
119
|
handler(event);
|
|
111
|
-
return MPRemoteCommandHandlerStatus.Success;
|
|
120
|
+
return globalThis.MPRemoteCommandHandlerStatus.Success;
|
|
112
121
|
});
|
|
113
122
|
remoteTargets.push({
|
|
114
123
|
command,
|
|
@@ -129,7 +138,7 @@ var createAudioPlayer = () => {
|
|
|
129
138
|
});
|
|
130
139
|
}
|
|
131
140
|
interruptionObserver = NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock(AVAudioSessionInterruptionNotification, null, NSOperationQueue.mainQueue, (notification) => {
|
|
132
|
-
if (notification.userInfo.objectForKey(AVAudioSessionInterruptionTypeKey).unsignedIntegerValue ===
|
|
141
|
+
if (notification.userInfo.objectForKey(AVAudioSessionInterruptionTypeKey).unsignedIntegerValue === interruptionBegan) {
|
|
133
142
|
wasPlayingBeforeInterruption = state === "playing";
|
|
134
143
|
player?.pause();
|
|
135
144
|
state = "paused";
|
|
@@ -137,7 +146,7 @@ var createAudioPlayer = () => {
|
|
|
137
146
|
} else if (wasPlayingBeforeInterruption) {
|
|
138
147
|
const options = notification.userInfo.objectForKey(AVAudioSessionInterruptionOptionKey)?.unsignedIntegerValue ?? 0;
|
|
139
148
|
wasPlayingBeforeInterruption = false;
|
|
140
|
-
if ((options &
|
|
149
|
+
if ((options & interruptionShouldResume) !== 0) play();
|
|
141
150
|
}
|
|
142
151
|
});
|
|
143
152
|
return {
|
|
@@ -171,12 +180,7 @@ var createAudioPlayer = () => {
|
|
|
171
180
|
await load();
|
|
172
181
|
},
|
|
173
182
|
play,
|
|
174
|
-
pause
|
|
175
|
-
player?.pause();
|
|
176
|
-
state = "paused";
|
|
177
|
-
updateNowPlaying();
|
|
178
|
-
emit();
|
|
179
|
-
},
|
|
183
|
+
pause,
|
|
180
184
|
seek: async (value) => {
|
|
181
185
|
if (!player || !Number.isFinite(value)) return;
|
|
182
186
|
player.seekToTime(CMTimeMakeWithSeconds(Math.max(0, value), 600));
|
|
@@ -194,7 +198,7 @@ var createAudioPlayer = () => {
|
|
|
194
198
|
if (timer) clearInterval(timer);
|
|
195
199
|
timer = void 0;
|
|
196
200
|
if (nowPlaying) nowPlaying.nowPlayingInfo = null;
|
|
197
|
-
session.setActiveWithOptionsError(false,
|
|
201
|
+
session.setActiveWithOptionsError(false, notifyOthersOnDeactivation, void 0);
|
|
198
202
|
listeners.clear();
|
|
199
203
|
queue = [];
|
|
200
204
|
index = -1;
|
package/dist/web/index2.js
CHANGED
|
@@ -128,7 +128,16 @@ var createAudioPlayer = () => {
|
|
|
128
128
|
audio.load();
|
|
129
129
|
listeners.clear();
|
|
130
130
|
queue = [];
|
|
131
|
-
if (typeof navigator !== "undefined" && "mediaSession" in navigator)
|
|
131
|
+
if (typeof navigator !== "undefined" && "mediaSession" in navigator) {
|
|
132
|
+
navigator.mediaSession.metadata = null;
|
|
133
|
+
for (const action of [
|
|
134
|
+
"play",
|
|
135
|
+
"pause",
|
|
136
|
+
"seekto",
|
|
137
|
+
"nexttrack",
|
|
138
|
+
"previoustrack"
|
|
139
|
+
]) navigator.mediaSession.setActionHandler(action, null);
|
|
140
|
+
}
|
|
132
141
|
}
|
|
133
142
|
};
|
|
134
143
|
};
|
package/package.json
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octane-xplat/audio",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.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": [
|
|
@@ -41,10 +41,11 @@
|
|
|
41
41
|
"access": "public"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@nativescript-community/octane": "0.2.
|
|
44
|
+
"@nativescript-community/octane": "0.2.4",
|
|
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.1",
|
|
48
49
|
"typescript": "5.9.3",
|
|
49
50
|
"vite": "8.3.0"
|
|
50
51
|
},
|
|
@@ -71,6 +72,10 @@
|
|
|
71
72
|
}
|
|
72
73
|
},
|
|
73
74
|
"scripts": {
|
|
74
|
-
"
|
|
75
|
+
"test": "node --test tests/*.test.mjs",
|
|
76
|
+
"build": "vite build && vite build --mode native && pnpm typegen && node ../../scripts/check-native-dist.mjs",
|
|
77
|
+
"typegen": "tsc -p tsconfig.types.json",
|
|
78
|
+
"test:packed": "node tests/packed-consumer.mjs",
|
|
79
|
+
"pack:check": "tsrx-typegen --pack-check"
|
|
75
80
|
}
|
|
76
81
|
}
|
package/src/audio.android.ts
CHANGED
|
@@ -1,100 +1,144 @@
|
|
|
1
|
-
import { Utils } from
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { Utils } from '@nativescript/core'
|
|
2
|
+
import type { AudioCapabilities, AudioPlayer, AudioSnapshot, Track } from './types'
|
|
3
|
+
|
|
4
|
+
// The plugin's Java bridge is exposed by NativeScript's runtime metadata.
|
|
5
|
+
declare const com: {
|
|
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
|
+
}
|
|
8
26
|
|
|
9
27
|
export const createAudioPlayer = (): AudioPlayer => {
|
|
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
|
-
|
|
100
|
-
|
|
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
|
+
}
|