@octane-xplat/audio 0.18.0 → 0.20.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.
@@ -1,146 +1,203 @@
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'
4
6
 
5
7
  // The plugin's Java bridge is exposed by NativeScript's runtime metadata.
6
8
  declare const com: {
7
- xplat: {
8
- audio: {
9
- AudioBridge: {
10
- ACTION_QUEUE: string
11
- ACTION_PLAY: string
12
- ACTION_PAUSE: string
13
- ACTION_SEEK: string
14
- ACTION_RELEASE: string
15
- dispatch(
16
- context: android.content.Context,
17
- action: string,
18
- payload: string,
19
- index: number,
20
- seconds: number,
21
- ): void
22
- snapshot(): string
23
- }
24
- }
25
- }
9
+ xplat: {
10
+ audio: {
11
+ AudioBridge: {
12
+ ACTION_QUEUE: string
13
+ ACTION_UPDATE: string
14
+ ACTION_PLAY: string
15
+ ACTION_PAUSE: string
16
+ ACTION_SEEK: string
17
+ ACTION_RELEASE: string
18
+ ACTION_RATE: string
19
+ ACTION_VOLUME: string
20
+ ACTION_MUTED: string
21
+ dispatch(
22
+ context: android.content.Context,
23
+ action: string,
24
+ payload: string,
25
+ index: number,
26
+ seconds: number,
27
+ ): void
28
+ snapshot(): string
29
+ }
30
+ }
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
- const actions = com.xplat.audio.AudioBridge
30
- // `Utils.android` is a {} stub in the pinned Windows core build.
31
- const context = (Utils.android as any).getApplicationContext()
32
- const listeners = new Set<(snapshot: AudioSnapshot) => void>()
33
- let queue: Track[] = []
34
- let index = -1
35
- let disposed = false
36
- let poll: ReturnType<typeof setInterval> | undefined
37
-
38
- const snapshot = (): AudioSnapshot => {
39
- const value = JSON.parse(actions.snapshot()) as {
40
- state: AudioSnapshot['state']
41
- index?: number
42
- currentTime?: number
43
- duration?: number
44
- error?: string
45
- }
46
-
47
- if (Number.isInteger(value.index) && value.index! >= 0) {
48
- index = value.index!
49
- }
50
-
51
- return {
52
- state: value.state,
53
- track: queue[index],
54
- currentTime: value.currentTime ?? 0,
55
- duration: value.duration ?? 0,
56
- error: value.error ? new Error(value.error) : undefined,
57
- }
58
- }
59
-
60
- const emit = () => {
61
- const value = snapshot()
62
- for (const listener of listeners) {
63
- listener(value)
64
- }
65
- }
66
-
67
- const dispatch = (action: string, payload = '', at = 0, seconds = 0) => {
68
- if (disposed) {
69
- throw new Error('AudioPlayer is disposed')
70
- }
71
-
72
- actions.dispatch(context, action, payload, at, seconds)
73
- }
74
-
75
- const ensurePolling = () => {
76
- if (listeners.size && !poll) {
77
- poll = setInterval(emit, 250)
78
- }
79
-
80
- if (!listeners.size && poll) {
81
- clearInterval(poll)
82
- poll = undefined
83
- }
84
- }
85
-
86
- return {
87
- capabilities: (): AudioCapabilities => ({
88
- supported: true,
89
- backgroundPlayback: true,
90
- systemControls: true,
91
- userGestureRequired: false,
92
- interruptions: true,
93
- }),
94
- snapshot,
95
- subscribe: (listener) => {
96
- listeners.add(listener)
97
- listener(snapshot())
98
- ensurePolling()
99
- return () => {
100
- listeners.delete(listener)
101
- ensurePolling()
102
- }
103
- },
104
- setQueue: async (tracks, startAt = 0) => {
105
- if (disposed) {
106
- throw new Error('AudioPlayer is disposed')
107
- }
108
-
109
- queue = [...tracks]
110
- index = queue.length
111
- ? Math.max(
112
- 0,
113
- Math.min(Number.isFinite(startAt) ? Math.floor(startAt) : 0, queue.length - 1),
114
- )
115
- : -1
116
-
117
- dispatch(actions.ACTION_QUEUE, JSON.stringify(queue), index)
118
- emit()
119
- },
120
- play: async () => dispatch(actions.ACTION_PLAY),
121
- pause: async () => dispatch(actions.ACTION_PAUSE),
122
- seek: async (seconds) => {
123
- if (Number.isFinite(seconds)) {
124
- dispatch(actions.ACTION_SEEK, '', 0, Math.max(0, seconds))
125
- }
126
-
127
- emit()
128
- },
129
- dispose: () => {
130
- if (disposed) {
131
- return
132
- }
133
-
134
- disposed = true
135
- actions.dispatch(context, actions.ACTION_RELEASE, '', 0, 0)
136
- if (poll) {
137
- clearInterval(poll)
138
- }
139
-
140
- poll = undefined
141
- listeners.clear()
142
- queue = []
143
- index = -1
144
- },
145
- }
36
+ const actions = com.xplat.audio.AudioBridge
37
+ // `Utils.android` is a {} stub in the pinned Windows core build.
38
+ const context = (Utils.android as any).getApplicationContext()
39
+ const listeners = new Set<(snapshot: AudioSnapshot) => void>()
40
+ let queue: Track[] = []
41
+ let index = -1
42
+ let disposed = false
43
+ let playbackRate = 1
44
+ let volume = 1
45
+ let muted = false
46
+ let queueComplete = true
47
+ let poll: ReturnType<typeof setInterval> | undefined
48
+
49
+ /** The Android bridge's snapshot payload. */
50
+ interface SnapshotReply {
51
+ state: AudioSnapshot['state']
52
+ index?: number
53
+ id?: string
54
+ currentTime?: number
55
+ duration?: number
56
+ error?: string
57
+ }
58
+
59
+ const snapshot = (): AudioSnapshot => {
60
+ const parsed: unknown = JSON.parse(actions.snapshot())
61
+ // Bridge-supplied envelope; validated fields below, bounded once here.
62
+ const value = (typeof parsed === 'object' && parsed !== null ? parsed : {}) as SnapshotReply
63
+
64
+ if (Number.isInteger(value.index) && value.index! >= 0) {
65
+ index = value.index!
66
+ }
67
+
68
+ return {
69
+ state: value.state,
70
+ playbackRate,
71
+ volume,
72
+ muted,
73
+ queueComplete,
74
+ track: value.id ? queue.find((track) => track.id === value.id) : queue[index],
75
+ currentTime: value.currentTime ?? 0,
76
+ duration: value.duration ?? 0,
77
+ error: value.error ? new Error(value.error) : undefined,
78
+ }
79
+ }
80
+
81
+ const emit = () => {
82
+ const value = snapshot()
83
+
84
+ for (const listener of listeners) {
85
+ listener(value)
86
+ }
87
+ }
88
+
89
+ const dispatch = (action: string, payload = '', at = 0, seconds = 0) => {
90
+ if (disposed) {
91
+ throw new Error('AudioPlayer is disposed')
92
+ }
93
+
94
+ actions.dispatch(context, action, payload, at, seconds)
95
+ }
96
+
97
+ const ensurePolling = () => {
98
+ if (listeners.size && !poll) {
99
+ poll = setInterval(emit, 250)
100
+ }
101
+
102
+ if (!listeners.size && poll) {
103
+ clearInterval(poll)
104
+ poll = undefined
105
+ }
106
+ }
107
+
108
+ return {
109
+ capabilities: (): AudioCapabilities => ({
110
+ supported: true,
111
+ backgroundPlayback: true,
112
+ systemControls: true,
113
+ userGestureRequired: false,
114
+ interruptions: true,
115
+ }),
116
+ snapshot,
117
+ subscribe: (listener) => {
118
+ listeners.add(listener)
119
+ listener(snapshot())
120
+ ensurePolling()
121
+ return () => {
122
+ listeners.delete(listener)
123
+ ensurePolling()
124
+ }
125
+ },
126
+ setQueue: async (tracks, startAt = 0, options = {}) => {
127
+ if (disposed) {
128
+ throw new Error('AudioPlayer is disposed')
129
+ }
130
+
131
+ queue = copyQueue(tracks)
132
+ queueComplete = options.complete ?? true
133
+ index = queue.length
134
+ ? Math.max(
135
+ 0,
136
+ Math.min(Number.isFinite(startAt) ? Math.floor(startAt) : 0, queue.length - 1),
137
+ )
138
+ : -1
139
+
140
+ dispatch(
141
+ actions.ACTION_QUEUE,
142
+ JSON.stringify({ tracks: queue, complete: queueComplete }),
143
+ index,
144
+ )
145
+
146
+ emit()
147
+ },
148
+ play: async () => dispatch(actions.ACTION_PLAY),
149
+ pause: async () => dispatch(actions.ACTION_PAUSE),
150
+ seek: async (seconds) => {
151
+ if (Number.isFinite(seconds)) {
152
+ dispatch(actions.ACTION_SEEK, '', 0, Math.max(0, seconds))
153
+ }
154
+
155
+ emit()
156
+ },
157
+
158
+ updateQueue: async (tracks, options = {}) => {
159
+ const next = copyQueue(tracks)
160
+ const complete = options.complete ?? queueComplete
161
+
162
+ dispatch(actions.ACTION_UPDATE, JSON.stringify({ tracks: next, complete }))
163
+ queue = next
164
+ queueComplete = complete
165
+ emit()
166
+ },
167
+ setPlaybackRate: async (rate) => {
168
+ const next = bounded(rate, 0.5, 3)
169
+
170
+ dispatch(actions.ACTION_RATE, '', 0, next)
171
+ playbackRate = next
172
+ emit()
173
+ },
174
+ setVolume: async (gain) => {
175
+ const next = bounded(gain, 0, 1)
176
+
177
+ dispatch(actions.ACTION_VOLUME, '', 0, next)
178
+ volume = next
179
+ emit()
180
+ },
181
+ setMuted: async (value) => {
182
+ dispatch(actions.ACTION_MUTED, '', 0, value ? 1 : 0)
183
+ muted = value
184
+ emit()
185
+ },
186
+ dispose: () => {
187
+ if (disposed) {
188
+ return
189
+ }
190
+
191
+ disposed = true
192
+ actions.dispatch(context, actions.ACTION_RELEASE, '', 0, 0)
193
+ if (poll) {
194
+ clearInterval(poll)
195
+ }
196
+
197
+ poll = undefined
198
+ listeners.clear()
199
+ queue = []
200
+ index = -1
201
+ },
202
+ }
146
203
  }