Skip to content

Commit c510a11

Browse files
fix(UNT-T28491): unable to decode file android
1 parent 2da83a6 commit c510a11

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

example/src/App.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
useSafeAreaInsets,
3232
} from 'react-native-safe-area-context';
3333
import { Gifs, Icons } from './assets';
34-
import { audioListArray, type ListItem } from './constants';
34+
import { generateAudioList, type ListItem } from './constants';
3535
import stylesheet from './styles';
3636
import { Colors } from './theme';
3737

@@ -209,11 +209,19 @@ const LivePlayerComponent = ({
209209
const AppContainer = () => {
210210
const [shouldScroll, setShouldScroll] = useState<boolean>(true);
211211
const [currentPlaying, setCurrentPlaying] = useState<string>('');
212-
const [list, setList] = useState<ListItem[]>(audioListArray);
212+
const [list, setList] = useState<ListItem[]>([]);
213213

214214
const { top, bottom } = useSafeAreaInsets();
215215
const styles = stylesheet({ top, bottom });
216216

217+
useEffect(() => {
218+
generateAudioList().then(audioListArray => {
219+
if (audioListArray?.length > 0) {
220+
setList(audioListArray);
221+
}
222+
});
223+
}, []);
224+
217225
return (
218226
<View style={styles.appContainer}>
219227
<StatusBar

example/src/constants/Audios.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,47 @@ const filePath: string = globalMetrics.isAndroid
1919
* Copy a file to the specified destination path if it doesn't exist.
2020
* @param {string} value - The name of the file to copy.
2121
* @param {string} destinationPath - The destination path to copy the file to.
22-
* @returns {Promise<void>} A Promise that resolves when the file is copied.
22+
* @returns {Promise<boolean>} A Promise that resolves to true if the file is copied successfully, otherwise false.
2323
*/
2424
const copyFile = async (
2525
value: string,
2626
destinationPath: string
27-
): Promise<void> => {
27+
): Promise<boolean> => {
2828
const fileExists = await fs.exists(`${destinationPath}/${value}`);
2929

3030
if (!fileExists) {
3131
try {
3232
const file = await fs.readFileRes(`raw/${value}`, 'base64');
3333
await fs.writeFile(`${destinationPath}/${value}`, file, 'base64');
34+
return true;
3435
} catch (error) {
35-
console.error(error);
36+
console.error(`Error copying file ${value}: `, error);
37+
return false;
3638
}
3739
}
40+
41+
return true; // File already exists
3842
};
3943

4044
/**
41-
* Copy all files in the 'audioAssetArray' to the destination path (Android only).
42-
* @returns {Promise<void>} A Promise that resolves when all files are copied.
45+
* Copy all files in the 'audioAssetArray' to the destination path (Android only), or return all files (iOS).
46+
* @returns {Promise<string[]>} A Promise that resolves to a list of successfully copied file paths.
4347
*/
44-
const copyFilesToAndroidResources = async (): Promise<void> => {
48+
const copyFilesToNativeResources = async (): Promise<string[]> => {
4549
if (globalMetrics.isAndroid) {
46-
await Promise.all(audioAssetArray.map(value => copyFile(value, filePath)));
50+
const successfulCopies = await Promise.all(
51+
audioAssetArray.map(async value => {
52+
const isSuccess = await copyFile(value, filePath);
53+
return isSuccess ? value : null;
54+
})
55+
);
56+
57+
// Filter out unsuccessful file copies
58+
return successfulCopies?.filter?.(value => value !== null);
4759
}
60+
61+
// On iOS, return all files without copying
62+
return audioAssetArray;
4863
};
4964

5065
const audioAssetArray = [
@@ -54,15 +69,17 @@ const audioAssetArray = [
5469
'file_example_mp3_15s.mp3',
5570
];
5671

57-
copyFilesToAndroidResources();
58-
5972
/**
60-
* List of file objects with information about the files.
61-
* @type {ListItem[]}
73+
* Generate a list of file objects with information about successfully copied files (Android)
74+
* or all files (iOS).
75+
* @returns {Promise<ListItem[]>} A Promise that resolves to the list of file objects.
6276
*/
63-
export const audioListArray: ListItem[] = audioAssetArray.map(
64-
(value, index) => ({
77+
export const generateAudioList = async (): Promise<ListItem[]> => {
78+
const audioAssets = await copyFilesToNativeResources();
79+
80+
// Generate the final list based on the copied or available files
81+
return audioAssets?.map?.((value, index) => ({
6582
fromCurrentUser: index % 2 !== 0,
6683
path: `${filePath}/${value}`,
67-
})
68-
);
84+
}));
85+
};

0 commit comments

Comments
 (0)