Skip to content

Commit 92a96d6

Browse files
committed
feat(example): get tracks from directory
1 parent aae3cbc commit 92a96d6

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"build:ios": "react-native build-ios --mode Debug"
1111
},
1212
"dependencies": {
13+
"@react-native-documents/picker": "^10.1.3",
1314
"react": "19.0.0",
1415
"react-native": "0.79.2",
1516
"react-native-permissions": "^5.4.0"

example/src/TrackList.tsx

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,30 @@ import {
77
StyleSheet,
88
Image,
99
} from 'react-native';
10-
import { globalStyles } from './Styles';
1110
import { getTracksAsync } from '@nodefinity/react-native-music-library';
1211
import { useState } from 'react';
1312
import type { Track } from '../../src/NativeMusicLibrary';
13+
import { usePermission } from './usePermission';
14+
import { pickDirectory } from '@react-native-documents/picker';
1415

1516
export default function TrackList() {
1617
const [tracks, setTracks] = useState<Track[]>([]);
1718
const [loading, setLoading] = useState(false);
19+
const { permissionStatus, requestPermissions } = usePermission();
1820

1921
const getAllTracks = async () => {
2022
try {
23+
if (!permissionStatus?.granted) {
24+
const result = await requestPermissions();
25+
if (!result.granted) {
26+
Alert.alert(
27+
'Error',
28+
'Can not request permission again, please grant audio permission manually'
29+
);
30+
return;
31+
}
32+
}
33+
2134
setLoading(true);
2235
const startTime = Date.now();
2336
const results = await loadAllTracks();
@@ -76,15 +89,46 @@ export default function TrackList() {
7689
</View>
7790
);
7891

92+
const getTracksFromPickedDirectory = async () => {
93+
try {
94+
const { uri } = await pickDirectory({
95+
requestLongTermAccess: false,
96+
});
97+
98+
console.log('uri', uri);
99+
100+
if (!uri) return;
101+
102+
const results = await getTracksAsync({
103+
first: 100,
104+
directory: uri,
105+
});
106+
107+
Alert.alert(
108+
'Success',
109+
`Picked ${results.items.length} tracks from:\n${uri}`
110+
);
111+
} catch (err) {
112+
Alert.alert('Error', err as string);
113+
} finally {
114+
setLoading(false);
115+
}
116+
};
117+
79118
return (
80119
<>
81-
<View style={globalStyles.buttonContainer}>
82-
<View style={globalStyles.buttonSpacer} />
120+
<View style={styles.buttonContainer}>
83121
<Button
84122
title={`${loading ? 'loading...' : ''} get all tracks`}
85123
onPress={getAllTracks}
86124
disabled={loading}
87125
/>
126+
127+
<Button
128+
title={`${loading ? 'loading...' : ''} get tracks from directory`}
129+
onPress={getTracksFromPickedDirectory}
130+
disabled={loading}
131+
/>
88132
</View>
89133

90134
{tracks.length > 0 && (
@@ -100,6 +144,11 @@ export default function TrackList() {
100144
}
101145

102146
const styles = StyleSheet.create({
147+
buttonContainer: {
148+
width: '100%',
149+
maxWidth: 300,
150+
gap: 10,
151+
},
103152
list: {
104153
flex: 1,
105154
width: '100%',

yarn.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,6 +2859,16 @@ __metadata:
28592859
languageName: node
28602860
linkType: hard
28612861

2862+
"@react-native-documents/picker@npm:^10.1.3":
2863+
version: 10.1.3
2864+
resolution: "@react-native-documents/picker@npm:10.1.3"
2865+
peerDependencies:
2866+
react: "*"
2867+
react-native: "*"
2868+
checksum: 2b1957bf3998a077e3dcf70a2fe8896a2c3c73101d8caef215eafc8525ad12d3380c7d5919752f55a3fa89a9a42f52b98b602e951f4a8a3a96259fba21b19042
2869+
languageName: node
2870+
linkType: hard
2871+
28622872
"@react-native/assets-registry@npm:0.79.2":
28632873
version: 0.79.2
28642874
resolution: "@react-native/assets-registry@npm:0.79.2"
@@ -10589,6 +10599,7 @@ __metadata:
1058910599
"@react-native-community/cli": 18.0.0
1059010600
"@react-native-community/cli-platform-android": 18.0.0
1059110601
"@react-native-community/cli-platform-ios": 18.0.0
10602+
"@react-native-documents/picker": ^10.1.3
1059210603
"@react-native/babel-preset": 0.79.2
1059310604
"@react-native/metro-config": 0.79.2
1059410605
"@react-native/typescript-config": 0.79.2

0 commit comments

Comments
 (0)