@javascriptcommon/react-native-track-player 4.1.8 → 4.1.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@javascriptcommon/react-native-track-player",
3
- "version": "4.1.8",
3
+ "version": "4.1.9",
4
4
  "description": "A fully fledged audio module created for music apps",
5
5
  "main": "lib/src/index.js",
6
6
  "types": "lib/src/index.d.ts",
@@ -31,7 +31,6 @@
31
31
  "android/src/**/*",
32
32
  "android/build.gradle",
33
33
  "android/proguard-rules.txt",
34
- "web/**/*",
35
34
  "*.podspec",
36
35
  "specs"
37
36
  ],
@@ -1,2 +0,0 @@
1
- import TrackPlayerModule from '../web';
2
- export default TrackPlayerModule;
@@ -1,2 +0,0 @@
1
- import TrackPlayerModule from '../web';
2
- export default TrackPlayerModule;
@@ -1,2 +0,0 @@
1
- declare const resolveAssetResource: (base64: unknown) => unknown;
2
- export default resolveAssetResource;
@@ -1,8 +0,0 @@
1
- const resolveAssetResource = (base64) => {
2
- if (/^https?:\/\//.test(base64)) {
3
- return base64;
4
- }
5
- // TODO: resolveAssetResource for web
6
- return base64;
7
- };
8
- export default resolveAssetResource;
@@ -1,40 +0,0 @@
1
- import { State } from '../../src/constants/State';
2
- import type { Track, Progress, PlaybackState } from '../../src/interfaces';
3
- export declare class Player {
4
- protected hasInitialized: boolean;
5
- protected element?: HTMLMediaElement;
6
- protected player?: shaka.Player;
7
- protected _current?: Track;
8
- protected _playWhenReady: boolean;
9
- protected _state: PlaybackState;
10
- get current(): Track | undefined;
11
- set current(cur: Track | undefined);
12
- get state(): PlaybackState;
13
- set state(newState: PlaybackState);
14
- get playWhenReady(): boolean;
15
- set playWhenReady(pwr: boolean);
16
- setupPlayer(): Promise<void>;
17
- /**
18
- * event handlers
19
- */
20
- protected onStateUpdate(state: Exclude<State, State.Error>): void;
21
- protected onError(error: any): void;
22
- /**
23
- * player control
24
- */
25
- load(track: Track): Promise<void>;
26
- retry(): Promise<void>;
27
- stop(): Promise<void>;
28
- play(): Promise<void>;
29
- pause(): void;
30
- setRate(rate: number): number;
31
- getRate(): number;
32
- seekBy(offset: number): void;
33
- seekTo(seconds: number): void;
34
- setVolume(volume: number): void;
35
- getVolume(): number;
36
- getDuration(): number;
37
- getPosition(): number;
38
- getProgress(): Progress;
39
- getBufferedPosition(): (index: number) => number;
40
- }
@@ -1,188 +0,0 @@
1
- import { State } from '../../src/constants/State';
2
- import { SetupNotCalledError } from './SetupNotCalledError';
3
- export class Player {
4
- hasInitialized = false;
5
- element;
6
- player;
7
- _current = undefined;
8
- _playWhenReady = false;
9
- _state = { state: State.None };
10
- // current getter/setter
11
- get current() {
12
- return this._current;
13
- }
14
- set current(cur) {
15
- this._current = cur;
16
- }
17
- // state getter/setter
18
- get state() {
19
- return this._state;
20
- }
21
- set state(newState) {
22
- this._state = newState;
23
- }
24
- // playWhenReady getter/setter
25
- get playWhenReady() {
26
- return this._playWhenReady;
27
- }
28
- set playWhenReady(pwr) {
29
- this._playWhenReady = pwr;
30
- }
31
- async setupPlayer() {
32
- // shaka only runs in a browser
33
- if (typeof window === 'undefined')
34
- return;
35
- if (this.hasInitialized === true) {
36
- // TODO: double check the structure of this error message
37
- throw { code: 'player_already_initialized', message: 'The player has already been initialized via setupPlayer.' };
38
- }
39
- // @ts-ignore
40
- const shaka = (await import('shaka-player/dist/shaka-player.ui')).default;
41
- // Install built-in polyfills to patch browser incompatibilities.
42
- shaka.polyfill.installAll();
43
- // Check to see if the browser supports the basic APIs Shaka needs.
44
- if (!shaka.Player.isBrowserSupported()) {
45
- // This browser does not have the minimum set of APIs we need.
46
- this.state = {
47
- state: State.Error,
48
- error: {
49
- code: 'not_supported',
50
- message: 'Browser not supported.',
51
- },
52
- };
53
- throw new Error('Browser not supported.');
54
- }
55
- // build dom element and attach shaka-player
56
- this.element = document.createElement('audio');
57
- this.element.setAttribute('id', 'react-native-track-player');
58
- this.player = new shaka.Player();
59
- this.player?.attach(this.element);
60
- // Listen for relevant events events.
61
- this.player.addEventListener('error', (error) => {
62
- // Extract the shaka.util.Error object from the event.
63
- this.onError(error.detail);
64
- });
65
- this.element.addEventListener('ended', this.onStateUpdate.bind(this, State.Ended));
66
- this.element.addEventListener('playing', this.onStateUpdate.bind(this, State.Playing));
67
- this.element.addEventListener('pause', this.onStateUpdate.bind(this, State.Paused));
68
- this.player.addEventListener('loading', this.onStateUpdate.bind(this, State.Loading));
69
- this.player.addEventListener('loaded', this.onStateUpdate.bind(this, State.Ready));
70
- this.player.addEventListener('buffering', ({ buffering }) => {
71
- if (buffering === true) {
72
- this.onStateUpdate(State.Buffering);
73
- }
74
- });
75
- // Attach player to the window to make it easy to access in the JS console.
76
- // @ts-ignore
77
- window.rntp = this.player;
78
- this.hasInitialized = true;
79
- }
80
- /**
81
- * event handlers
82
- */
83
- onStateUpdate(state) {
84
- this.state = { state };
85
- }
86
- onError(error) {
87
- // unload the current track to allow for clean playback on other
88
- this.player?.unload();
89
- this.state = {
90
- state: State.Error,
91
- error: {
92
- code: error.code.toString(),
93
- message: error.message,
94
- },
95
- };
96
- // Log the error.
97
- console.debug('Error code', error.code, 'object', error);
98
- }
99
- /**
100
- * player control
101
- */
102
- async load(track) {
103
- if (!this.player)
104
- throw new SetupNotCalledError();
105
- await this.player.load(track.url);
106
- this.current = track;
107
- }
108
- async retry() {
109
- if (!this.player)
110
- throw new SetupNotCalledError();
111
- this.player.retryStreaming();
112
- }
113
- async stop() {
114
- if (!this.player)
115
- throw new SetupNotCalledError();
116
- this.current = undefined;
117
- await this.player.unload();
118
- }
119
- play() {
120
- if (!this.element)
121
- throw new SetupNotCalledError();
122
- this.playWhenReady = true;
123
- return this.element.play()
124
- .catch(err => {
125
- console.error(err);
126
- });
127
- }
128
- pause() {
129
- if (!this.element)
130
- throw new SetupNotCalledError();
131
- this.playWhenReady = false;
132
- return this.element.pause();
133
- }
134
- setRate(rate) {
135
- if (!this.element)
136
- throw new SetupNotCalledError();
137
- return this.element.playbackRate = rate;
138
- }
139
- getRate() {
140
- if (!this.element)
141
- throw new SetupNotCalledError();
142
- return this.element.playbackRate;
143
- }
144
- seekBy(offset) {
145
- if (!this.element)
146
- throw new SetupNotCalledError();
147
- this.element.currentTime += offset;
148
- }
149
- seekTo(seconds) {
150
- if (!this.element)
151
- throw new SetupNotCalledError();
152
- this.element.currentTime = seconds;
153
- }
154
- setVolume(volume) {
155
- if (!this.element)
156
- throw new SetupNotCalledError();
157
- this.element.volume = volume;
158
- }
159
- getVolume() {
160
- if (!this.element)
161
- throw new SetupNotCalledError();
162
- return this.element.volume;
163
- }
164
- getDuration() {
165
- if (!this.element)
166
- throw new SetupNotCalledError();
167
- return this.element.duration;
168
- }
169
- getPosition() {
170
- if (!this.element)
171
- throw new SetupNotCalledError();
172
- return this.element.currentTime;
173
- }
174
- getProgress() {
175
- if (!this.element)
176
- throw new SetupNotCalledError();
177
- return {
178
- position: this.element.currentTime,
179
- duration: this.element.duration || 0,
180
- buffered: 0, // TODO: this.element.buffered.end,
181
- };
182
- }
183
- getBufferedPosition() {
184
- if (!this.element)
185
- throw new SetupNotCalledError();
186
- return this.element.buffered.end;
187
- }
188
- }
@@ -1,31 +0,0 @@
1
- import { Player } from './Player';
2
- import type { Track } from '../../src/interfaces';
3
- import { RepeatMode } from './RepeatMode';
4
- import { State } from '../../src';
5
- export declare class PlaylistPlayer extends Player {
6
- protected playlist: Track[];
7
- protected lastIndex?: number;
8
- protected _currentIndex?: number;
9
- protected repeatMode: RepeatMode;
10
- protected onStateUpdate(state: Exclude<State, State.Error>): Promise<void>;
11
- protected onTrackEnded(): Promise<void>;
12
- protected onPlaylistEnded(): void;
13
- protected get currentIndex(): number | undefined;
14
- protected set currentIndex(current: number | undefined);
15
- protected goToIndex(index: number, initialPosition?: number): Promise<void>;
16
- add(tracks: Track[], insertBeforeIndex?: number): Promise<void>;
17
- skip(index: number, initialPosition?: number): Promise<void>;
18
- skipToNext(initialPosition?: number): Promise<void>;
19
- skipToPrevious(initialPosition?: number): Promise<void>;
20
- getTrack(index: number): Track | null;
21
- setRepeatMode(mode: RepeatMode): void;
22
- getRepeatMode(): RepeatMode;
23
- remove(indexes: number[]): Promise<void>;
24
- stop(): Promise<void>;
25
- reset(): Promise<void>;
26
- removeUpcomingTracks(): Promise<void>;
27
- move(fromIndex: number, toIndex: number): Promise<void>;
28
- updateMetadataForTrack(index: number, metadata: Partial<Track>): void;
29
- clearNowPlayingMetadata(): void;
30
- updateNowPlayingMetadata(metadata: Partial<Track>): void;
31
- }
@@ -1,181 +0,0 @@
1
- import { Player } from './Player';
2
- import { RepeatMode } from './RepeatMode';
3
- import { State } from '../../src';
4
- export class PlaylistPlayer extends Player {
5
- // TODO: use immer to make the `playlist` immutable
6
- playlist = [];
7
- lastIndex;
8
- _currentIndex;
9
- repeatMode = RepeatMode.Off;
10
- async onStateUpdate(state) {
11
- super.onStateUpdate(state);
12
- if (state === State.Ended) {
13
- await this.onTrackEnded();
14
- }
15
- }
16
- async onTrackEnded() {
17
- switch (this.repeatMode) {
18
- case RepeatMode.Track:
19
- if (this.currentIndex !== undefined) {
20
- await this.goToIndex(this.currentIndex);
21
- }
22
- break;
23
- case RepeatMode.Playlist:
24
- if (this.currentIndex === this.playlist.length - 1) {
25
- await this.goToIndex(0);
26
- }
27
- else {
28
- await this.skipToNext();
29
- }
30
- break;
31
- default:
32
- try {
33
- await this.skipToNext();
34
- }
35
- catch (err) {
36
- if (err.message !== 'playlist_exhausted') {
37
- throw err;
38
- }
39
- this.onPlaylistEnded();
40
- }
41
- break;
42
- }
43
- }
44
- // eslint-disable-next-line @typescript-eslint/no-empty-function
45
- onPlaylistEnded() { }
46
- get currentIndex() {
47
- return this._currentIndex;
48
- }
49
- set currentIndex(current) {
50
- this.lastIndex = this.currentIndex;
51
- this._currentIndex = current;
52
- }
53
- async goToIndex(index, initialPosition) {
54
- const track = this.playlist[index];
55
- if (!track) {
56
- throw new Error('playlist_exhausted');
57
- }
58
- if (this.currentIndex !== index) {
59
- this.currentIndex = index;
60
- await this.load(track);
61
- }
62
- if (initialPosition) {
63
- this.seekTo(initialPosition);
64
- }
65
- if (this.playWhenReady) {
66
- await this.play();
67
- }
68
- }
69
- async add(tracks, insertBeforeIndex) {
70
- if (insertBeforeIndex !== -1 && insertBeforeIndex !== undefined) {
71
- this.playlist.splice(insertBeforeIndex, 0, ...tracks);
72
- }
73
- else {
74
- this.playlist.push(...tracks);
75
- }
76
- if (this.currentIndex === undefined) {
77
- await this.goToIndex(0);
78
- }
79
- }
80
- async skip(index, initialPosition) {
81
- const track = this.playlist[index];
82
- if (track === undefined) {
83
- throw new Error('index out of bounds');
84
- }
85
- await this.goToIndex(index, initialPosition);
86
- }
87
- async skipToNext(initialPosition) {
88
- if (this.currentIndex === undefined)
89
- return;
90
- const index = this.currentIndex + 1;
91
- await this.goToIndex(index, initialPosition);
92
- }
93
- async skipToPrevious(initialPosition) {
94
- if (this.currentIndex === undefined)
95
- return;
96
- const index = this.currentIndex - 1;
97
- await this.goToIndex(index, initialPosition);
98
- }
99
- getTrack(index) {
100
- const track = this.playlist[index];
101
- return track || null;
102
- }
103
- setRepeatMode(mode) {
104
- this.repeatMode = mode;
105
- }
106
- getRepeatMode() {
107
- return this.repeatMode;
108
- }
109
- async remove(indexes) {
110
- const idxMap = indexes.reduce((acc, elem) => {
111
- acc[elem] = true;
112
- return acc;
113
- }, {});
114
- let isCurrentRemoved = false;
115
- this.playlist = this.playlist.filter((_track, idx) => {
116
- const keep = !idxMap[idx];
117
- if (!keep && idx === this.currentIndex) {
118
- isCurrentRemoved = true;
119
- }
120
- return keep;
121
- });
122
- if (this.currentIndex === undefined) {
123
- return;
124
- }
125
- const hasItems = this.playlist.length > 0;
126
- if (isCurrentRemoved && hasItems) {
127
- await this.goToIndex(this.currentIndex % this.playlist.length);
128
- }
129
- else if (isCurrentRemoved) {
130
- await this.stop();
131
- }
132
- }
133
- async stop() {
134
- await super.stop();
135
- this.currentIndex = undefined;
136
- }
137
- async reset() {
138
- await this.stop();
139
- this.playlist = [];
140
- }
141
- async removeUpcomingTracks() {
142
- if (this.currentIndex === undefined)
143
- return;
144
- this.playlist = this.playlist.slice(0, this.currentIndex + 1);
145
- }
146
- async move(fromIndex, toIndex) {
147
- if (!this.playlist[fromIndex]) {
148
- throw new Error('index out of bounds');
149
- }
150
- if (this.currentIndex === fromIndex) {
151
- throw new Error('you cannot move the currently playing track');
152
- }
153
- if (this.currentIndex === toIndex) {
154
- throw new Error('you cannot replace the currently playing track');
155
- }
156
- // calculate `currentIndex` after move
157
- let shift = undefined;
158
- if (this.currentIndex) {
159
- if (fromIndex < this.currentIndex && toIndex > this.currentIndex) {
160
- shift = -1;
161
- }
162
- else if (fromIndex > this.currentIndex && toIndex < this.currentIndex) {
163
- shift = +1;
164
- }
165
- }
166
- // move the track
167
- const fromItem = this.playlist[fromIndex];
168
- this.playlist.splice(fromIndex, 1);
169
- this.playlist.splice(toIndex, 0, fromItem);
170
- if (this.currentIndex && shift) {
171
- this.currentIndex = this.currentIndex + shift;
172
- }
173
- }
174
- // TODO
175
- // eslint-disable-next-line @typescript-eslint/no-empty-function
176
- updateMetadataForTrack(index, metadata) { }
177
- // eslint-disable-next-line @typescript-eslint/no-empty-function
178
- clearNowPlayingMetadata() { }
179
- // eslint-disable-next-line @typescript-eslint/no-empty-function
180
- updateNowPlayingMetadata(metadata) { }
181
- }
@@ -1,5 +0,0 @@
1
- export declare enum RepeatMode {
2
- Off = "REPEAT_OFF",
3
- Track = "REPEAT_TRACK",
4
- Playlist = "REPEAT_PLAYLIST"
5
- }
@@ -1,6 +0,0 @@
1
- export var RepeatMode;
2
- (function (RepeatMode) {
3
- RepeatMode["Off"] = "REPEAT_OFF";
4
- RepeatMode["Track"] = "REPEAT_TRACK";
5
- RepeatMode["Playlist"] = "REPEAT_PLAYLIST";
6
- })(RepeatMode || (RepeatMode = {}));
@@ -1,3 +0,0 @@
1
- export declare class SetupNotCalledError extends Error {
2
- constructor();
3
- }
@@ -1,5 +0,0 @@
1
- export class SetupNotCalledError extends Error {
2
- constructor() {
3
- super('You must call `setupPlayer` prior to interacting with the player.');
4
- }
5
- }
@@ -1,3 +0,0 @@
1
- export * from './Player';
2
- export * from './PlaylistPlayer';
3
- export * from './RepeatMode';
@@ -1,3 +0,0 @@
1
- export * from './Player';
2
- export * from './PlaylistPlayer';
3
- export * from './RepeatMode';
@@ -1,63 +0,0 @@
1
- import { PlaybackState, State } from '../src';
2
- import type { Track, UpdateOptions } from '../src';
3
- import { PlaylistPlayer, RepeatMode } from './TrackPlayer';
4
- export declare class TrackPlayerModule extends PlaylistPlayer {
5
- protected emitter: import("react-native").DeviceEventEmitterStatic;
6
- protected progressUpdateEventInterval: any;
7
- readonly CAPABILITY_PLAY = "CAPABILITY_PLAY";
8
- readonly CAPABILITY_PLAY_FROM_ID = "CAPABILITY_PLAY_FROM_ID";
9
- readonly CAPABILITY_PLAY_FROM_SEARCH = "CAPABILITY_PLAY_FROM_SEARCH";
10
- readonly CAPABILITY_PAUSE = "CAPABILITY_PAUSE";
11
- readonly CAPABILITY_STOP = "CAPABILITY_STOP";
12
- readonly CAPABILITY_SEEK_TO = "CAPABILITY_SEEK_TO";
13
- readonly CAPABILITY_SKIP = "CAPABILITY_SKIP";
14
- readonly CAPABILITY_SKIP_TO_NEXT = "CAPABILITY_SKIP_TO_NEXT";
15
- readonly CAPABILITY_SKIP_TO_PREVIOUS = "CAPABILITY_SKIP_TO_PREVIOUS";
16
- readonly CAPABILITY_JUMP_FORWARD = "CAPABILITY_JUMP_FORWARD";
17
- readonly CAPABILITY_JUMP_BACKWARD = "CAPABILITY_JUMP_BACKWARD";
18
- readonly CAPABILITY_SET_RATING = "CAPABILITY_SET_RATING";
19
- readonly CAPABILITY_LIKE = "CAPABILITY_LIKE";
20
- readonly CAPABILITY_DISLIKE = "CAPABILITY_DISLIKE";
21
- readonly CAPABILITY_BOOKMARK = "CAPABILITY_BOOKMARK";
22
- readonly STATE_NONE = "STATE_NONE";
23
- readonly STATE_READY = "STATE_READY";
24
- readonly STATE_PLAYING = "STATE_PLAYING";
25
- readonly STATE_PAUSED = "STATE_PAUSED";
26
- readonly STATE_STOPPED = "STATE_STOPPED";
27
- readonly STATE_BUFFERING = "STATE_BUFFERING";
28
- readonly STATE_CONNECTING = "STATE_CONNECTING";
29
- readonly RATING_HEART = "RATING_HEART";
30
- readonly RATING_THUMBS_UP_DOWN = "RATING_THUMBS_UP_DOWN";
31
- readonly RATING_3_STARS = "RATING_3_STARS";
32
- readonly RATING_4_STARS = "RATING_4_STARS";
33
- readonly RATING_5_STARS = "RATING_5_STARS";
34
- readonly RATING_PERCENTAGE = "RATING_PERCENTAGE";
35
- readonly REPEAT_OFF = RepeatMode.Off;
36
- readonly REPEAT_TRACK = RepeatMode.Track;
37
- readonly REPEAT_QUEUE = RepeatMode.Playlist;
38
- readonly PITCH_ALGORITHM_LINEAR = "PITCH_ALGORITHM_LINEAR";
39
- readonly PITCH_ALGORITHM_MUSIC = "PITCH_ALGORITHM_MUSIC";
40
- readonly PITCH_ALGORITHM_VOICE = "PITCH_ALGORITHM_VOICE";
41
- get state(): PlaybackState;
42
- set state(newState: PlaybackState);
43
- updateOptions(options: UpdateOptions): Promise<void>;
44
- protected setupProgressUpdates(interval?: number): void;
45
- protected clearUpdateEventInterval(): void;
46
- protected onTrackEnded(): Promise<void>;
47
- protected onPlaylistEnded(): Promise<void>;
48
- get playWhenReady(): boolean;
49
- set playWhenReady(pwr: boolean);
50
- getPlayWhenReady(): boolean;
51
- setPlayWhenReady(pwr: boolean): boolean;
52
- load(track: Track): Promise<void>;
53
- getQueue(): Track[];
54
- setQueue(queue: Track[]): Promise<void>;
55
- getActiveTrack(): Track | undefined;
56
- getActiveTrackIndex(): number | undefined;
57
- /**
58
- * @deprecated
59
- * @returns State
60
- */
61
- getState(): State;
62
- getPlaybackState(): PlaybackState;
63
- }