const downloadAudio = async (url: string, filename: string): Promise<string> => {
let fileUri = await Filesystem.downloadFile({
path: filename,
url: url,
directory: Directory.Data,
recursive: true
});
console.log(fileUri);
return fileUri.path!;
};
const playAudio = async (audioPath: string, id: string) => {
try {
const fullUrl = `https://sitename.com/${audioPath}`;
const filename = audioPath.split("/").pop();
const assetId = `audio-${id}`;
const fileUri = await downloadAudio(fullUrl, filename!);
if (Capacitor.getPlatform() === 'web') {
await NativeAudio.preload({
assetId,
assetPath: audioPath,
audioChannelNum: 1,
isUrl: true,
});
await NativeAudio.play({ assetId });
} else if(Capacitor.getPlatform() === 'android') {
await NativeAudio.preload({
assetId,
assetPath: fileUri,
audioChannelNum: 1,
isUrl: false,
});
await NativeAudio.play({ assetId });
}
} catch (err) {
console.error("Error playing audio:", err);
}
};
With this code I am not able to play audio in mobile