2020-07-06 19:23:46 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { Text, View } from 'react-native';
|
2022-08-08 21:02:08 +00:00
|
|
|
import { Audio, InterruptionModeAndroid, InterruptionModeIOS } from 'expo-av';
|
2020-07-06 19:23:46 +00:00
|
|
|
import { BorderlessButton } from 'react-native-gesture-handler';
|
|
|
|
import { getInfoAsync } from 'expo-file-system';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake';
|
2020-07-06 19:23:46 +00:00
|
|
|
|
|
|
|
import styles from './styles';
|
|
|
|
import I18n from '../../i18n';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon } from '../CustomIcon';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { events, logEvent } from '../../lib/methods/helpers/log';
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes } from '../../theme';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
interface IMessageBoxRecordAudioProps {
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2021-12-13 16:27:01 +00:00
|
|
|
permissionToUpload: boolean;
|
2021-09-13 20:41:05 +00:00
|
|
|
recordingCallback: Function;
|
|
|
|
onFinish: Function;
|
2022-08-08 18:38:01 +00:00
|
|
|
onStart: Function;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
2020-07-06 19:23:46 +00:00
|
|
|
|
2023-09-26 14:38:47 +00:00
|
|
|
const RECORDING_EXTENSION = '.aac';
|
2020-07-06 19:23:46 +00:00
|
|
|
const RECORDING_SETTINGS = {
|
|
|
|
android: {
|
2023-09-26 14:38:47 +00:00
|
|
|
// Settings related to audio encoding.
|
2020-07-06 19:23:46 +00:00
|
|
|
extension: RECORDING_EXTENSION,
|
2023-09-26 14:38:47 +00:00
|
|
|
outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_AAC_ADTS,
|
2023-08-18 17:48:33 +00:00
|
|
|
audioEncoder: Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC,
|
2023-09-26 14:38:47 +00:00
|
|
|
// Settings related to audio quality.
|
2023-08-18 17:48:33 +00:00
|
|
|
sampleRate: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.android.sampleRate,
|
|
|
|
numberOfChannels: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.android.numberOfChannels,
|
|
|
|
bitRate: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.android.bitRate
|
2020-07-06 19:23:46 +00:00
|
|
|
},
|
|
|
|
ios: {
|
2023-09-26 14:38:47 +00:00
|
|
|
// Settings related to audio encoding.
|
2020-07-06 19:23:46 +00:00
|
|
|
extension: RECORDING_EXTENSION,
|
2023-09-26 14:38:47 +00:00
|
|
|
audioQuality: Audio.RECORDING_OPTION_IOS_AUDIO_QUALITY_MEDIUM,
|
|
|
|
outputFormat: Audio.RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4AAC,
|
|
|
|
// Settings related to audio quality.
|
2023-08-18 17:48:33 +00:00
|
|
|
sampleRate: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.ios.sampleRate,
|
|
|
|
numberOfChannels: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.ios.numberOfChannels,
|
2023-09-26 14:38:47 +00:00
|
|
|
bitRate: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.ios.bitRate
|
2022-08-08 21:02:08 +00:00
|
|
|
},
|
|
|
|
web: {}
|
2020-07-06 19:23:46 +00:00
|
|
|
};
|
2023-09-26 14:38:47 +00:00
|
|
|
|
2020-07-06 19:23:46 +00:00
|
|
|
const RECORDING_MODE = {
|
|
|
|
allowsRecordingIOS: true,
|
|
|
|
playsInSilentModeIOS: true,
|
2021-09-21 14:47:00 +00:00
|
|
|
staysActiveInBackground: true,
|
2020-07-06 19:23:46 +00:00
|
|
|
shouldDuckAndroid: true,
|
|
|
|
playThroughEarpieceAndroid: false,
|
2022-08-08 21:02:08 +00:00
|
|
|
interruptionModeIOS: InterruptionModeIOS.DoNotMix,
|
|
|
|
interruptionModeAndroid: InterruptionModeAndroid.DoNotMix
|
2020-07-06 19:23:46 +00:00
|
|
|
};
|
|
|
|
|
2022-03-31 22:39:24 +00:00
|
|
|
const formatTime = function (time: number) {
|
|
|
|
const minutes = Math.floor(time / 60);
|
|
|
|
const seconds = time % 60;
|
|
|
|
const min = minutes < 10 ? `0${minutes}` : minutes;
|
|
|
|
const sec = seconds < 10 ? `0${seconds}` : seconds;
|
|
|
|
return `${min}:${sec}`;
|
2020-07-06 19:23:46 +00:00
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
export default class RecordAudio extends React.PureComponent<IMessageBoxRecordAudioProps, any> {
|
|
|
|
private isRecorderBusy: boolean;
|
2022-03-31 22:39:24 +00:00
|
|
|
private recording!: Audio.Recording;
|
2021-09-23 12:16:23 +00:00
|
|
|
private LastDuration: number;
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
constructor(props: IMessageBoxRecordAudioProps) {
|
2020-07-06 19:23:46 +00:00
|
|
|
super(props);
|
|
|
|
this.isRecorderBusy = false;
|
2021-09-23 12:16:23 +00:00
|
|
|
this.LastDuration = 0;
|
2020-07-06 19:23:46 +00:00
|
|
|
this.state = {
|
|
|
|
isRecording: false,
|
2021-09-23 12:16:23 +00:00
|
|
|
isRecorderActive: false,
|
2020-07-06 19:23:46 +00:00
|
|
|
recordingDurationMillis: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
const { recordingCallback } = this.props;
|
2021-09-23 12:16:23 +00:00
|
|
|
const { isRecorderActive } = this.state;
|
2020-07-06 19:23:46 +00:00
|
|
|
|
2021-09-23 12:16:23 +00:00
|
|
|
recordingCallback(isRecorderActive);
|
2020-07-06 19:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.recording) {
|
|
|
|
this.cancelRecordingAudio();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get duration() {
|
|
|
|
const { recordingDurationMillis } = this.state;
|
|
|
|
return formatTime(Math.floor(recordingDurationMillis / 1000));
|
|
|
|
}
|
|
|
|
|
2021-09-23 12:16:23 +00:00
|
|
|
get GetLastDuration() {
|
|
|
|
return formatTime(Math.floor(this.LastDuration / 1000));
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
isRecordingPermissionGranted = async () => {
|
2020-07-06 19:23:46 +00:00
|
|
|
try {
|
|
|
|
const permission = await Audio.getPermissionsAsync();
|
|
|
|
if (permission.status === 'granted') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
await Audio.requestPermissionsAsync();
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
return false;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-07-06 19:23:46 +00:00
|
|
|
|
2022-03-31 22:39:24 +00:00
|
|
|
onRecordingStatusUpdate = (status: Audio.RecordingStatus) => {
|
2020-07-06 19:23:46 +00:00
|
|
|
this.setState({
|
|
|
|
isRecording: status.isRecording,
|
|
|
|
recordingDurationMillis: status.durationMillis
|
|
|
|
});
|
2021-09-23 12:16:23 +00:00
|
|
|
this.LastDuration = status.durationMillis;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-07-06 19:23:46 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
startRecordingAudio = async () => {
|
2022-08-08 18:38:01 +00:00
|
|
|
const { onStart } = this.props;
|
|
|
|
onStart();
|
|
|
|
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_AUDIO_RECORD);
|
2020-07-06 19:23:46 +00:00
|
|
|
if (!this.isRecorderBusy) {
|
|
|
|
this.isRecorderBusy = true;
|
2021-09-23 12:16:23 +00:00
|
|
|
this.LastDuration = 0;
|
2020-07-06 19:23:46 +00:00
|
|
|
try {
|
|
|
|
const canRecord = await this.isRecordingPermissionGranted();
|
|
|
|
if (canRecord) {
|
|
|
|
await Audio.setAudioModeAsync(RECORDING_MODE);
|
|
|
|
|
2021-09-23 12:16:23 +00:00
|
|
|
this.setState({ isRecorderActive: true });
|
2020-07-06 19:23:46 +00:00
|
|
|
this.recording = new Audio.Recording();
|
|
|
|
await this.recording.prepareToRecordAsync(RECORDING_SETTINGS);
|
|
|
|
this.recording.setOnRecordingStatusUpdate(this.onRecordingStatusUpdate);
|
|
|
|
|
|
|
|
await this.recording.startAsync();
|
|
|
|
activateKeepAwake();
|
|
|
|
} else {
|
|
|
|
await Audio.requestPermissionsAsync();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_AUDIO_RECORD_F);
|
2020-07-06 19:23:46 +00:00
|
|
|
}
|
|
|
|
this.isRecorderBusy = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
finishRecordingAudio = async () => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_AUDIO_FINISH);
|
2020-07-06 19:23:46 +00:00
|
|
|
if (!this.isRecorderBusy) {
|
|
|
|
const { onFinish } = this.props;
|
|
|
|
|
|
|
|
this.isRecorderBusy = true;
|
|
|
|
try {
|
|
|
|
await this.recording.stopAndUnloadAsync();
|
|
|
|
|
|
|
|
const fileURI = this.recording.getURI();
|
2022-03-31 22:39:24 +00:00
|
|
|
const fileData = await getInfoAsync(fileURI as string);
|
2020-07-06 19:23:46 +00:00
|
|
|
const fileInfo = {
|
2023-09-26 14:38:47 +00:00
|
|
|
name: `${Date.now()}.aac`,
|
2020-07-06 19:23:46 +00:00
|
|
|
mime: 'audio/aac',
|
|
|
|
type: 'audio/aac',
|
|
|
|
store: 'Uploads',
|
|
|
|
path: fileURI,
|
2023-05-18 21:09:33 +00:00
|
|
|
size: fileData.exists ? fileData.size : null
|
2020-07-06 19:23:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
onFinish(fileInfo);
|
|
|
|
} catch (error) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_AUDIO_FINISH_F);
|
2020-07-06 19:23:46 +00:00
|
|
|
}
|
2021-09-23 12:16:23 +00:00
|
|
|
this.setState({ isRecording: false, isRecorderActive: false, recordingDurationMillis: 0 });
|
2020-07-06 19:23:46 +00:00
|
|
|
deactivateKeepAwake();
|
|
|
|
this.isRecorderBusy = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
cancelRecordingAudio = async () => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_AUDIO_CANCEL);
|
2020-07-06 19:23:46 +00:00
|
|
|
if (!this.isRecorderBusy) {
|
|
|
|
this.isRecorderBusy = true;
|
|
|
|
try {
|
|
|
|
await this.recording.stopAndUnloadAsync();
|
|
|
|
} catch (error) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_AUDIO_CANCEL_F);
|
2020-07-06 19:23:46 +00:00
|
|
|
}
|
2021-09-23 12:16:23 +00:00
|
|
|
this.setState({ isRecording: false, isRecorderActive: false, recordingDurationMillis: 0 });
|
2020-07-06 19:23:46 +00:00
|
|
|
deactivateKeepAwake();
|
|
|
|
this.isRecorderBusy = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2021-12-13 16:27:01 +00:00
|
|
|
const { theme, permissionToUpload } = this.props;
|
2021-09-23 12:16:23 +00:00
|
|
|
const { isRecording, isRecorderActive } = this.state;
|
2021-12-13 16:27:01 +00:00
|
|
|
if (!permissionToUpload) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-23 12:16:23 +00:00
|
|
|
if (!isRecording && !isRecorderActive) {
|
2020-07-06 19:23:46 +00:00
|
|
|
return (
|
2022-04-08 14:27:10 +00:00
|
|
|
<BorderlessButton onPress={this.startRecordingAudio} style={styles.actionButton} testID='messagebox-send-audio'>
|
|
|
|
<View accessible accessibilityLabel={I18n.t('Send_audio_message')} accessibilityRole='button'>
|
|
|
|
<CustomIcon name='microphone' size={24} color={themes[theme].auxiliaryTintColor} />
|
|
|
|
</View>
|
2020-07-06 19:23:46 +00:00
|
|
|
</BorderlessButton>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-23 12:16:23 +00:00
|
|
|
if (!isRecording && isRecorderActive) {
|
|
|
|
return (
|
|
|
|
<View style={styles.recordingContent}>
|
|
|
|
<View style={styles.textArea}>
|
2022-04-08 14:27:10 +00:00
|
|
|
<BorderlessButton onPress={this.cancelRecordingAudio} style={styles.actionButton}>
|
|
|
|
<View accessible accessibilityLabel={I18n.t('Cancel_recording')} accessibilityRole='button'>
|
|
|
|
<CustomIcon size={24} color={themes[theme].dangerColor} name='delete' />
|
|
|
|
</View>
|
2021-09-23 12:16:23 +00:00
|
|
|
</BorderlessButton>
|
|
|
|
<Text style={[styles.recordingDurationText, { color: themes[theme].titleText }]}>{this.GetLastDuration}</Text>
|
|
|
|
</View>
|
2022-04-08 14:27:10 +00:00
|
|
|
<BorderlessButton onPress={this.finishRecordingAudio} style={styles.actionButton}>
|
|
|
|
<View accessible accessibilityLabel={I18n.t('Finish_recording')} accessibilityRole='button'>
|
|
|
|
<CustomIcon size={24} color={themes[theme].tintColor} name='send-filled' />
|
|
|
|
</View>
|
2021-09-23 12:16:23 +00:00
|
|
|
</BorderlessButton>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-06 19:23:46 +00:00
|
|
|
return (
|
|
|
|
<View style={styles.recordingContent}>
|
|
|
|
<View style={styles.textArea}>
|
2022-04-08 14:27:10 +00:00
|
|
|
<BorderlessButton onPress={this.cancelRecordingAudio} style={styles.actionButton}>
|
|
|
|
<View accessible accessibilityLabel={I18n.t('Cancel_recording')} accessibilityRole='button'>
|
|
|
|
<CustomIcon size={24} color={themes[theme].dangerColor} name='delete' />
|
|
|
|
</View>
|
2020-07-06 19:23:46 +00:00
|
|
|
</BorderlessButton>
|
2021-09-23 12:16:23 +00:00
|
|
|
<Text style={[styles.recordingDurationText, { color: themes[theme].titleText }]}>{this.duration}</Text>
|
|
|
|
<CustomIcon size={24} color={themes[theme].dangerColor} name='record' />
|
2020-07-06 19:23:46 +00:00
|
|
|
</View>
|
2022-04-08 14:27:10 +00:00
|
|
|
<BorderlessButton onPress={this.finishRecordingAudio} style={styles.actionButton}>
|
|
|
|
<View accessible accessibilityLabel={I18n.t('Finish_recording')} accessibilityRole='button'>
|
|
|
|
<CustomIcon size={24} color={themes[theme].tintColor} name='send-filled' />
|
|
|
|
</View>
|
2020-07-06 19:23:46 +00:00
|
|
|
</BorderlessButton>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|