@octane-xplat/audio 0.0.0-nightly.g2e22171
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/LICENSE +21 -0
- package/README.md +69 -0
- package/package.json +99 -0
- package/platforms/android/AndroidManifest.xml +15 -0
- package/platforms/android/audio.aar +0 -0
- package/platforms/android/include.gradle +8 -0
- package/platforms/android/java/com/xplat/audio/AudioBridge.java +58 -0
- package/platforms/android/java/com/xplat/audio/AudioPlaybackService.java +122 -0
- package/platforms/ios/build.xcconfig +5 -0
- package/platforms/macos/include/XplatAudioPlayer.h +12 -0
- package/platforms/macos/src/XplatAudioPlayer.m +161 -0
- package/src/audio.android.ts +146 -0
- package/src/audio.ios.ts +391 -0
- package/src/audio.macos.ts +278 -0
- package/src/audio.ts +16 -0
- package/src/audio.web.ts +183 -0
- package/src/index.macos.ts +2 -0
- package/src/index.ts +2 -0
- package/src/index.web.ts +2 -0
- package/src/types.ts +41 -0
- package/types/index.d.ts +1 -0
- package/types/types.d.ts +36 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { Utils } from '@nativescript/core'
|
|
2
|
+
import type { AudioCapabilities, AudioPlayer, AudioSnapshot, Track } from './types'
|
|
3
|
+
import { android } from '@octane-xplat/platform/android'
|
|
4
|
+
|
|
5
|
+
// The plugin's Java bridge is exposed by NativeScript's runtime metadata.
|
|
6
|
+
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
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
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
|
+
}
|
|
146
|
+
}
|
package/src/audio.ios.ts
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import type { AudioCapabilities, AudioPlayer, AudioSnapshot, Track } from './types'
|
|
2
|
+
import {
|
|
3
|
+
AVAudioSession,
|
|
4
|
+
AVAudioSessionCategoryOptions,
|
|
5
|
+
AVAudioSessionSetActiveOptions,
|
|
6
|
+
AVPlayer,
|
|
7
|
+
AVPlayerItem,
|
|
8
|
+
MPNowPlayingInfoCenter,
|
|
9
|
+
MPRemoteCommandCenter,
|
|
10
|
+
NSDictionary,
|
|
11
|
+
NSMutableDictionary,
|
|
12
|
+
NSNotificationCenter,
|
|
13
|
+
NSOperationQueue,
|
|
14
|
+
NSURL,
|
|
15
|
+
} from '@octane-xplat/platform/ios'
|
|
16
|
+
|
|
17
|
+
import type { NSObjectProtocol } from '@octane-xplat/platform/ios'
|
|
18
|
+
|
|
19
|
+
// CoreMedia is available at runtime but omitted from NativeScript's common
|
|
20
|
+
// ambient references. Reuse AVPlayer's time type for this bridge slice.
|
|
21
|
+
type PlayerTime = ReturnType<AVPlayer['currentTime']>
|
|
22
|
+
declare function CMTimeGetSeconds(time: PlayerTime): number
|
|
23
|
+
declare function CMTimeMakeWithSeconds(seconds: number, preferredTimescale: number): PlayerTime
|
|
24
|
+
|
|
25
|
+
export const createAudioPlayer = (): AudioPlayer => {
|
|
26
|
+
const listeners = new Set<(snapshot: AudioSnapshot) => void>()
|
|
27
|
+
const session = AVAudioSession.sharedInstance()
|
|
28
|
+
// NativeScript's ambient const enums cannot be read with verbatimModuleSyntax.
|
|
29
|
+
const interruptionBegan = 1 // AVAudioSessionInterruptionType.Began
|
|
30
|
+
const interruptionEnded = 0 // AVAudioSessionInterruptionType.Ended
|
|
31
|
+
const interruptionShouldResume = 1 // AVAudioSessionInterruptionOptions.ShouldResume
|
|
32
|
+
const statusFailed = 2 // AVPlayerStatus.Failed
|
|
33
|
+
const notifyOthersOnDeactivation = 1 as AVAudioSessionSetActiveOptions
|
|
34
|
+
// Lock-screen transport (MPRemoteCommandCenter / MPNowPlayingInfoCenter)
|
|
35
|
+
// needs MediaPlayer.framework linked AND metadata-generated — plugin
|
|
36
|
+
// LDFLAGS alone don't reach the metadata scan, so the MP* globals can be
|
|
37
|
+
// undefined. Playback still works; guard and report systemControls:false.
|
|
38
|
+
const hasMediaPlayer =
|
|
39
|
+
typeof MPRemoteCommandCenter !== 'undefined' &&
|
|
40
|
+
typeof MPNowPlayingInfoCenter !== 'undefined' &&
|
|
41
|
+
// The runtime metadata keys may be absent when MediaPlayer isn't linked.
|
|
42
|
+
typeof (globalThis as any).MPMediaItemPropertyPlaybackDuration !== 'undefined' &&
|
|
43
|
+
typeof (globalThis as any).MPRemoteCommandHandlerStatus !== 'undefined'
|
|
44
|
+
|
|
45
|
+
const commandCenter = hasMediaPlayer ? MPRemoteCommandCenter.sharedCommandCenter() : undefined
|
|
46
|
+
const nowPlaying = hasMediaPlayer ? MPNowPlayingInfoCenter.defaultCenter() : undefined
|
|
47
|
+
let queue: Track[] = []
|
|
48
|
+
let index = -1
|
|
49
|
+
let player: AVPlayer | undefined
|
|
50
|
+
let state: AudioSnapshot['state'] = 'idle'
|
|
51
|
+
let error: Error | undefined
|
|
52
|
+
let disposed = false
|
|
53
|
+
let wasPlayingBeforeInterruption = false
|
|
54
|
+
let timer: ReturnType<typeof setInterval> | undefined
|
|
55
|
+
let endObserver: NSObjectProtocol | undefined
|
|
56
|
+
let failureObserver: NSObjectProtocol | undefined
|
|
57
|
+
let interruptionObserver: NSObjectProtocol | undefined
|
|
58
|
+
const remoteTargets: Array<{ command: any; token: any }> = []
|
|
59
|
+
|
|
60
|
+
const current = () => queue[index]
|
|
61
|
+
const seconds = (time: PlayerTime) => {
|
|
62
|
+
const value = CMTimeGetSeconds(time)
|
|
63
|
+
return Number.isFinite(value) && value > 0 ? value : 0
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const snapshot = (): AudioSnapshot => ({
|
|
67
|
+
state,
|
|
68
|
+
track: current(),
|
|
69
|
+
currentTime: player ? seconds(player.currentTime()) : 0,
|
|
70
|
+
duration: player?.currentItem ? seconds(player.currentItem.duration) : 0,
|
|
71
|
+
error,
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
const emit = () => {
|
|
75
|
+
const value = snapshot()
|
|
76
|
+
for (const listener of listeners) {
|
|
77
|
+
listener(value)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const updateNowPlaying = () => {
|
|
82
|
+
if (!nowPlaying) {
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const track = current()
|
|
87
|
+
if (!track) {
|
|
88
|
+
nowPlaying.nowPlayingInfo = null as unknown as NSDictionary<string, any>
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const info = NSMutableDictionary.dictionary()
|
|
93
|
+
info.setObjectForKey(track.title ?? track.id, MPMediaItemPropertyTitle)
|
|
94
|
+
if (track.artist) {
|
|
95
|
+
info.setObjectForKey(track.artist, MPMediaItemPropertyArtist)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (track.album) {
|
|
99
|
+
info.setObjectForKey(track.album, MPMediaItemPropertyAlbumTitle)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
info.setObjectForKey(
|
|
103
|
+
snapshot().duration,
|
|
104
|
+
(globalThis as any).MPMediaItemPropertyPlaybackDuration,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
info.setObjectForKey(snapshot().currentTime, MPNowPlayingInfoPropertyElapsedPlaybackTime)
|
|
108
|
+
info.setObjectForKey(player?.rate ?? 0, MPNowPlayingInfoPropertyPlaybackRate)
|
|
109
|
+
nowPlaying.nowPlayingInfo = info
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// AVPlayer failures arrive asynchronously — the item error carries the
|
|
113
|
+
// detail, falling back to the player error.
|
|
114
|
+
const playerError = () => {
|
|
115
|
+
const cause = player?.currentItem?.error ?? player?.error
|
|
116
|
+
return new Error(cause?.localizedDescription ?? 'Audio playback failed')
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const clearTrack = () => {
|
|
120
|
+
if (endObserver) {
|
|
121
|
+
NSNotificationCenter.defaultCenter.removeObserver(endObserver)
|
|
122
|
+
endObserver = undefined
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (failureObserver) {
|
|
126
|
+
NSNotificationCenter.defaultCenter.removeObserver(failureObserver)
|
|
127
|
+
failureObserver = undefined
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
player?.pause()
|
|
131
|
+
player = undefined
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const load = async (autoplay = false) => {
|
|
135
|
+
clearTrack()
|
|
136
|
+
wasPlayingBeforeInterruption = false
|
|
137
|
+
const track = current()
|
|
138
|
+
if (!track) {
|
|
139
|
+
state = 'idle'
|
|
140
|
+
updateNowPlaying()
|
|
141
|
+
emit()
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
state = 'loading'
|
|
146
|
+
error = undefined
|
|
147
|
+
emit()
|
|
148
|
+
try {
|
|
149
|
+
const url = track.source.startsWith('/')
|
|
150
|
+
? NSURL.fileURLWithPath(track.source)
|
|
151
|
+
: NSURL.URLWithString(track.source)
|
|
152
|
+
|
|
153
|
+
const item = AVPlayerItem.playerItemWithURL(url)
|
|
154
|
+
player = AVPlayer.playerWithPlayerItem(item)
|
|
155
|
+
endObserver = NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock(
|
|
156
|
+
AVPlayerItemDidPlayToEndTimeNotification,
|
|
157
|
+
item,
|
|
158
|
+
NSOperationQueue.mainQueue,
|
|
159
|
+
() => void advance(1),
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
failureObserver = NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock(
|
|
163
|
+
AVPlayerItemFailedToPlayToEndTimeNotification,
|
|
164
|
+
item,
|
|
165
|
+
NSOperationQueue.mainQueue,
|
|
166
|
+
(notification) => {
|
|
167
|
+
const cause = notification.userInfo?.objectForKey(
|
|
168
|
+
AVPlayerItemFailedToPlayToEndTimeErrorKey,
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
error = new Error(cause?.localizedDescription ?? 'Audio playback failed')
|
|
172
|
+
state = 'error'
|
|
173
|
+
player?.pause()
|
|
174
|
+
emit()
|
|
175
|
+
},
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
state = 'paused'
|
|
179
|
+
updateNowPlaying()
|
|
180
|
+
emit()
|
|
181
|
+
if (autoplay) {
|
|
182
|
+
await play()
|
|
183
|
+
}
|
|
184
|
+
} catch (cause) {
|
|
185
|
+
error = cause instanceof Error ? cause : new Error(String(cause))
|
|
186
|
+
state = 'error'
|
|
187
|
+
emit()
|
|
188
|
+
throw error
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const advance = async (delta: number) => {
|
|
193
|
+
if (!queue.length || disposed) {
|
|
194
|
+
return
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (index + delta >= queue.length) {
|
|
198
|
+
state = 'ended'
|
|
199
|
+
player?.pause()
|
|
200
|
+
emit()
|
|
201
|
+
return
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
index = Math.max(0, index + delta)
|
|
205
|
+
await load(true)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const play = async () => {
|
|
209
|
+
if (!player || disposed) {
|
|
210
|
+
return
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const category = AVAudioSessionCategoryPlayback
|
|
214
|
+
session.setCategoryModeOptionsError(
|
|
215
|
+
category,
|
|
216
|
+
AVAudioSessionModeDefault,
|
|
217
|
+
0 as AVAudioSessionCategoryOptions,
|
|
218
|
+
undefined,
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
session.setActiveWithOptionsError(true, 0 as AVAudioSessionSetActiveOptions, undefined)
|
|
222
|
+
if (state === 'ended') {
|
|
223
|
+
// The final item sits at end-of-stream — rewind before replaying,
|
|
224
|
+
// matching the macOS bridge's ended-restart.
|
|
225
|
+
player.seekToTime(CMTimeMakeWithSeconds(0, 600))
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
player.play()
|
|
229
|
+
state = 'playing'
|
|
230
|
+
updateNowPlaying()
|
|
231
|
+
emit()
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const pause = async () => {
|
|
235
|
+
// An explicit pause must not be auto-resumed by a pending interruption
|
|
236
|
+
// Ended notification.
|
|
237
|
+
wasPlayingBeforeInterruption = false
|
|
238
|
+
player?.pause()
|
|
239
|
+
state = 'paused'
|
|
240
|
+
updateNowPlaying()
|
|
241
|
+
emit()
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const addRemoteTarget = (command: any, handler: (event?: any) => void) => {
|
|
245
|
+
const token = command.addTargetWithHandler((event: any) => {
|
|
246
|
+
handler(event)
|
|
247
|
+
return (globalThis as any).MPRemoteCommandHandlerStatus.Success
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
remoteTargets.push({ command, token })
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (commandCenter) {
|
|
254
|
+
addRemoteTarget(commandCenter.playCommand, () => void play())
|
|
255
|
+
addRemoteTarget(commandCenter.pauseCommand, () => void pause())
|
|
256
|
+
addRemoteTarget(commandCenter.nextTrackCommand, () => void advance(1))
|
|
257
|
+
addRemoteTarget(commandCenter.previousTrackCommand, () => void advance(-1))
|
|
258
|
+
addRemoteTarget(commandCenter.changePlaybackPositionCommand, (event) => {
|
|
259
|
+
if (player && Number.isFinite(event.positionTime)) {
|
|
260
|
+
player.seekToTime(CMTimeMakeWithSeconds(Math.max(0, event.positionTime), 600))
|
|
261
|
+
updateNowPlaying()
|
|
262
|
+
emit()
|
|
263
|
+
}
|
|
264
|
+
})
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
interruptionObserver = NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock(
|
|
268
|
+
AVAudioSessionInterruptionNotification,
|
|
269
|
+
null,
|
|
270
|
+
NSOperationQueue.mainQueue,
|
|
271
|
+
(notification) => {
|
|
272
|
+
const type = notification.userInfo?.objectForKey(
|
|
273
|
+
AVAudioSessionInterruptionTypeKey,
|
|
274
|
+
)?.unsignedIntegerValue
|
|
275
|
+
|
|
276
|
+
if (type === interruptionBegan) {
|
|
277
|
+
wasPlayingBeforeInterruption = state === 'playing'
|
|
278
|
+
player?.pause()
|
|
279
|
+
state = 'paused'
|
|
280
|
+
emit()
|
|
281
|
+
} else if (type === interruptionEnded && wasPlayingBeforeInterruption) {
|
|
282
|
+
const options =
|
|
283
|
+
notification.userInfo?.objectForKey(AVAudioSessionInterruptionOptionKey)
|
|
284
|
+
?.unsignedIntegerValue ?? 0
|
|
285
|
+
|
|
286
|
+
wasPlayingBeforeInterruption = false
|
|
287
|
+
if ((options & interruptionShouldResume) !== 0) {
|
|
288
|
+
void play()
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
return {
|
|
295
|
+
capabilities: (): AudioCapabilities => ({
|
|
296
|
+
supported: true,
|
|
297
|
+
backgroundPlayback: true,
|
|
298
|
+
systemControls: hasMediaPlayer,
|
|
299
|
+
userGestureRequired: false,
|
|
300
|
+
interruptions: true,
|
|
301
|
+
}),
|
|
302
|
+
snapshot,
|
|
303
|
+
subscribe: (listener) => {
|
|
304
|
+
listeners.add(listener)
|
|
305
|
+
listener(snapshot())
|
|
306
|
+
timer ??= setInterval(() => {
|
|
307
|
+
if (
|
|
308
|
+
state !== 'error' &&
|
|
309
|
+
(player?.status === statusFailed || player?.currentItem?.status === statusFailed)
|
|
310
|
+
) {
|
|
311
|
+
error = playerError()
|
|
312
|
+
state = 'error'
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
updateNowPlaying()
|
|
316
|
+
emit()
|
|
317
|
+
}, 500)
|
|
318
|
+
|
|
319
|
+
return () => {
|
|
320
|
+
listeners.delete(listener)
|
|
321
|
+
if (!listeners.size && timer) {
|
|
322
|
+
clearInterval(timer)
|
|
323
|
+
timer = undefined
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
},
|
|
327
|
+
setQueue: async (tracks, startAt = 0) => {
|
|
328
|
+
if (disposed) {
|
|
329
|
+
throw new Error('AudioPlayer is disposed')
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
queue = [...tracks]
|
|
333
|
+
index = queue.length
|
|
334
|
+
? Math.max(
|
|
335
|
+
0,
|
|
336
|
+
Math.min(Number.isFinite(startAt) ? Math.floor(startAt) : 0, queue.length - 1),
|
|
337
|
+
)
|
|
338
|
+
: -1
|
|
339
|
+
|
|
340
|
+
await load()
|
|
341
|
+
},
|
|
342
|
+
play,
|
|
343
|
+
pause,
|
|
344
|
+
seek: async (value) => {
|
|
345
|
+
if (!player || !Number.isFinite(value)) {
|
|
346
|
+
return
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
player.seekToTime(CMTimeMakeWithSeconds(Math.max(0, value), 600))
|
|
350
|
+
updateNowPlaying()
|
|
351
|
+
emit()
|
|
352
|
+
},
|
|
353
|
+
dispose: () => {
|
|
354
|
+
if (disposed) {
|
|
355
|
+
return
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
disposed = true
|
|
359
|
+
clearTrack()
|
|
360
|
+
if (interruptionObserver) {
|
|
361
|
+
NSNotificationCenter.defaultCenter.removeObserver(interruptionObserver)
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
interruptionObserver = undefined
|
|
365
|
+
for (const { command, token } of remoteTargets) {
|
|
366
|
+
command.removeTarget(token)
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
remoteTargets.length = 0
|
|
370
|
+
if (timer) {
|
|
371
|
+
clearInterval(timer)
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
timer = undefined
|
|
375
|
+
if (nowPlaying) {
|
|
376
|
+
nowPlaying.nowPlayingInfo = null as unknown as NSDictionary<string, any>
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// The Objective-C error-out argument is intentionally null at runtime;
|
|
380
|
+
// NativeScript's generated type only permits Reference<NSError> | undefined.
|
|
381
|
+
const noErrorOut = null as unknown as Parameters<typeof session.setActiveWithOptionsError>[2]
|
|
382
|
+
|
|
383
|
+
session.setActiveWithOptionsError(false, notifyOthersOnDeactivation, noErrorOut)
|
|
384
|
+
|
|
385
|
+
listeners.clear()
|
|
386
|
+
queue = []
|
|
387
|
+
index = -1
|
|
388
|
+
state = 'idle'
|
|
389
|
+
},
|
|
390
|
+
}
|
|
391
|
+
}
|