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';
|
2020-07-06 19:23:46 +00:00
|
|
|
import { Audio } from 'expo-av';
|
|
|
|
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';
|
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { events, logEvent } from '../../utils/log';
|
|
|
|
|
|
|
|
interface IMessageBoxRecordAudioProps {
|
|
|
|
theme: string;
|
|
|
|
recordingCallback: Function;
|
|
|
|
onFinish: Function;
|
|
|
|
}
|
2020-07-06 19:23:46 +00:00
|
|
|
|
2021-09-21 14:47:00 +00:00
|
|
|
const RECORDING_EXTENSION = '.m4a';
|
2020-07-06 19:23:46 +00:00
|
|
|
const RECORDING_SETTINGS = {
|
|
|
|
android: {
|
|
|
|
extension: RECORDING_EXTENSION,
|
2021-09-21 14:47:00 +00:00
|
|
|
outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_MPEG_4,
|
2020-07-06 19:23:46 +00:00
|
|
|
audioEncoder: Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC,
|
|
|
|
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
|
|
|
|
},
|
|
|
|
ios: {
|
|
|
|
extension: RECORDING_EXTENSION,
|
|
|
|
audioQuality: Audio.RECORDING_OPTION_IOS_AUDIO_QUALITY_MIN,
|
|
|
|
sampleRate: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.ios.sampleRate,
|
|
|
|
numberOfChannels: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.ios.numberOfChannels,
|
|
|
|
bitRate: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.ios.bitRate,
|
|
|
|
outputFormat: Audio.RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4AAC
|
|
|
|
}
|
|
|
|
};
|
|
|
|
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,
|
|
|
|
interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
|
|
|
|
interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX
|
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const formatTime = function (seconds: any) {
|
|
|
|
let minutes: any = Math.floor(seconds / 60);
|
2020-07-06 19:23:46 +00:00
|
|
|
seconds %= 60;
|
2021-09-13 20:41:05 +00:00
|
|
|
if (minutes < 10) {
|
|
|
|
minutes = `0${minutes}`;
|
|
|
|
}
|
|
|
|
if (seconds < 10) {
|
|
|
|
seconds = `0${seconds}`;
|
|
|
|
}
|
|
|
|
return `${minutes}:${seconds}`;
|
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;
|
|
|
|
|
|
|
|
private recording: any;
|
2020-07-06 19:23:46 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
constructor(props: IMessageBoxRecordAudioProps) {
|
2020-07-06 19:23:46 +00:00
|
|
|
super(props);
|
|
|
|
this.isRecorderBusy = false;
|
|
|
|
this.state = {
|
|
|
|
isRecording: false,
|
|
|
|
recordingDurationMillis: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
const { recordingCallback } = this.props;
|
|
|
|
const { isRecording } = this.state;
|
|
|
|
|
|
|
|
recordingCallback(isRecording);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.recording) {
|
|
|
|
this.cancelRecordingAudio();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get duration() {
|
|
|
|
const { recordingDurationMillis } = this.state;
|
|
|
|
return formatTime(Math.floor(recordingDurationMillis / 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
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
onRecordingStatusUpdate = (status: any) => {
|
2020-07-06 19:23:46 +00:00
|
|
|
this.setState({
|
|
|
|
isRecording: status.isRecording,
|
|
|
|
recordingDurationMillis: 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 () => {
|
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;
|
|
|
|
try {
|
|
|
|
const canRecord = await this.isRecordingPermissionGranted();
|
|
|
|
if (canRecord) {
|
|
|
|
await Audio.setAudioModeAsync(RECORDING_MODE);
|
|
|
|
|
|
|
|
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();
|
|
|
|
const fileData = await getInfoAsync(fileURI);
|
|
|
|
const fileInfo = {
|
2021-09-21 14:47:00 +00:00
|
|
|
name: `${Date.now()}.m4a`,
|
2020-07-06 19:23:46 +00:00
|
|
|
mime: 'audio/aac',
|
|
|
|
type: 'audio/aac',
|
|
|
|
store: 'Uploads',
|
|
|
|
path: fileURI,
|
|
|
|
size: fileData.size
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
this.setState({ isRecording: false, recordingDurationMillis: 0 });
|
|
|
|
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
|
|
|
}
|
|
|
|
this.setState({ isRecording: false, recordingDurationMillis: 0 });
|
|
|
|
deactivateKeepAwake();
|
|
|
|
this.isRecorderBusy = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { theme } = this.props;
|
|
|
|
const { isRecording } = this.state;
|
|
|
|
|
|
|
|
if (!isRecording) {
|
|
|
|
return (
|
|
|
|
<BorderlessButton
|
|
|
|
onPress={this.startRecordingAudio}
|
|
|
|
style={styles.actionButton}
|
|
|
|
testID='messagebox-send-audio'
|
2021-09-13 20:41:05 +00:00
|
|
|
// @ts-ignore
|
2020-07-06 19:23:46 +00:00
|
|
|
accessibilityLabel={I18n.t('Send_audio_message')}
|
2021-09-13 20:41:05 +00:00
|
|
|
accessibilityTraits='button'>
|
2020-10-30 17:35:07 +00:00
|
|
|
<CustomIcon name='microphone' size={24} color={themes[theme].auxiliaryTintColor} />
|
2020-07-06 19:23:46 +00:00
|
|
|
</BorderlessButton>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.recordingContent}>
|
|
|
|
<View style={styles.textArea}>
|
|
|
|
<BorderlessButton
|
|
|
|
onPress={this.cancelRecordingAudio}
|
2021-09-13 20:41:05 +00:00
|
|
|
// @ts-ignore
|
2020-07-06 19:23:46 +00:00
|
|
|
accessibilityLabel={I18n.t('Cancel_recording')}
|
|
|
|
accessibilityTraits='button'
|
2021-09-13 20:41:05 +00:00
|
|
|
style={styles.actionButton}>
|
|
|
|
<CustomIcon size={24} color={themes[theme].dangerColor} name='close' />
|
2020-07-06 19:23:46 +00:00
|
|
|
</BorderlessButton>
|
2021-09-13 20:41:05 +00:00
|
|
|
<Text style={[styles.recordingCancelText, { color: themes[theme].titleText }]}>{this.duration}</Text>
|
2020-07-06 19:23:46 +00:00
|
|
|
</View>
|
|
|
|
<BorderlessButton
|
|
|
|
onPress={this.finishRecordingAudio}
|
2021-09-13 20:41:05 +00:00
|
|
|
// @ts-ignore
|
2020-07-06 19:23:46 +00:00
|
|
|
accessibilityLabel={I18n.t('Finish_recording')}
|
|
|
|
accessibilityTraits='button'
|
2021-09-13 20:41:05 +00:00
|
|
|
style={styles.actionButton}>
|
|
|
|
<CustomIcon size={24} color={themes[theme].successColor} name='check' />
|
2020-07-06 19:23:46 +00:00
|
|
|
</BorderlessButton>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|