I am unable to access audio files on ios using expo-media-library

798 Views Asked by At

I'm trying to fetch all the songs on a user's phone using expo-media-library like this:

const getAudioFiles = async () => {
    let media = await MediaLibrary.getAssetsAsync({
      mediaType: "audio",
    });
    media = await MediaLibrary.getAssetsAsync({
      mediaType: "audio",
      first: media.totalCount,
    });
    setTotalAudioCount(media.totalCount);
    setAudioFiles([...audioFiles, ...media.assets]);
  };

It fetches successfully on android, but on an iPhone, it returns an empty array with no errors (but there are actually audio files on the phone!)

Any help on this will be really appreciated

2

There are 2 best solutions below

3
On

Changed mediaType

const getAudioFiles = async () => {
  const media = await MediaLibrary.getAssetsAsync({
    mediaType: MediaLibrary.MediaType.audio, // Changed mediaType
    first: media.totalCount,
  });
  setTotalAudioCount(media.totalCount);
  setAudioFiles([...audioFiles, ...media.assets]);
};
0
On

you should run the app on an actual device and it will work because I try it on the IOS simulator and does not work.