@octane-xplat/audio 0.7.2 → 0.8.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/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 +8 -3
- package/src/audio.android.ts +13 -0
- package/src/audio.ios.ts +34 -18
- package/src/audio.web.ts +3 -0
- package/types/index.d.ts +1 -1
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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octane-xplat/audio",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Long-form audio playback for Octane xplat",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"octane",
|
|
@@ -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.0",
|
|
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
|
@@ -6,6 +6,19 @@ import type {
|
|
|
6
6
|
Track,
|
|
7
7
|
} from "./types";
|
|
8
8
|
|
|
9
|
+
// The plugin's Java bridge is exposed by NativeScript's runtime metadata.
|
|
10
|
+
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
|
+
};
|
|
21
|
+
|
|
9
22
|
export const createAudioPlayer = (): AudioPlayer => {
|
|
10
23
|
const actions = com.xplat.audio.AudioBridge;
|
|
11
24
|
const context = Utils.android.getApplicationContext();
|
package/src/audio.ios.ts
CHANGED
|
@@ -5,9 +5,19 @@ import type {
|
|
|
5
5
|
Track,
|
|
6
6
|
} from "./types";
|
|
7
7
|
|
|
8
|
+
// CoreMedia is available at runtime but omitted from NativeScript's common
|
|
9
|
+
// ambient references. Reuse AVPlayer's time type for this bridge slice.
|
|
10
|
+
type PlayerTime = ReturnType<AVPlayer["currentTime"]>;
|
|
11
|
+
declare function CMTimeGetSeconds(time: PlayerTime): number;
|
|
12
|
+
declare function CMTimeMakeWithSeconds(seconds: number, preferredTimescale: number): PlayerTime;
|
|
13
|
+
|
|
8
14
|
export const createAudioPlayer = (): AudioPlayer => {
|
|
9
15
|
const listeners = new Set<(snapshot: AudioSnapshot) => void>();
|
|
10
16
|
const session = AVAudioSession.sharedInstance();
|
|
17
|
+
// NativeScript's ambient const enums cannot be read with verbatimModuleSyntax.
|
|
18
|
+
const interruptionBegan = 1; // AVAudioSessionInterruptionType.Began
|
|
19
|
+
const interruptionShouldResume = 1; // AVAudioSessionInterruptionOptions.ShouldResume
|
|
20
|
+
const notifyOthersOnDeactivation = 1 as AVAudioSessionSetActiveOptions;
|
|
11
21
|
// Lock-screen transport (MPRemoteCommandCenter / MPNowPlayingInfoCenter)
|
|
12
22
|
// needs MediaPlayer.framework linked AND metadata-generated — plugin
|
|
13
23
|
// LDFLAGS alone don't reach the metadata scan, so the MP* globals can be
|
|
@@ -15,8 +25,8 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
15
25
|
const hasMediaPlayer =
|
|
16
26
|
typeof MPRemoteCommandCenter !== 'undefined' &&
|
|
17
27
|
typeof MPNowPlayingInfoCenter !== 'undefined' &&
|
|
18
|
-
//
|
|
19
|
-
typeof (globalThis as any).
|
|
28
|
+
// The runtime metadata keys may be absent when MediaPlayer isn't linked.
|
|
29
|
+
typeof (globalThis as any).MPMediaItemPropertyPlaybackDuration !== 'undefined' &&
|
|
20
30
|
typeof (globalThis as any).MPRemoteCommandHandlerStatus !== 'undefined';
|
|
21
31
|
|
|
22
32
|
const commandCenter = hasMediaPlayer ? MPRemoteCommandCenter.sharedCommandCenter() : undefined;
|
|
@@ -34,7 +44,7 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
34
44
|
const remoteTargets: Array<{ command: any; token: any }> = [];
|
|
35
45
|
|
|
36
46
|
const current = () => queue[index];
|
|
37
|
-
const seconds = (time:
|
|
47
|
+
const seconds = (time: PlayerTime) => {
|
|
38
48
|
const value = CMTimeGetSeconds(time);
|
|
39
49
|
return Number.isFinite(value) && value > 0 ? value : 0;
|
|
40
50
|
};
|
|
@@ -56,7 +66,7 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
56
66
|
if (!nowPlaying) {return;}
|
|
57
67
|
const track = current();
|
|
58
68
|
if (!track) {
|
|
59
|
-
nowPlaying.nowPlayingInfo = null
|
|
69
|
+
nowPlaying.nowPlayingInfo = null as unknown as NSDictionary<string, any>;
|
|
60
70
|
return;
|
|
61
71
|
}
|
|
62
72
|
|
|
@@ -64,7 +74,11 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
64
74
|
info.setObjectForKey(track.title ?? track.id, MPMediaItemPropertyTitle);
|
|
65
75
|
if (track.artist) {info.setObjectForKey(track.artist, MPMediaItemPropertyArtist);}
|
|
66
76
|
if (track.album) {info.setObjectForKey(track.album, MPMediaItemPropertyAlbumTitle);}
|
|
67
|
-
info.setObjectForKey(
|
|
77
|
+
info.setObjectForKey(
|
|
78
|
+
snapshot().duration,
|
|
79
|
+
(globalThis as any).MPMediaItemPropertyPlaybackDuration,
|
|
80
|
+
);
|
|
81
|
+
|
|
68
82
|
info.setObjectForKey(snapshot().currentTime, MPNowPlayingInfoPropertyElapsedPlaybackTime);
|
|
69
83
|
info.setObjectForKey(player?.rate ?? 0, MPNowPlayingInfoPropertyPlaybackRate);
|
|
70
84
|
nowPlaying.nowPlayingInfo = info;
|
|
@@ -135,18 +149,25 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
135
149
|
const play = async () => {
|
|
136
150
|
if (!player || disposed) {return;}
|
|
137
151
|
const category = AVAudioSessionCategoryPlayback;
|
|
138
|
-
session.setCategoryModeOptionsError(category, AVAudioSessionModeDefault, 0,
|
|
139
|
-
session.setActiveWithOptionsError(true, 0,
|
|
152
|
+
session.setCategoryModeOptionsError(category, AVAudioSessionModeDefault, 0 as AVAudioSessionCategoryOptions, undefined);
|
|
153
|
+
session.setActiveWithOptionsError(true, 0 as AVAudioSessionSetActiveOptions, undefined);
|
|
140
154
|
player.play();
|
|
141
155
|
state = "playing";
|
|
142
156
|
updateNowPlaying();
|
|
143
157
|
emit();
|
|
144
158
|
};
|
|
145
159
|
|
|
160
|
+
const pause = async () => {
|
|
161
|
+
player?.pause();
|
|
162
|
+
state = "paused";
|
|
163
|
+
updateNowPlaying();
|
|
164
|
+
emit();
|
|
165
|
+
};
|
|
166
|
+
|
|
146
167
|
const addRemoteTarget = (command: any, handler: (event?: any) => void) => {
|
|
147
168
|
const token = command.addTargetWithHandler((event: any) => {
|
|
148
169
|
handler(event);
|
|
149
|
-
return MPRemoteCommandHandlerStatus.Success;
|
|
170
|
+
return (globalThis as any).MPRemoteCommandHandlerStatus.Success;
|
|
150
171
|
});
|
|
151
172
|
|
|
152
173
|
remoteTargets.push({ command, token });
|
|
@@ -172,7 +193,7 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
172
193
|
NSOperationQueue.mainQueue,
|
|
173
194
|
(notification) => {
|
|
174
195
|
const type = notification.userInfo.objectForKey(AVAudioSessionInterruptionTypeKey).unsignedIntegerValue;
|
|
175
|
-
if (type ===
|
|
196
|
+
if (type === interruptionBegan) {
|
|
176
197
|
wasPlayingBeforeInterruption = state === "playing";
|
|
177
198
|
player?.pause();
|
|
178
199
|
state = "paused";
|
|
@@ -180,7 +201,7 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
180
201
|
} else if (wasPlayingBeforeInterruption) {
|
|
181
202
|
const options = notification.userInfo.objectForKey(AVAudioSessionInterruptionOptionKey)?.unsignedIntegerValue ?? 0;
|
|
182
203
|
wasPlayingBeforeInterruption = false;
|
|
183
|
-
if ((options &
|
|
204
|
+
if ((options & interruptionShouldResume) !== 0) {
|
|
184
205
|
void play();
|
|
185
206
|
}
|
|
186
207
|
}
|
|
@@ -222,12 +243,7 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
222
243
|
await load();
|
|
223
244
|
},
|
|
224
245
|
play,
|
|
225
|
-
pause
|
|
226
|
-
player?.pause();
|
|
227
|
-
state = "paused";
|
|
228
|
-
updateNowPlaying();
|
|
229
|
-
emit();
|
|
230
|
-
},
|
|
246
|
+
pause,
|
|
231
247
|
seek: async (value) => {
|
|
232
248
|
if (!player || !Number.isFinite(value)) {return;}
|
|
233
249
|
player.seekToTime(CMTimeMakeWithSeconds(Math.max(0, value), 600));
|
|
@@ -244,8 +260,8 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
244
260
|
remoteTargets.length = 0;
|
|
245
261
|
if (timer) {clearInterval(timer);}
|
|
246
262
|
timer = undefined;
|
|
247
|
-
if (nowPlaying) {nowPlaying.nowPlayingInfo = null
|
|
248
|
-
session.setActiveWithOptionsError(false,
|
|
263
|
+
if (nowPlaying) {nowPlaying.nowPlayingInfo = null as unknown as NSDictionary<string, any>;}
|
|
264
|
+
session.setActiveWithOptionsError(false, notifyOthersOnDeactivation, undefined);
|
|
249
265
|
listeners.clear();
|
|
250
266
|
queue = [];
|
|
251
267
|
index = -1;
|
package/src/audio.web.ts
CHANGED
|
@@ -177,6 +177,9 @@ export const createAudioPlayer = (): AudioPlayer => {
|
|
|
177
177
|
queue = [];
|
|
178
178
|
if (typeof navigator !== "undefined" && "mediaSession" in navigator) {
|
|
179
179
|
navigator.mediaSession.metadata = null;
|
|
180
|
+
for (const action of ['play', 'pause', 'seekto', 'nexttrack', 'previoustrack'] as const) {
|
|
181
|
+
navigator.mediaSession.setActionHandler(action, null);
|
|
182
|
+
}
|
|
180
183
|
}
|
|
181
184
|
},
|
|
182
185
|
};
|
package/types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./types";
|
|
1
|
+
export * from "./types.js";
|