How to upload an audio file in React Native to Filestack?

191 Views Asked by At

These are my functions for initiating and terminating the recording::

      async function handleRecordingStart() {
    const { granted } = await Audio.getPermissionsAsync();

    if (granted) {
      try {
        const { recording } = await Audio.Recording.createAsync({
          ...Audio.RecordingOptionsPresets.HIGH_QUALITY,
          outputFormat: ".mp3",
        });
        setRecording(recording);
      } catch (error) {
        console.log(error);
      }
    }
  }

  {
    /* Stop recording */
  }
  async function handleRecordingStop() {
    try {
      if (recording) {
        await recording.stopAndUnloadAsync();
        const fileUri = await recording.getURI();
        setRecordingFileURI(fileUri);
        setRecording(null);
        const url = uploadFileAndGetURL(fileUri);
        console.log(url);
      }
    } catch (error) {
      console.log(error);
    }
  }

And here is my upload function:

async function uploadFileAndGetURL() {
    console.log("Starting upload...");
    console.log("Local file uri:", recordingFileURI);
    try {
      if (recordingFileURI) {
        const response = await axios.post(
          `https://www.filestackapi.com/api/store/S3?key=${FILESTACKAPI}`,
          recordingFileURI,
          {
            headers: {
              "Content-Type": "audio/mp3", // tried setting this to .m4a too
            },
          }
        );
        if (response.status === 200) {
          console.log("Upload finished!");
          console.log("URL:", response.data.url);
          console.log("response for debug:", response);
          return response.data.url;
        }
      }
    } catch (error) {
      throw error; // Você pode escolher como lidar com o erro de upload
    }
  }

However, I've run into a problem when attempting to upload the audio file to Filestack. It appears that the uploaded audio file has a duration of 0:00 seconds. Any assistance or insights on resolving this issue would be greatly appreciated!I'm unsure about how to upload a file, whether I should use the URI directly or if I need to perform any transformations.

Example of the URI:

LOG Local file: file:///Users/name/Library/Developer/CoreSimulator/Devices/CD8A6DB/data/Containers/Data/Application/8F898/Library/Caches/ExponentExperienceData/%2540user%company/AV/recording-4BEC2B3B-60.m4a

Upload URL:

Audio upload in FileStack

0

There are 0 best solutions below