react-native-nitro-player 1.6.0-alpha.1 → 1.7.0-alpha.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 +6 -1
- package/android/src/main/java/com/margelo/nitro/nitroplayer/HybridAudioDevices.kt +9 -1
- package/android/src/main/java/com/margelo/nitro/nitroplayer/HybridDownloadManager.kt +23 -3
- package/android/src/main/java/com/margelo/nitro/nitroplayer/HybridEqualizer.kt +23 -3
- package/android/src/main/java/com/margelo/nitro/nitroplayer/HybridPlayerQueue.kt +34 -17
- package/android/src/main/java/com/margelo/nitro/nitroplayer/HybridTrackPlayer.kt +6 -0
- package/android/src/main/java/com/margelo/nitro/nitroplayer/connection/AndroidAutoConnectionDetector.kt +22 -22
- package/android/src/main/java/com/margelo/nitro/nitroplayer/core/TrackPlayerCore.kt +78 -7
- package/android/src/main/java/com/margelo/nitro/nitroplayer/core/TrackPlayerListener.kt +19 -0
- package/android/src/main/java/com/margelo/nitro/nitroplayer/core/TrackPlayerPlayback.kt +33 -19
- package/android/src/main/java/com/margelo/nitro/nitroplayer/core/TrackPlayerQueueBuild.kt +0 -1
- package/android/src/main/java/com/margelo/nitro/nitroplayer/download/DownloadDatabase.kt +9 -5
- package/android/src/main/java/com/margelo/nitro/nitroplayer/download/DownloadManagerCore.kt +96 -56
- package/android/src/main/java/com/margelo/nitro/nitroplayer/download/DownloadWorker.kt +62 -22
- package/android/src/main/java/com/margelo/nitro/nitroplayer/download/TrackItemJson.kt +60 -0
- package/android/src/main/java/com/margelo/nitro/nitroplayer/equalizer/EqualizerCore.kt +9 -9
- package/android/src/main/java/com/margelo/nitro/nitroplayer/media/AuthAwareHttpDataSourceFactory.kt +12 -3
- package/android/src/main/java/com/margelo/nitro/nitroplayer/media/ExoPlayerBuilder.kt +4 -0
- package/android/src/main/java/com/margelo/nitro/nitroplayer/media/MediaSessionManager.kt +99 -0
- package/android/src/main/java/com/margelo/nitro/nitroplayer/media/PlaybackService.kt +22 -1
- package/android/src/main/java/com/margelo/nitro/nitroplayer/playlist/PlaylistManager.kt +85 -70
- package/android/src/main/res/values/strings.xml +7 -0
- package/ios/HybridDownloadManager.swift +13 -3
- package/ios/HybridTrackPlayer.swift +8 -2
- package/ios/core/TrackPlayerCast.swift +32 -1
- package/ios/core/TrackPlayerCore.swift +19 -3
- package/ios/core/TrackPlayerListener.swift +15 -16
- package/ios/core/TrackPlayerNotify.swift +11 -11
- package/ios/core/TrackPlayerPlayback.swift +54 -16
- package/ios/core/TrackPlayerQueueBuild.swift +14 -2
- package/ios/core/TrackPlayerTempQueue.swift +1 -0
- package/ios/core/TrackPlayerUrlLoader.swift +11 -3
- package/ios/download/DownloadDatabase.swift +16 -5
- package/ios/download/DownloadFileManager.swift +12 -15
- package/ios/download/DownloadManagerCore.swift +61 -16
- package/ios/equalizer/EqualizerCore.swift +37 -9
- package/ios/media/MediaSessionManager.swift +89 -4
- package/ios/playlist/PlaylistManager.swift +47 -46
- package/ios/queue/HybridPlayerQueue.swift +28 -7
- package/lib/hooks/callbackManager.d.ts +9 -0
- package/lib/hooks/callbackManager.js +33 -0
- package/lib/hooks/useActualQueue.js +18 -6
- package/lib/hooks/useAndroidAutoConnection.js +2 -4
- package/lib/hooks/useAudioDevices.js +21 -24
- package/lib/hooks/useDownloadActions.js +33 -11
- package/lib/hooks/useDownloadProgress.js +20 -14
- package/lib/hooks/useDownloadStorage.js +1 -1
- package/lib/hooks/useNowPlaying.js +32 -6
- package/lib/hooks/useOnSeek.js +2 -2
- package/lib/hooks/usePlaylist.js +6 -15
- package/lib/specs/TrackPlayer.nitro.d.ts +6 -2
- package/lib/types/PlayerQueue.d.ts +8 -0
- package/nitrogen/generated/android/NitroPlayerOnLoad.cpp +2 -0
- package/nitrogen/generated/android/c++/JFunc_void_TrackItem.hpp +85 -0
- package/nitrogen/generated/android/c++/JHybridTrackPlayerSpec.cpp +5 -0
- package/nitrogen/generated/android/c++/JHybridTrackPlayerSpec.hpp +1 -0
- package/nitrogen/generated/android/c++/JPlayerConfig.hpp +9 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroplayer/Func_void_TrackItem.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroplayer/HybridTrackPlayerSpec.kt +9 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroplayer/PlayerConfig.kt +8 -2
- package/nitrogen/generated/ios/NitroPlayer-Swift-Cxx-Bridge.cpp +8 -0
- package/nitrogen/generated/ios/NitroPlayer-Swift-Cxx-Bridge.hpp +22 -0
- package/nitrogen/generated/ios/c++/HybridTrackPlayerSpecSwift.hpp +6 -0
- package/nitrogen/generated/ios/swift/Func_void_TrackItem.swift +46 -0
- package/nitrogen/generated/ios/swift/HybridTrackPlayerSpec.swift +1 -0
- package/nitrogen/generated/ios/swift/HybridTrackPlayerSpec_cxx.swift +16 -0
- package/nitrogen/generated/ios/swift/PlayerConfig.swift +37 -1
- package/nitrogen/generated/shared/c++/HybridTrackPlayerSpec.cpp +1 -0
- package/nitrogen/generated/shared/c++/HybridTrackPlayerSpec.hpp +1 -0
- package/nitrogen/generated/shared/c++/PlayerConfig.hpp +9 -1
- package/package.json +1 -1
- package/src/hooks/callbackManager.ts +44 -0
- package/src/hooks/useActualQueue.ts +18 -6
- package/src/hooks/useAndroidAutoConnection.ts +6 -6
- package/src/hooks/useAudioDevices.ts +23 -24
- package/src/hooks/useDownloadActions.ts +35 -11
- package/src/hooks/useDownloadProgress.ts +22 -19
- package/src/hooks/useDownloadStorage.ts +4 -3
- package/src/hooks/useNowPlaying.ts +34 -6
- package/src/hooks/useOnSeek.ts +2 -2
- package/src/hooks/usePlaylist.ts +6 -14
- package/src/specs/TrackPlayer.nitro.ts +6 -2
- package/src/types/PlayerQueue.ts +8 -0
- package/android/src/main/java/com/margelo/nitro/nitroplayer/media/NitroPlayerNotificationProvider.kt +0 -200
- package/android/src/main/java/com/margelo/nitro/nitroplayer/queue/Queue.kt +0 -94
- package/android/src/main/java/com/margelo/nitro/nitroplayer/queue/QueueManager.kt +0 -143
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useEffect, useState } from 'react';
|
|
2
2
|
import { TrackPlayer } from '../index';
|
|
3
|
+
import { callbackManager } from './callbackManager';
|
|
3
4
|
/**
|
|
4
5
|
* Hook to detect Android Auto connection status using the official Android for Cars API
|
|
5
6
|
* Based on: https://developer.android.com/training/cars/apps#car-connection
|
|
@@ -13,13 +14,10 @@ import { TrackPlayer } from '../index';
|
|
|
13
14
|
export function useAndroidAutoConnection() {
|
|
14
15
|
const [isConnected, setIsConnected] = useState(false);
|
|
15
16
|
useEffect(() => {
|
|
16
|
-
// Set initial state
|
|
17
17
|
const initialState = TrackPlayer.isAndroidAutoConnected();
|
|
18
18
|
setIsConnected(initialState);
|
|
19
|
-
|
|
20
|
-
TrackPlayer.onAndroidAutoConnectionChange((connected) => {
|
|
19
|
+
return callbackManager.subscribeToAndroidAutoConnection((connected) => {
|
|
21
20
|
setIsConnected(connected);
|
|
22
|
-
console.log('🚗 Android Auto connection changed:', connected);
|
|
23
21
|
});
|
|
24
22
|
}, []);
|
|
25
23
|
return { isConnected };
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { useEffect, useState } from 'react';
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import { AudioDevices } from '../index';
|
|
3
|
+
function devicesEqual(a, b) {
|
|
4
|
+
if (a.length !== b.length)
|
|
5
|
+
return false;
|
|
6
|
+
return a.every((d, i) => d.id === b[i].id && d.isActive === b[i].isActive);
|
|
7
|
+
}
|
|
4
8
|
/**
|
|
5
9
|
* Hook to get audio devices (Android only)
|
|
6
10
|
*
|
|
@@ -26,30 +30,23 @@ import { NitroModules } from 'react-native-nitro-modules';
|
|
|
26
30
|
export function useAudioDevices() {
|
|
27
31
|
const [devices, setDevices] = useState([]);
|
|
28
32
|
useEffect(() => {
|
|
29
|
-
if (
|
|
30
|
-
return undefined;
|
|
31
|
-
}
|
|
32
|
-
try {
|
|
33
|
-
const AudioDevices = NitroModules.createHybridObject('AudioDevices');
|
|
34
|
-
// Get initial devices
|
|
35
|
-
const updateDevices = () => {
|
|
36
|
-
try {
|
|
37
|
-
const currentDevices = AudioDevices.getAudioDevices();
|
|
38
|
-
setDevices(currentDevices);
|
|
39
|
-
}
|
|
40
|
-
catch (error) {
|
|
41
|
-
console.error('Error getting audio devices:', error);
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
updateDevices();
|
|
45
|
-
// Poll for changes every 2 seconds
|
|
46
|
-
const interval = setInterval(updateDevices, 2000);
|
|
47
|
-
return () => clearInterval(interval);
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
console.error('Error setting up audio devices polling:', error);
|
|
33
|
+
if (!AudioDevices) {
|
|
51
34
|
return undefined;
|
|
52
35
|
}
|
|
36
|
+
const updateDevices = () => {
|
|
37
|
+
try {
|
|
38
|
+
const currentDevices = AudioDevices.getAudioDevices();
|
|
39
|
+
// Bail when nothing changed — the bridge returns a fresh array identity
|
|
40
|
+
// every tick, which would otherwise re-render every consumer at 0.5Hz
|
|
41
|
+
setDevices((prev) => devicesEqual(prev, currentDevices) ? prev : currentDevices);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
console.error('Error getting audio devices:', error);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
updateDevices();
|
|
48
|
+
const interval = setInterval(updateDevices, 2000);
|
|
49
|
+
return () => clearInterval(interval);
|
|
53
50
|
}, []);
|
|
54
51
|
return { devices };
|
|
55
52
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useCallback, useState } from 'react';
|
|
1
|
+
import { useCallback, useRef, useState } from 'react';
|
|
2
2
|
import { DownloadManager } from '../index';
|
|
3
3
|
/**
|
|
4
4
|
* Hook for download actions (download, pause, resume, cancel, delete)
|
|
@@ -7,8 +7,30 @@ export function useDownloadActions() {
|
|
|
7
7
|
const [isDownloading, setIsDownloading] = useState(false);
|
|
8
8
|
const [isDeleting, setIsDeleting] = useState(false);
|
|
9
9
|
const [error, setError] = useState(null);
|
|
10
|
-
|
|
10
|
+
// Pending counts: overlapping calls otherwise clear the flag while a
|
|
11
|
+
// second request is still in flight
|
|
12
|
+
const downloadingCount = useRef(0);
|
|
13
|
+
const deletingCount = useRef(0);
|
|
14
|
+
const beginDownloading = useCallback(() => {
|
|
15
|
+
downloadingCount.current += 1;
|
|
11
16
|
setIsDownloading(true);
|
|
17
|
+
}, []);
|
|
18
|
+
const endDownloading = useCallback(() => {
|
|
19
|
+
downloadingCount.current = Math.max(0, downloadingCount.current - 1);
|
|
20
|
+
if (downloadingCount.current === 0)
|
|
21
|
+
setIsDownloading(false);
|
|
22
|
+
}, []);
|
|
23
|
+
const beginDeleting = useCallback(() => {
|
|
24
|
+
deletingCount.current += 1;
|
|
25
|
+
setIsDeleting(true);
|
|
26
|
+
}, []);
|
|
27
|
+
const endDeleting = useCallback(() => {
|
|
28
|
+
deletingCount.current = Math.max(0, deletingCount.current - 1);
|
|
29
|
+
if (deletingCount.current === 0)
|
|
30
|
+
setIsDeleting(false);
|
|
31
|
+
}, []);
|
|
32
|
+
const downloadTrack = useCallback(async (track, playlistId) => {
|
|
33
|
+
beginDownloading();
|
|
12
34
|
setError(null);
|
|
13
35
|
try {
|
|
14
36
|
const downloadId = await DownloadManager.downloadTrack(track, playlistId);
|
|
@@ -19,11 +41,11 @@ export function useDownloadActions() {
|
|
|
19
41
|
throw e;
|
|
20
42
|
}
|
|
21
43
|
finally {
|
|
22
|
-
|
|
44
|
+
endDownloading();
|
|
23
45
|
}
|
|
24
46
|
}, []);
|
|
25
47
|
const downloadPlaylist = useCallback(async (playlistId, tracks) => {
|
|
26
|
-
|
|
48
|
+
beginDownloading();
|
|
27
49
|
setError(null);
|
|
28
50
|
try {
|
|
29
51
|
const downloadIds = await DownloadManager.downloadPlaylist(playlistId, tracks);
|
|
@@ -34,7 +56,7 @@ export function useDownloadActions() {
|
|
|
34
56
|
throw e;
|
|
35
57
|
}
|
|
36
58
|
finally {
|
|
37
|
-
|
|
59
|
+
endDownloading();
|
|
38
60
|
}
|
|
39
61
|
}, []);
|
|
40
62
|
const pauseDownload = useCallback(async (downloadId) => {
|
|
@@ -59,30 +81,30 @@ export function useDownloadActions() {
|
|
|
59
81
|
await DownloadManager.cancelAllDownloads();
|
|
60
82
|
}, []);
|
|
61
83
|
const deleteTrack = useCallback(async (trackId) => {
|
|
62
|
-
|
|
84
|
+
beginDeleting();
|
|
63
85
|
try {
|
|
64
86
|
await DownloadManager.deleteDownloadedTrack(trackId);
|
|
65
87
|
}
|
|
66
88
|
finally {
|
|
67
|
-
|
|
89
|
+
endDeleting();
|
|
68
90
|
}
|
|
69
91
|
}, []);
|
|
70
92
|
const deletePlaylist = useCallback(async (playlistId) => {
|
|
71
|
-
|
|
93
|
+
beginDeleting();
|
|
72
94
|
try {
|
|
73
95
|
await DownloadManager.deleteDownloadedPlaylist(playlistId);
|
|
74
96
|
}
|
|
75
97
|
finally {
|
|
76
|
-
|
|
98
|
+
endDeleting();
|
|
77
99
|
}
|
|
78
100
|
}, []);
|
|
79
101
|
const deleteAll = useCallback(async () => {
|
|
80
|
-
|
|
102
|
+
beginDeleting();
|
|
81
103
|
try {
|
|
82
104
|
await DownloadManager.deleteAllDownloads();
|
|
83
105
|
}
|
|
84
106
|
finally {
|
|
85
|
-
|
|
107
|
+
endDeleting();
|
|
86
108
|
}
|
|
87
109
|
}, []);
|
|
88
110
|
const configure = useCallback((config) => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect, useState, useRef, useCallback } from 'react';
|
|
1
|
+
import { useEffect, useState, useRef, useCallback, useMemo } from 'react';
|
|
2
2
|
import { DownloadManager } from '../index';
|
|
3
3
|
import { downloadCallbackManager } from './downloadCallbackManager';
|
|
4
4
|
/**
|
|
@@ -8,22 +8,24 @@ export function useDownloadProgress(options = {}) {
|
|
|
8
8
|
const { trackIds, downloadIds, activeOnly = false } = options;
|
|
9
9
|
const [progressMap, setProgressMap] = useState(new Map());
|
|
10
10
|
const isMounted = useRef(true);
|
|
11
|
+
// Content keys: callers pass inline arrays, so identity-based deps would
|
|
12
|
+
// resubscribe (and re-fetch over the bridge) on every parent render
|
|
13
|
+
const trackIdsKey = JSON.stringify(trackIds ?? []);
|
|
14
|
+
const downloadIdsKey = JSON.stringify(downloadIds ?? []);
|
|
11
15
|
const shouldTrack = useCallback((progress) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
const ids = JSON.parse(trackIdsKey);
|
|
17
|
+
const dlIds = JSON.parse(downloadIdsKey);
|
|
18
|
+
if (ids.length > 0 && !ids.includes(progress.trackId)) {
|
|
15
19
|
return false;
|
|
16
20
|
}
|
|
17
|
-
if (
|
|
18
|
-
downloadIds.length > 0 &&
|
|
19
|
-
!downloadIds.includes(progress.downloadId)) {
|
|
21
|
+
if (dlIds.length > 0 && !dlIds.includes(progress.downloadId)) {
|
|
20
22
|
return false;
|
|
21
23
|
}
|
|
22
24
|
if (activeOnly && progress.state !== 'downloading') {
|
|
23
25
|
return false;
|
|
24
26
|
}
|
|
25
27
|
return true;
|
|
26
|
-
}, [
|
|
28
|
+
}, [trackIdsKey, downloadIdsKey, activeOnly]);
|
|
27
29
|
useEffect(() => {
|
|
28
30
|
isMounted.current = true;
|
|
29
31
|
// Initialize with current active downloads
|
|
@@ -69,12 +71,16 @@ export function useDownloadProgress(options = {}) {
|
|
|
69
71
|
unsubscribe();
|
|
70
72
|
};
|
|
71
73
|
}, [shouldTrack]);
|
|
72
|
-
const progressList =
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
progressList
|
|
76
|
-
|
|
77
|
-
|
|
74
|
+
const { progressList, overallProgress, isDownloading } = useMemo(() => {
|
|
75
|
+
const list = Array.from(progressMap.values());
|
|
76
|
+
return {
|
|
77
|
+
progressList: list,
|
|
78
|
+
overallProgress: list.length > 0
|
|
79
|
+
? list.reduce((sum, p) => sum + p.progress, 0) / list.length
|
|
80
|
+
: 0,
|
|
81
|
+
isDownloading: list.some((p) => p.state === 'downloading'),
|
|
82
|
+
};
|
|
83
|
+
}, [progressMap]);
|
|
78
84
|
const getProgress = useCallback((trackId) => progressMap.get(trackId), [progressMap]);
|
|
79
85
|
return {
|
|
80
86
|
progressMap,
|
|
@@ -46,7 +46,7 @@ export function useDownloadStorage() {
|
|
|
46
46
|
const formattedAvailable = storageInfo
|
|
47
47
|
? formatBytes(storageInfo.availableSpace)
|
|
48
48
|
: '0 B';
|
|
49
|
-
const usagePercentage = storageInfo
|
|
49
|
+
const usagePercentage = storageInfo && storageInfo.totalSpace > 0
|
|
50
50
|
? (storageInfo.totalDownloadedSize / storageInfo.totalSpace) * 100
|
|
51
51
|
: 0;
|
|
52
52
|
return {
|
|
@@ -47,19 +47,41 @@ const DEFAULT_STATE = {
|
|
|
47
47
|
export function useNowPlaying() {
|
|
48
48
|
const [state, setState] = useState(DEFAULT_STATE);
|
|
49
49
|
const isMounted = useRef(true);
|
|
50
|
+
// Once pushed events carry position, an async getState() snapshot must not
|
|
51
|
+
// overwrite it — a slow snapshot resolving after newer ticks jumped the
|
|
52
|
+
// progress bar backward on play/pause/track transitions.
|
|
53
|
+
const eventsOwnPosition = useRef(false);
|
|
54
|
+
const fetchScheduled = useRef(false);
|
|
50
55
|
const fetchFullState = useCallback(async () => {
|
|
51
56
|
if (!isMounted.current)
|
|
52
57
|
return;
|
|
53
58
|
try {
|
|
54
59
|
const newState = await TrackPlayer.getState();
|
|
55
60
|
if (isMounted.current) {
|
|
56
|
-
setState(
|
|
61
|
+
setState((prev) => eventsOwnPosition.current
|
|
62
|
+
? {
|
|
63
|
+
...newState,
|
|
64
|
+
currentPosition: prev.currentPosition,
|
|
65
|
+
totalDuration: prev.totalDuration,
|
|
66
|
+
}
|
|
67
|
+
: newState);
|
|
57
68
|
}
|
|
58
69
|
}
|
|
59
70
|
catch (error) {
|
|
60
71
|
console.error('[useNowPlaying] Error updating player state:', error);
|
|
61
72
|
}
|
|
62
73
|
}, []);
|
|
74
|
+
// Coalesces the track-change + playback-state burst that fires on one
|
|
75
|
+
// transition into a single getState() round trip
|
|
76
|
+
const requestFullState = useCallback(() => {
|
|
77
|
+
if (fetchScheduled.current)
|
|
78
|
+
return;
|
|
79
|
+
fetchScheduled.current = true;
|
|
80
|
+
queueMicrotask(() => {
|
|
81
|
+
fetchScheduled.current = false;
|
|
82
|
+
fetchFullState();
|
|
83
|
+
});
|
|
84
|
+
}, [fetchFullState]);
|
|
63
85
|
// Initialize with current state
|
|
64
86
|
useEffect(() => {
|
|
65
87
|
isMounted.current = true;
|
|
@@ -68,18 +90,20 @@ export function useNowPlaying() {
|
|
|
68
90
|
isMounted.current = false;
|
|
69
91
|
};
|
|
70
92
|
}, [fetchFullState]);
|
|
71
|
-
// Subscribe to track changes — full refresh
|
|
93
|
+
// Subscribe to track changes — full refresh; the new track's position comes
|
|
94
|
+
// from the snapshot, so release event ownership until ticks resume
|
|
72
95
|
useEffect(() => {
|
|
73
96
|
return callbackManager.subscribeToTrackChange(() => {
|
|
74
|
-
|
|
97
|
+
eventsOwnPosition.current = false;
|
|
98
|
+
requestFullState();
|
|
75
99
|
});
|
|
76
|
-
}, [
|
|
100
|
+
}, [requestFullState]);
|
|
77
101
|
// Subscribe to playback state changes — full refresh
|
|
78
102
|
useEffect(() => {
|
|
79
103
|
return callbackManager.subscribeToPlaybackState(() => {
|
|
80
|
-
|
|
104
|
+
requestFullState();
|
|
81
105
|
});
|
|
82
|
-
}, [
|
|
106
|
+
}, [requestFullState]);
|
|
83
107
|
// Subscribe to progress changes — lightweight position/duration update.
|
|
84
108
|
// Bail out when neither value changed at whole-second resolution: the native tick
|
|
85
109
|
// is sub-second, and a new state object on every tick re-renders every consumer.
|
|
@@ -87,6 +111,7 @@ export function useNowPlaying() {
|
|
|
87
111
|
return callbackManager.subscribeToPlaybackProgressChange((currentPosition, totalDuration) => {
|
|
88
112
|
if (!isMounted.current)
|
|
89
113
|
return;
|
|
114
|
+
eventsOwnPosition.current = true;
|
|
90
115
|
setState((prev) => {
|
|
91
116
|
if (Math.floor(prev.currentPosition) === Math.floor(currentPosition) &&
|
|
92
117
|
prev.totalDuration === totalDuration) {
|
|
@@ -101,6 +126,7 @@ export function useNowPlaying() {
|
|
|
101
126
|
return callbackManager.subscribeToSeek((currentPosition, totalDuration) => {
|
|
102
127
|
if (!isMounted.current)
|
|
103
128
|
return;
|
|
129
|
+
eventsOwnPosition.current = true;
|
|
104
130
|
setState((prev) => ({ ...prev, currentPosition, totalDuration }));
|
|
105
131
|
});
|
|
106
132
|
}, []);
|
package/lib/hooks/useOnSeek.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect, useState } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { callbackManager } from './callbackManager';
|
|
3
3
|
/**
|
|
4
4
|
* Hook to get the last seek event information
|
|
5
5
|
* @returns Object with last seek position and total duration, or undefined if no seek has occurred
|
|
@@ -8,7 +8,7 @@ export function useOnSeek() {
|
|
|
8
8
|
const [position, setPosition] = useState(undefined);
|
|
9
9
|
const [totalDuration, setTotalDuration] = useState(undefined);
|
|
10
10
|
useEffect(() => {
|
|
11
|
-
|
|
11
|
+
return callbackManager.subscribeToSeek((newPosition, newTotalDuration) => {
|
|
12
12
|
setPosition(newPosition);
|
|
13
13
|
setTotalDuration(newTotalDuration);
|
|
14
14
|
});
|
package/lib/hooks/usePlaylist.js
CHANGED
|
@@ -40,7 +40,6 @@ export function usePlaylist() {
|
|
|
40
40
|
const [allTracks, setAllTracks] = useState([]);
|
|
41
41
|
const [isLoading, setIsLoading] = useState(true);
|
|
42
42
|
const isMounted = useRef(true);
|
|
43
|
-
const hasSubscribed = useRef(false);
|
|
44
43
|
// Tracks the last fetched playlist ID so track-change events can skip
|
|
45
44
|
// a full refresh when the playlist itself hasn't changed.
|
|
46
45
|
const lastPlaylistIdRef = useRef(undefined);
|
|
@@ -119,21 +118,13 @@ export function usePlaylist() {
|
|
|
119
118
|
isMounted.current = false;
|
|
120
119
|
};
|
|
121
120
|
}, [refreshPlaylists]);
|
|
122
|
-
// Subscribe to native playlist changes
|
|
121
|
+
// Subscribe to native playlist changes via the shared manager
|
|
123
122
|
useEffect(() => {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
if (isMounted.current) {
|
|
130
|
-
refreshPlaylists();
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
catch (error) {
|
|
135
|
-
console.error('[usePlaylist] Error setting up playlist listener:', error);
|
|
136
|
-
}
|
|
123
|
+
return callbackManager.subscribeToPlaylistsChanged(() => {
|
|
124
|
+
if (isMounted.current) {
|
|
125
|
+
refreshPlaylists();
|
|
126
|
+
}
|
|
127
|
+
});
|
|
137
128
|
}, [refreshPlaylists]);
|
|
138
129
|
// Refresh on track change only if the active playlist ID changed.
|
|
139
130
|
useEffect(() => {
|
|
@@ -46,6 +46,11 @@ export interface TrackPlayer extends HybridObject<{
|
|
|
46
46
|
* updates and ID3 frames emitted while a live stream plays.
|
|
47
47
|
*/
|
|
48
48
|
onTimedMetadata(callback: (metadata: TimedMetadata) => void): void;
|
|
49
|
+
/**
|
|
50
|
+
* Fires when the app was opened by tapping the playback notification,
|
|
51
|
+
* with the track that was playing at tap time. Android only — no-op on iOS.
|
|
52
|
+
*/
|
|
53
|
+
appStartedWithNotification(callback: (track: TrackItem) => void): void;
|
|
49
54
|
onAndroidAutoConnectionChange(callback: (connected: boolean) => void): void;
|
|
50
55
|
isAndroidAutoConnected(): boolean;
|
|
51
56
|
setVolume(volume: number): Promise<void>;
|
|
@@ -87,8 +92,7 @@ export interface TrackPlayer extends HybridObject<{
|
|
|
87
92
|
*/
|
|
88
93
|
onTracksNeedUpdate(callback: (tracks: TrackItem[], lookahead: number) => void): void;
|
|
89
94
|
/**
|
|
90
|
-
*
|
|
91
|
-
* @returns Promise resolving to 0-based index, or -1 if no track playing
|
|
95
|
+
* Set playback speed. Rejects non-finite and non-positive values.
|
|
92
96
|
*/
|
|
93
97
|
setPlaybackSpeed(speed: number): Promise<void>;
|
|
94
98
|
/**
|
|
@@ -48,6 +48,14 @@ export interface PlayerConfig {
|
|
|
48
48
|
androidAutoEnabled?: boolean;
|
|
49
49
|
carPlayEnabled?: boolean;
|
|
50
50
|
showInNotification?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Fixed interval, in seconds, for remote skip-forward controls (default: 15)
|
|
53
|
+
*/
|
|
54
|
+
remoteSkipForwardInterval?: number;
|
|
55
|
+
/**
|
|
56
|
+
* Fixed interval, in seconds, for remote skip-backward controls (default: 15)
|
|
57
|
+
*/
|
|
58
|
+
remoteSkipBackwardInterval?: number;
|
|
51
59
|
/**
|
|
52
60
|
* Number of upcoming tracks to preload URLs for (default: 5)
|
|
53
61
|
* Higher values = more proactive loading, but more network requests
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
#include "JFunc_void_double_double.hpp"
|
|
37
37
|
#include "JFunc_void_double_double_std__optional_bool_.hpp"
|
|
38
38
|
#include "JFunc_void_TimedMetadata.hpp"
|
|
39
|
+
#include "JFunc_void_TrackItem.hpp"
|
|
39
40
|
#include "JFunc_void_std__vector_TrackItem__double.hpp"
|
|
40
41
|
#include "JFunc_void_std__vector_TrackItem__std__vector_TrackItem_.hpp"
|
|
41
42
|
#include <NitroModules/DefaultConstructableObject.hpp>
|
|
@@ -131,6 +132,7 @@ void registerAllNatives() {
|
|
|
131
132
|
margelo::nitro::nitroplayer::JFunc_void_double_double_cxx::registerNatives();
|
|
132
133
|
margelo::nitro::nitroplayer::JFunc_void_double_double_std__optional_bool__cxx::registerNatives();
|
|
133
134
|
margelo::nitro::nitroplayer::JFunc_void_TimedMetadata_cxx::registerNatives();
|
|
135
|
+
margelo::nitro::nitroplayer::JFunc_void_TrackItem_cxx::registerNatives();
|
|
134
136
|
margelo::nitro::nitroplayer::JFunc_void_std__vector_TrackItem__double_cxx::registerNatives();
|
|
135
137
|
margelo::nitro::nitroplayer::JFunc_void_std__vector_TrackItem__std__vector_TrackItem__cxx::registerNatives();
|
|
136
138
|
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// JFunc_void_TrackItem.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <fbjni/fbjni.h>
|
|
11
|
+
#include <functional>
|
|
12
|
+
|
|
13
|
+
#include "TrackItem.hpp"
|
|
14
|
+
#include <functional>
|
|
15
|
+
#include <NitroModules/JNICallable.hpp>
|
|
16
|
+
#include "JTrackItem.hpp"
|
|
17
|
+
#include <string>
|
|
18
|
+
#include <NitroModules/Null.hpp>
|
|
19
|
+
#include <variant>
|
|
20
|
+
#include <optional>
|
|
21
|
+
#include "JVariant_NullType_String.hpp"
|
|
22
|
+
#include <NitroModules/JNull.hpp>
|
|
23
|
+
#include <NitroModules/AnyMap.hpp>
|
|
24
|
+
#include <NitroModules/JAnyMap.hpp>
|
|
25
|
+
|
|
26
|
+
namespace margelo::nitro::nitroplayer {
|
|
27
|
+
|
|
28
|
+
using namespace facebook;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Represents the Java/Kotlin callback `(track: TrackItem) -> Unit`.
|
|
32
|
+
* This can be passed around between C++ and Java/Kotlin.
|
|
33
|
+
*/
|
|
34
|
+
struct JFunc_void_TrackItem: public jni::JavaClass<JFunc_void_TrackItem> {
|
|
35
|
+
public:
|
|
36
|
+
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/nitroplayer/Func_void_TrackItem;";
|
|
37
|
+
|
|
38
|
+
public:
|
|
39
|
+
/**
|
|
40
|
+
* Invokes the function this `JFunc_void_TrackItem` instance holds through JNI.
|
|
41
|
+
*/
|
|
42
|
+
void invoke(const TrackItem& track) const {
|
|
43
|
+
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<JTrackItem> /* track */)>("invoke");
|
|
44
|
+
method(self(), JTrackItem::fromCpp(track));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* An implementation of Func_void_TrackItem that is backed by a C++ implementation (using `std::function<...>`)
|
|
50
|
+
*/
|
|
51
|
+
class JFunc_void_TrackItem_cxx final: public jni::HybridClass<JFunc_void_TrackItem_cxx, JFunc_void_TrackItem> {
|
|
52
|
+
public:
|
|
53
|
+
static jni::local_ref<JFunc_void_TrackItem::javaobject> fromCpp(const std::function<void(const TrackItem& /* track */)>& func) {
|
|
54
|
+
return JFunc_void_TrackItem_cxx::newObjectCxxArgs(func);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public:
|
|
58
|
+
/**
|
|
59
|
+
* Invokes the C++ `std::function<...>` this `JFunc_void_TrackItem_cxx` instance holds.
|
|
60
|
+
*/
|
|
61
|
+
void invoke_cxx(jni::alias_ref<JTrackItem> track) {
|
|
62
|
+
_func(track->toCpp());
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public:
|
|
66
|
+
[[nodiscard]]
|
|
67
|
+
inline const std::function<void(const TrackItem& /* track */)>& getFunction() const {
|
|
68
|
+
return _func;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public:
|
|
72
|
+
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/nitroplayer/Func_void_TrackItem_cxx;";
|
|
73
|
+
static void registerNatives() {
|
|
74
|
+
registerHybrid({makeNativeMethod("invoke_cxx", JFunc_void_TrackItem_cxx::invoke_cxx)});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private:
|
|
78
|
+
explicit JFunc_void_TrackItem_cxx(const std::function<void(const TrackItem& /* track */)>& func): _func(func) { }
|
|
79
|
+
|
|
80
|
+
private:
|
|
81
|
+
friend HybridBase;
|
|
82
|
+
std::function<void(const TrackItem& /* track */)> _func;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
} // namespace margelo::nitro::nitroplayer
|
|
@@ -60,6 +60,7 @@ namespace margelo::nitro::nitroplayer { struct TimedMetadata; }
|
|
|
60
60
|
#include "TimedMetadata.hpp"
|
|
61
61
|
#include "JFunc_void_TimedMetadata.hpp"
|
|
62
62
|
#include "JTimedMetadata.hpp"
|
|
63
|
+
#include "JFunc_void_TrackItem.hpp"
|
|
63
64
|
#include "JFunc_void_bool.hpp"
|
|
64
65
|
#include "JFunc_void_std__vector_TrackItem__double.hpp"
|
|
65
66
|
#include "JFunc_void_std__vector_TrackItem__std__vector_TrackItem_.hpp"
|
|
@@ -329,6 +330,10 @@ namespace margelo::nitro::nitroplayer {
|
|
|
329
330
|
static const auto method = _javaPart->javaClassStatic()->getMethod<void(jni::alias_ref<JFunc_void_TimedMetadata::javaobject> /* callback */)>("onTimedMetadata_cxx");
|
|
330
331
|
method(_javaPart, JFunc_void_TimedMetadata_cxx::fromCpp(callback));
|
|
331
332
|
}
|
|
333
|
+
void JHybridTrackPlayerSpec::appStartedWithNotification(const std::function<void(const TrackItem& /* track */)>& callback) {
|
|
334
|
+
static const auto method = _javaPart->javaClassStatic()->getMethod<void(jni::alias_ref<JFunc_void_TrackItem::javaobject> /* callback */)>("appStartedWithNotification_cxx");
|
|
335
|
+
method(_javaPart, JFunc_void_TrackItem_cxx::fromCpp(callback));
|
|
336
|
+
}
|
|
332
337
|
void JHybridTrackPlayerSpec::onAndroidAutoConnectionChange(const std::function<void(bool /* connected */)>& callback) {
|
|
333
338
|
static const auto method = _javaPart->javaClassStatic()->getMethod<void(jni::alias_ref<JFunc_void_bool::javaobject> /* callback */)>("onAndroidAutoConnectionChange_cxx");
|
|
334
339
|
method(_javaPart, JFunc_void_bool_cxx::fromCpp(callback));
|
|
@@ -73,6 +73,7 @@ namespace margelo::nitro::nitroplayer {
|
|
|
73
73
|
void onSeek(const std::function<void(double /* position */, double /* totalDuration */)>& callback) override;
|
|
74
74
|
void onPlaybackProgressChange(const std::function<void(double /* position */, double /* totalDuration */, std::optional<bool> /* isManuallySeeked */)>& callback) override;
|
|
75
75
|
void onTimedMetadata(const std::function<void(const TimedMetadata& /* metadata */)>& callback) override;
|
|
76
|
+
void appStartedWithNotification(const std::function<void(const TrackItem& /* track */)>& callback) override;
|
|
76
77
|
void onAndroidAutoConnectionChange(const std::function<void(bool /* connected */)>& callback) override;
|
|
77
78
|
bool isAndroidAutoConnected() override;
|
|
78
79
|
std::shared_ptr<Promise<void>> setVolume(double volume) override;
|
|
@@ -38,6 +38,10 @@ namespace margelo::nitro::nitroplayer {
|
|
|
38
38
|
jni::local_ref<jni::JBoolean> carPlayEnabled = this->getFieldValue(fieldCarPlayEnabled);
|
|
39
39
|
static const auto fieldShowInNotification = clazz->getField<jni::JBoolean>("showInNotification");
|
|
40
40
|
jni::local_ref<jni::JBoolean> showInNotification = this->getFieldValue(fieldShowInNotification);
|
|
41
|
+
static const auto fieldRemoteSkipForwardInterval = clazz->getField<jni::JDouble>("remoteSkipForwardInterval");
|
|
42
|
+
jni::local_ref<jni::JDouble> remoteSkipForwardInterval = this->getFieldValue(fieldRemoteSkipForwardInterval);
|
|
43
|
+
static const auto fieldRemoteSkipBackwardInterval = clazz->getField<jni::JDouble>("remoteSkipBackwardInterval");
|
|
44
|
+
jni::local_ref<jni::JDouble> remoteSkipBackwardInterval = this->getFieldValue(fieldRemoteSkipBackwardInterval);
|
|
41
45
|
static const auto fieldLookaheadCount = clazz->getField<jni::JDouble>("lookaheadCount");
|
|
42
46
|
jni::local_ref<jni::JDouble> lookaheadCount = this->getFieldValue(fieldLookaheadCount);
|
|
43
47
|
static const auto fieldAndroidNotificationIcon = clazz->getField<jni::JString>("androidNotificationIcon");
|
|
@@ -46,6 +50,8 @@ namespace margelo::nitro::nitroplayer {
|
|
|
46
50
|
androidAutoEnabled != nullptr ? std::make_optional(static_cast<bool>(androidAutoEnabled->value())) : std::nullopt,
|
|
47
51
|
carPlayEnabled != nullptr ? std::make_optional(static_cast<bool>(carPlayEnabled->value())) : std::nullopt,
|
|
48
52
|
showInNotification != nullptr ? std::make_optional(static_cast<bool>(showInNotification->value())) : std::nullopt,
|
|
53
|
+
remoteSkipForwardInterval != nullptr ? std::make_optional(remoteSkipForwardInterval->value()) : std::nullopt,
|
|
54
|
+
remoteSkipBackwardInterval != nullptr ? std::make_optional(remoteSkipBackwardInterval->value()) : std::nullopt,
|
|
49
55
|
lookaheadCount != nullptr ? std::make_optional(lookaheadCount->value()) : std::nullopt,
|
|
50
56
|
androidNotificationIcon != nullptr ? std::make_optional(androidNotificationIcon->toStdString()) : std::nullopt
|
|
51
57
|
);
|
|
@@ -57,7 +63,7 @@ namespace margelo::nitro::nitroplayer {
|
|
|
57
63
|
*/
|
|
58
64
|
[[maybe_unused]]
|
|
59
65
|
static jni::local_ref<JPlayerConfig::javaobject> fromCpp(const PlayerConfig& value) {
|
|
60
|
-
using JSignature = JPlayerConfig(jni::alias_ref<jni::JBoolean>, jni::alias_ref<jni::JBoolean>, jni::alias_ref<jni::JBoolean>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JString>);
|
|
66
|
+
using JSignature = JPlayerConfig(jni::alias_ref<jni::JBoolean>, jni::alias_ref<jni::JBoolean>, jni::alias_ref<jni::JBoolean>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JDouble>, jni::alias_ref<jni::JString>);
|
|
61
67
|
static const auto clazz = javaClassStatic();
|
|
62
68
|
static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
|
|
63
69
|
return create(
|
|
@@ -65,6 +71,8 @@ namespace margelo::nitro::nitroplayer {
|
|
|
65
71
|
value.androidAutoEnabled.has_value() ? jni::JBoolean::valueOf(value.androidAutoEnabled.value()) : nullptr,
|
|
66
72
|
value.carPlayEnabled.has_value() ? jni::JBoolean::valueOf(value.carPlayEnabled.value()) : nullptr,
|
|
67
73
|
value.showInNotification.has_value() ? jni::JBoolean::valueOf(value.showInNotification.value()) : nullptr,
|
|
74
|
+
value.remoteSkipForwardInterval.has_value() ? jni::JDouble::valueOf(value.remoteSkipForwardInterval.value()) : nullptr,
|
|
75
|
+
value.remoteSkipBackwardInterval.has_value() ? jni::JDouble::valueOf(value.remoteSkipBackwardInterval.value()) : nullptr,
|
|
68
76
|
value.lookaheadCount.has_value() ? jni::JDouble::valueOf(value.lookaheadCount.value()) : nullptr,
|
|
69
77
|
value.androidNotificationIcon.has_value() ? jni::make_jstring(value.androidNotificationIcon.value()) : nullptr
|
|
70
78
|
);
|