From 84db57e405620e6c6922274eae1f50b2d446892f Mon Sep 17 00:00:00 2001 From: Gleidson Daniel Date: Wed, 9 Nov 2022 16:40:22 -0300 Subject: [PATCH] organize the code --- app/containers/MessageBox/RecordAudio.tsx | 37 ++++++++--------------- app/lib/methods/audioFile.ts | 21 +++++++++++-- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/app/containers/MessageBox/RecordAudio.tsx b/app/containers/MessageBox/RecordAudio.tsx index 7a749f58f..576164d8b 100644 --- a/app/containers/MessageBox/RecordAudio.tsx +++ b/app/containers/MessageBox/RecordAudio.tsx @@ -4,13 +4,11 @@ import { Audio, InterruptionModeAndroid, InterruptionModeIOS } from 'expo-av'; import { BorderlessButton } from 'react-native-gesture-handler'; import { getInfoAsync } from 'expo-file-system'; import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake'; -import * as FileSystem from 'expo-file-system'; -import { FFmpegKit, ReturnCode } from 'ffmpeg-kit-react-native'; import styles from './styles'; import I18n from '../../i18n'; import { themes } from '../../lib/constants'; -import { parseFilename, sanitizeString } from '../../lib/methods/audioFile'; +import { encodeAudio } from '../../lib/methods/audioFile'; import { events, logEvent } from '../../lib/methods/helpers/log'; import { TSupportedThemes } from '../../theme'; import { CustomIcon } from '../CustomIcon'; @@ -160,28 +158,17 @@ export default class RecordAudio extends React.PureComponent { - const returnCode = await session.getReturnCode(); - if (ReturnCode.isSuccess(returnCode)) { - const fileData = await getInfoAsync(newFilePath); - console.log(fileData) - const fileInfo = { - name: parseFilename(fileData.uri), - mime: 'audio/mpeg', - type: 'audio/mpeg', - store: 'Uploads', - path: newFilePath, - size: fileData.size - }; - onFinish(fileInfo); - } else { - console.log('error'); - } - }); + const audioEncodedPath = await encodeAudio(fileURI!); + const fileData = await getInfoAsync(audioEncodedPath); + const fileInfo = { + name: `${Date.now()}.mp3`, + mime: 'audio/mpeg', + type: 'audio/mpeg', + store: 'Uploads', + path: fileData.uri, + size: fileData.size + }; + onFinish(fileInfo); } catch (error) { logEvent(events.ROOM_AUDIO_FINISH_F); } diff --git a/app/lib/methods/audioFile.ts b/app/lib/methods/audioFile.ts index 65e627457..b7d6a1731 100644 --- a/app/lib/methods/audioFile.ts +++ b/app/lib/methods/audioFile.ts @@ -1,12 +1,13 @@ import * as FileSystem from 'expo-file-system'; +import { FFmpegKit, ReturnCode } from 'ffmpeg-kit-react-native'; import { sanitizeLikeString } from '../database/utils'; import { store } from '../store/auxStore'; import log from './helpers/log'; -export const sanitizeString = (value: string) => sanitizeLikeString(value.substring(value.lastIndexOf('/') + 1)); +const sanitizeString = (value: string) => sanitizeLikeString(value.substring(value.lastIndexOf('/') + 1)); -export const parseFilename = (value: string) => { +const parseFilename = (value: string) => { const extension = value.substring(value.lastIndexOf('.') + 1); const filename = sanitizeString(value.substring(value.lastIndexOf('/') + 1).split('.')[0]); return `${filename}.${extension}`; @@ -52,3 +53,19 @@ export const deleteAllAudioFiles = async (serverUrl: string): Promise => { log(error); } }; + +export const encodeAudio = (fileURI: string): Promise => + new Promise((resolve, reject) => { + const fileName = sanitizeString(fileURI.substring(fileURI.lastIndexOf('/') + 1).split('.')[0]); + const newFilePath = `${FileSystem.cacheDirectory}${fileName}.mp3`; + const ffmpegCommand = `-hide_banner -y -i ${fileURI} -c:a libmp3lame -qscale:a 2 ${newFilePath}`; + FFmpegKit.execute(ffmpegCommand).then(async session => { + const returnCode = await session.getReturnCode(); + if (ReturnCode.isSuccess(returnCode)) { + return resolve(newFilePath); + } + if (ReturnCode.isCancel(returnCode)) { + return reject(); + } + }); + });