organize the code

This commit is contained in:
Gleidson Daniel 2022-11-09 16:40:22 -03:00
parent a73afa9494
commit 84db57e405
2 changed files with 31 additions and 27 deletions

View File

@ -4,13 +4,11 @@ import { Audio, InterruptionModeAndroid, InterruptionModeIOS } from 'expo-av';
import { BorderlessButton } from 'react-native-gesture-handler'; import { BorderlessButton } from 'react-native-gesture-handler';
import { getInfoAsync } from 'expo-file-system'; import { getInfoAsync } from 'expo-file-system';
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake'; 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 styles from './styles';
import I18n from '../../i18n'; import I18n from '../../i18n';
import { themes } from '../../lib/constants'; 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 { events, logEvent } from '../../lib/methods/helpers/log';
import { TSupportedThemes } from '../../theme'; import { TSupportedThemes } from '../../theme';
import { CustomIcon } from '../CustomIcon'; import { CustomIcon } from '../CustomIcon';
@ -160,28 +158,17 @@ export default class RecordAudio extends React.PureComponent<IMessageBoxRecordAu
await this.recording.stopAndUnloadAsync(); await this.recording.stopAndUnloadAsync();
const fileURI = this.recording.getURI(); const fileURI = this.recording.getURI();
const fileName = sanitizeString(fileURI!.substring(fileURI!.lastIndexOf('/') + 1).split('.')[0]); const audioEncodedPath = await encodeAudio(fileURI!);
const newFilePath = `${FileSystem.cacheDirectory}/${fileName}.mp3`; const fileData = await getInfoAsync(audioEncodedPath);
const ffmpegCommand = `-hide_banner -y -i ${fileURI} -c:a libmp3lame -qscale:a 2 ${newFilePath}`; const fileInfo = {
name: `${Date.now()}.mp3`,
FFmpegKit.execute(ffmpegCommand).then(async session => { mime: 'audio/mpeg',
const returnCode = await session.getReturnCode(); type: 'audio/mpeg',
if (ReturnCode.isSuccess(returnCode)) { store: 'Uploads',
const fileData = await getInfoAsync(newFilePath); path: fileData.uri,
console.log(fileData) size: fileData.size
const fileInfo = { };
name: parseFilename(fileData.uri), onFinish(fileInfo);
mime: 'audio/mpeg',
type: 'audio/mpeg',
store: 'Uploads',
path: newFilePath,
size: fileData.size
};
onFinish(fileInfo);
} else {
console.log('error');
}
});
} catch (error) { } catch (error) {
logEvent(events.ROOM_AUDIO_FINISH_F); logEvent(events.ROOM_AUDIO_FINISH_F);
} }

View File

@ -1,12 +1,13 @@
import * as FileSystem from 'expo-file-system'; import * as FileSystem from 'expo-file-system';
import { FFmpegKit, ReturnCode } from 'ffmpeg-kit-react-native';
import { sanitizeLikeString } from '../database/utils'; import { sanitizeLikeString } from '../database/utils';
import { store } from '../store/auxStore'; import { store } from '../store/auxStore';
import log from './helpers/log'; 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 extension = value.substring(value.lastIndexOf('.') + 1);
const filename = sanitizeString(value.substring(value.lastIndexOf('/') + 1).split('.')[0]); const filename = sanitizeString(value.substring(value.lastIndexOf('/') + 1).split('.')[0]);
return `${filename}.${extension}`; return `${filename}.${extension}`;
@ -52,3 +53,19 @@ export const deleteAllAudioFiles = async (serverUrl: string): Promise<void> => {
log(error); log(error);
} }
}; };
export const encodeAudio = (fileURI: string): Promise<string> =>
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();
}
});
});