I am recording an audio using react-native-audio, I am able to record and play the recorded file.
Issue is with sending .aac file to the api. Only for .aac file I am getting api error. If I try to record audio in any other format and send it to the api then its working. I am not sure why this is happening. If there was an issue with the api then other formats should also give some error. Below is my code:
const onSubmitPressed = async () => {
const filePath2 = 'file://' + AudioUtils.DocumentDirectoryPath + '/test.aac';
const fileSize = await getFileSizeInBytes(filePath2);
console.log('fileSize: ',fileSize);
uploadAACFile(filePath2);
};
In here I am using getFileSizeInBytes to confirm that my file exists and have some size:
const getFileSizeInBytes = async (filePath1) => {
try {
const stats = await RNFS.stat(filePath);
return stats.size;
} catch (error) {
console.log('Error getting file size:', error);
return -1;
}
};
then finally:
const uploadAACFile = async (filePath1) => {
try {
const formData = new FormData();
formData.append('Id', '0');
formData.append('EmployeeNo', '10003');
formData.append('File', {
uri: filePath1,
type: 'audio/aac',
name: 'test.aac',
});
const requestOptions = {
method: 'POST',
body: formData,
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
};
console.log('formData: ',JSON.stringify(formData));
const response = await fetch('https://apiEndpoint', requestOptions);
const result = await response.json();
if (response.ok) {
console.log('File uploaded successfully');
console.log('Response:', result);
} else {
console.log('File upload failed');
console.log('Error:', result);
}
} catch (error) {
console.log('Error:', error);
}
};
I am getting error:
Error: {"Data": "", "ErrorMessage": null, "Message": "SaveFileInAzure", "NrOfRecords": 0, "Status": false}
But if I try this with other formats like .lpcm then I am getting success response:
Response: {"Data": "OK", "ErrorMessage": "", "Message": "OK", "NrOfRecords": 0, "Status": true};