react-native-nitro-player 0.3.0-alpha.7 → 0.3.0-alpha.8
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 +20 -0
- package/lib/hooks/index.d.ts +1 -0
- package/lib/hooks/index.js +1 -0
- package/lib/hooks/useNowPlaying.d.ts +36 -0
- package/lib/hooks/useNowPlaying.js +79 -0
- package/package.json +1 -1
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useNowPlaying.ts +84 -0
package/README.md
CHANGED
|
@@ -171,6 +171,21 @@ Automatically polls for audio device changes every 2 seconds.
|
|
|
171
171
|
|
|
172
172
|
- `devices: TAudioDevice[]` - Array of available audio devices
|
|
173
173
|
|
|
174
|
+
### `useNowPlaying()`
|
|
175
|
+
|
|
176
|
+
Returns the complete current player state (same as `TrackPlayer.getState()`). This hook provides all player information in a single object and automatically updates when the player state changes.
|
|
177
|
+
|
|
178
|
+
**Returns:**
|
|
179
|
+
|
|
180
|
+
- `PlayerState` object containing:
|
|
181
|
+
- `currentTrack: TrackItem | null` - The current track being played, or `null` if no track is playing
|
|
182
|
+
- `totalDuration: number` - Total duration of the current track in seconds
|
|
183
|
+
- `currentState: TrackPlayerState` - Current playback state (`'playing'`, `'paused'`, or `'stopped'`)
|
|
184
|
+
- `currentPlaylistId: string | null` - ID of the currently loaded playlist, or `null` if no playlist is loaded
|
|
185
|
+
- `currentIndex: number` - Index of the current track in the playlist (-1 if no track is playing)
|
|
186
|
+
|
|
187
|
+
**Note:** This hook is equivalent to calling `TrackPlayer.getState()` but provides reactive updates. It listens to track changes and playback state changes to update automatically. Also dont rely on progress from this hook
|
|
188
|
+
|
|
174
189
|
## Audio Device APIs
|
|
175
190
|
|
|
176
191
|
### `AudioDevices` (Android only)
|
|
@@ -347,6 +362,9 @@ function PlayerComponent() {
|
|
|
347
362
|
// Check Android Auto connection
|
|
348
363
|
const { isConnected } = useAndroidAutoConnection()
|
|
349
364
|
|
|
365
|
+
// Get complete player state (alternative to individual hooks)
|
|
366
|
+
const nowPlaying = useNowPlaying()
|
|
367
|
+
|
|
350
368
|
return (
|
|
351
369
|
<View>
|
|
352
370
|
{track && (
|
|
@@ -354,6 +372,8 @@ function PlayerComponent() {
|
|
|
354
372
|
)}
|
|
355
373
|
<Text>State: {state}</Text>
|
|
356
374
|
<Text>Progress: {position} / {totalDuration}</Text>
|
|
375
|
+
{/* Or use useNowPlaying for all state at once */}
|
|
376
|
+
<Text>Now Playing State: {nowPlaying.currentState}</Text>
|
|
357
377
|
</View>
|
|
358
378
|
)
|
|
359
379
|
}
|
package/lib/hooks/index.d.ts
CHANGED
|
@@ -4,3 +4,4 @@ export { useOnSeek } from './useOnSeek';
|
|
|
4
4
|
export { useOnPlaybackProgressChange } from './useOnPlaybackProgressChange';
|
|
5
5
|
export { useAndroidAutoConnection } from './useAndroidAutoConnection';
|
|
6
6
|
export { useAudioDevices } from './useAudioDevices';
|
|
7
|
+
export { useNowPlaying } from './useNowPlaying';
|
package/lib/hooks/index.js
CHANGED
|
@@ -4,3 +4,4 @@ export { useOnSeek } from './useOnSeek';
|
|
|
4
4
|
export { useOnPlaybackProgressChange } from './useOnPlaybackProgressChange';
|
|
5
5
|
export { useAndroidAutoConnection } from './useAndroidAutoConnection';
|
|
6
6
|
export { useAudioDevices } from './useAudioDevices';
|
|
7
|
+
export { useNowPlaying } from './useNowPlaying';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { PlayerState } from '../types/PlayerQueue';
|
|
2
|
+
/**
|
|
3
|
+
* Hook to get the current player state (same as TrackPlayer.getState())
|
|
4
|
+
*
|
|
5
|
+
* This hook provides all player state information including:
|
|
6
|
+
* - Current track
|
|
7
|
+
* - Current position and duration
|
|
8
|
+
* - Playback state (playing, paused, stopped)
|
|
9
|
+
* - Current playlist ID
|
|
10
|
+
* - Current track index
|
|
11
|
+
*
|
|
12
|
+
* The hook polls getState() periodically and also listens to events
|
|
13
|
+
* for immediate updates when state changes.
|
|
14
|
+
*
|
|
15
|
+
* @returns PlayerState object with all current player information
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* function PlayerComponent() {
|
|
20
|
+
* const state = useNowPlaying()
|
|
21
|
+
*
|
|
22
|
+
* return (
|
|
23
|
+
* <View>
|
|
24
|
+
* {state.currentTrack && (
|
|
25
|
+
* <Text>Now Playing: {state.currentTrack.title}</Text>
|
|
26
|
+
* )}
|
|
27
|
+
* <Text>Position: {state.currentPosition} / {state.totalDuration}</Text>
|
|
28
|
+
* <Text>State: {state.currentState}</Text>
|
|
29
|
+
* <Text>Playlist: {state.currentPlaylistId || 'None'}</Text>
|
|
30
|
+
* <Text>Index: {state.currentIndex}</Text>
|
|
31
|
+
* </View>
|
|
32
|
+
* )
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function useNowPlaying(): PlayerState;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { TrackPlayer } from '../index';
|
|
3
|
+
/**
|
|
4
|
+
* Hook to get the current player state (same as TrackPlayer.getState())
|
|
5
|
+
*
|
|
6
|
+
* This hook provides all player state information including:
|
|
7
|
+
* - Current track
|
|
8
|
+
* - Current position and duration
|
|
9
|
+
* - Playback state (playing, paused, stopped)
|
|
10
|
+
* - Current playlist ID
|
|
11
|
+
* - Current track index
|
|
12
|
+
*
|
|
13
|
+
* The hook polls getState() periodically and also listens to events
|
|
14
|
+
* for immediate updates when state changes.
|
|
15
|
+
*
|
|
16
|
+
* @returns PlayerState object with all current player information
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```tsx
|
|
20
|
+
* function PlayerComponent() {
|
|
21
|
+
* const state = useNowPlaying()
|
|
22
|
+
*
|
|
23
|
+
* return (
|
|
24
|
+
* <View>
|
|
25
|
+
* {state.currentTrack && (
|
|
26
|
+
* <Text>Now Playing: {state.currentTrack.title}</Text>
|
|
27
|
+
* )}
|
|
28
|
+
* <Text>Position: {state.currentPosition} / {state.totalDuration}</Text>
|
|
29
|
+
* <Text>State: {state.currentState}</Text>
|
|
30
|
+
* <Text>Playlist: {state.currentPlaylistId || 'None'}</Text>
|
|
31
|
+
* <Text>Index: {state.currentIndex}</Text>
|
|
32
|
+
* </View>
|
|
33
|
+
* )
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function useNowPlaying() {
|
|
38
|
+
const [state, setState] = useState(() => {
|
|
39
|
+
// Get initial state
|
|
40
|
+
try {
|
|
41
|
+
return TrackPlayer.getState();
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
console.error('Error getting initial player state:', error);
|
|
45
|
+
// Return default state
|
|
46
|
+
return {
|
|
47
|
+
currentTrack: null,
|
|
48
|
+
currentPosition: 0,
|
|
49
|
+
totalDuration: 0,
|
|
50
|
+
currentState: 'stopped',
|
|
51
|
+
currentPlaylistId: null,
|
|
52
|
+
currentIndex: -1,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
// Update state function
|
|
58
|
+
const updateState = () => {
|
|
59
|
+
try {
|
|
60
|
+
const newState = TrackPlayer.getState();
|
|
61
|
+
setState(newState);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
console.error('Error updating player state:', error);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
// Get initial state
|
|
68
|
+
updateState();
|
|
69
|
+
// Listen to track changes
|
|
70
|
+
TrackPlayer.onChangeTrack(() => {
|
|
71
|
+
updateState();
|
|
72
|
+
});
|
|
73
|
+
// Listen to playback state changes
|
|
74
|
+
TrackPlayer.onPlaybackStateChange(() => {
|
|
75
|
+
updateState();
|
|
76
|
+
});
|
|
77
|
+
}, []);
|
|
78
|
+
return state;
|
|
79
|
+
}
|
package/package.json
CHANGED
package/src/hooks/index.ts
CHANGED
|
@@ -4,3 +4,4 @@ export { useOnSeek } from './useOnSeek'
|
|
|
4
4
|
export { useOnPlaybackProgressChange } from './useOnPlaybackProgressChange'
|
|
5
5
|
export { useAndroidAutoConnection } from './useAndroidAutoConnection'
|
|
6
6
|
export { useAudioDevices } from './useAudioDevices'
|
|
7
|
+
export { useNowPlaying } from './useNowPlaying'
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
import { TrackPlayer } from '../index'
|
|
3
|
+
import type { PlayerState } from '../types/PlayerQueue'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook to get the current player state (same as TrackPlayer.getState())
|
|
7
|
+
*
|
|
8
|
+
* This hook provides all player state information including:
|
|
9
|
+
* - Current track
|
|
10
|
+
* - Current position and duration
|
|
11
|
+
* - Playback state (playing, paused, stopped)
|
|
12
|
+
* - Current playlist ID
|
|
13
|
+
* - Current track index
|
|
14
|
+
*
|
|
15
|
+
* The hook polls getState() periodically and also listens to events
|
|
16
|
+
* for immediate updates when state changes.
|
|
17
|
+
*
|
|
18
|
+
* @returns PlayerState object with all current player information
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* function PlayerComponent() {
|
|
23
|
+
* const state = useNowPlaying()
|
|
24
|
+
*
|
|
25
|
+
* return (
|
|
26
|
+
* <View>
|
|
27
|
+
* {state.currentTrack && (
|
|
28
|
+
* <Text>Now Playing: {state.currentTrack.title}</Text>
|
|
29
|
+
* )}
|
|
30
|
+
* <Text>Position: {state.currentPosition} / {state.totalDuration}</Text>
|
|
31
|
+
* <Text>State: {state.currentState}</Text>
|
|
32
|
+
* <Text>Playlist: {state.currentPlaylistId || 'None'}</Text>
|
|
33
|
+
* <Text>Index: {state.currentIndex}</Text>
|
|
34
|
+
* </View>
|
|
35
|
+
* )
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function useNowPlaying(): PlayerState {
|
|
40
|
+
const [state, setState] = useState<PlayerState>(() => {
|
|
41
|
+
// Get initial state
|
|
42
|
+
try {
|
|
43
|
+
return TrackPlayer.getState()
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.error('Error getting initial player state:', error)
|
|
46
|
+
// Return default state
|
|
47
|
+
return {
|
|
48
|
+
currentTrack: null,
|
|
49
|
+
currentPosition: 0,
|
|
50
|
+
totalDuration: 0,
|
|
51
|
+
currentState: 'stopped',
|
|
52
|
+
currentPlaylistId: null,
|
|
53
|
+
currentIndex: -1,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
// Update state function
|
|
60
|
+
const updateState = () => {
|
|
61
|
+
try {
|
|
62
|
+
const newState = TrackPlayer.getState()
|
|
63
|
+
setState(newState)
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.error('Error updating player state:', error)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Get initial state
|
|
70
|
+
updateState()
|
|
71
|
+
|
|
72
|
+
// Listen to track changes
|
|
73
|
+
TrackPlayer.onChangeTrack(() => {
|
|
74
|
+
updateState()
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
// Listen to playback state changes
|
|
78
|
+
TrackPlayer.onPlaybackStateChange(() => {
|
|
79
|
+
updateState()
|
|
80
|
+
})
|
|
81
|
+
}, [])
|
|
82
|
+
|
|
83
|
+
return state
|
|
84
|
+
}
|