How to call Open AI Transcription Audio from React Native. Expo GO. Invalid file format. Supported formats

I have a react native app with expo go. The functionality is simple, I tap record, and then stop, and the audio file should be sent to Open AI and transcribe it to text.

However I am having difficulties as it requires a File, not an URI.

I get this error:

> ents/ExponentExperienceData/@levm38/ai-reminders/recording_1718887602296.mp3"}
> ERROR  Failed to transcribe audio: [AxiosError: Request failed with
> status code 400]  LOG  Response data: {"error": {"code": null,
> "message": "Invalid file format. Supported formats: ['flac', 'm4a',
> 'mp3', 'mp4', 'mpeg', 'mpga', 'oga', 'ogg', 'wav', 'webm']", "param":
> null, "type": "invalid_request_error"}}

The code is as follows:

  const stopRecording = async () => {
        console.log('Stopping recording...');
        setRecording(null);
        setIsRecording(false);
        await recording?.stopAndUnloadAsync();
    
        const uri = recording?.getURI();
        if (uri) {
          const timestamp = Date.now();
    
          try {
            // Convert recording to MP3
            const mp3FileUri = `${FileSystem.documentDirectory}recording_${timestamp}.mp3`;
            await FileSystem.copyAsync({
              from: uri,
              to: mp3FileUri,
            });
    
            console.log('Recording saved as MP3:', mp3FileUri);
    
            // Fetch duration of the recording
            const { sound } = await Audio.Sound.createAsync({ uri: mp3FileUri });
            const { durationMillis } = await sound.getStatusAsync();
            await sound.unloadAsync();
    
            await saveRecording(mp3FileUri, timestamp, durationMillis);
    
            // Read the MP3 file content into a Base64 string
            const mp3Base64String = await FileSystem.readAsStringAsync(mp3FileUri, {
              encoding: FileSystem.EncodingType.Base64,
            });
    
            // Convert Base64 string back to Blob
            const mp3Blob = await base64ToBlob(mp3Base64String, 'audio/mpeg');
    
            // Call the transcription function with the Blob object
            await transcribeAudio(mp3Blob);
          } catch (error) {
            console.error('Failed to save recording as MP3:', error);
          }
        }
      };
    
      const transcribeAudio = async (fileBlob) => {
        try {
          const formData = new FormData();
          formData.append('file', {
            uri: fileBlob.uri,
            type: 'audio/mpeg', // Set correct MIME type here
            name: `recording_${Date.now()}.mp3`, // Ensure the name is set appropriately
          });
          formData.append('model', 'whisper-1');
          formData.append('response_format', 'verbose_json');
          formData.append('timestamp_granularities', 'segment');
      
          const headers = {
            'Authorization': `Bearer ${openaiApiKey}`,
            'Content-Type': 'multipart/form-data',
          };
      
          const response = await axios.post('https://api.openai.com/v1/audio/transcriptions', formData, { headers });
          console.log('Transcription response:', response.data); // Log full response for debugging
      
          // Extract the actual transcription text from the response
          const transcriptionText = response.data.text;
          console.log('Transcription text:', transcriptionText);
        } catch (error) {
          console.error('Failed to transcribe audio:', error);
          if (error.response) {
            console.log('Response data:', error.response.data);
          }
        }
      };


const base64ToBlob = async (base64Data, contentType) => {
  console.log('Converting Base64 to Blob...');
  try {
    const response = await fetch(`data:${contentType};base64,${base64Data}`);
    const blob = await response.blob();
    console.log('Blob conversion successful:', blob);
    return blob;
  } catch (error) {
    console.error('Failed to convert Base64 to Blob:', error);
    throw error;
  }
};

If I check my logs I get this:

> LOG  Recording saved as MP3:
> file:///var/mobile/Containers/Data/Application/2C4F636D-9A6C-4FAD-976D-3A4D7D7B2EE7/Documents/ExponentExperienceData/@levm38/ai-reminders/recording_1718887873585.mp3
> LOG  Recording saved {"duration": 1858, "timestamp": 1718887873585,
> "uri":
> "file:///var/mobile/Containers/Data/Application/2C4F636D-9A6C-4FAD-976D-3A4D7D7B2EE7/Documents/ExponentExperienceData/@levm38/ai-reminders/recording_1718887873585.mp3"}
> LOG  Converting Base64 to Blob...  LOG  Blob conversion successful:
> {"_data": {"__collector": {}, "blobId":
> "92385B71-9F4F-4F40-9D2B-D09A84B06AF6", "name": "Unknown", "offset":
> 0, "size": 68915, "type": "audio/mpeg"}}  ERROR  Failed to transcribe
> audio: [AxiosError: Request failed with status code 400]  LOG 
> Response data: {"error": {"code": null, "message": "Invalid file
> format. Supported formats: ['flac', 'm4a', 'mp3', 'mp4', 'mpeg',
> 'mpga', 'oga', 'ogg', 'wav', 'webm']", "param": null, "type":
> "invalid_request_error"}}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật