Skip to content

Can we load audio sources from cacheDir? #875

@anxheloo

Description

@anxheloo

🪲 Description

I want to download each remote url source and store it on cacheDir of the device. Than i can load directly from it. It is working fine on android but on IOS it keeps loading

🪲 What have you tried?
I installed react native blob util, download the source url (remote one) and store it on cache dir to load later, on android is working but not on IOS.

The search section is not working correctly.

🪲 Please post your code:

export const getOrDownloadFile = async ({fileUrl, id}: PathProps) => {
  const {hasInternet} = useAppStore.getState();

  // 1. If not fileUrl provided return null
  if (!fileUrl) {
    return null;
  }

  try {
    // 2.Construct local path based on fileUrl and id.
    const localPath = await getLocalPath({
      fileUrl,
      id,
    });

    // 3.Check if it exist on cache
    const exists = await ensureFileExists(localPath);

    // 4. If it exits return it based on platform
    if (exists) {
      if (Platform.OS === 'android') {
        return localPath;
      }
      return 'file://' + localPath;
    }

    if (!hasInternet) {
      return null;
    }

    // 5.Else download it, store on cache using localPath, return it
    const res = await ReactNativeBlobUtil.config({
      fileCache: true,
      path: localPath,
    }).fetch('GET', fileUrl);

    if (res.info().status !== 200) {
      return null;
    }

    return Platform.OS === 'android' ? res.path() : 'file://' + res.path();
  } catch (error: any) {
    return null;
  }
};


  useEffect(() => {
    if (!fileUrl) {
      return;
    }
    //Sound.CACHES Sound.MAIN_BUNDLE
    sound.current = new Sound(fileUrl, Sound.CACHES, error => {
      if (error) {
        setIsLoaded(false);
        return;
      }

      setDuration(Math.ceil(Number(sound?.current?.getDuration())));
      setIsLoaded(true);
    });

    return () => {
      sound.current?.release();
    };
  }, [fileUrl, stop]);

💡 Possible solution

Is your issue with...

  • [ x] iOS
  • Android
  • Windows

Are you using...

  • [x ] React Native CLI (e.g. react-native run-android)
  • Expo
  • Other: (please specify)

Which versions are you using?

  • React Native Sound: "react-native-sound": "^0.13.0",
  • React Native: "react-native": "0.79.5",
  • iOS: 18.5
  • Android:
  • Windows:

Does the problem occur on...

  • Simulator
  • [ x] Device

If your problem is happening on a device, which device?

  • Device:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions