-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[IMPROVEMENT] Expo-av audio recorder #2195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegolmello
merged 15 commits into
RocketChat:develop
from
regalstreak:audio-expo-recorder
Jul 6, 2020
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2311438
[NEW] MessageBox: Expo-av audio recorder
regalstreak d2f5cff
Merge branch 'develop' into audio-expo-recorder
diegolmello a79919d
RecordAudio: Introduce cancel and send buttons
regalstreak f192533
RecordAudio: Introduce recorderBusy state, refactor MessageBox, remov…
regalstreak 7272d3a
RecordAudio: Better audio quality 🎉, stop recording on unmount
regalstreak d557d67
RecordAudio: Use FileSystem from expo-file-system instead of RNFetchBlob
regalstreak 47f4699
chore: flush out react-native-audio
regalstreak 3ad86af
fix(MessageBox): bring back some missed styles during refactor
regalstreak ed5e09c
refactor(RecordAudio): use class component
regalstreak 0ee380a
refactor(RecordAudio): recorder busy to class property, styling changes
regalstreak 3090a07
fix(RecordAudio): missing await in isRecordingPermissionGranted
regalstreak 34cf913
fix(RecordAudio): set isRecording = false on cancel/finish, refactor …
regalstreak 841e334
Merge branch 'develop' into audio-expo-recorder
regalstreak c04d224
Merge branch 'develop' into audio-expo-recorder
diegolmello 0b80cd6
Merge branch 'develop' into audio-expo-recorder
diegolmello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { View, Text } from 'react-native'; | ||
| import { Audio } from 'expo-av'; | ||
| import { BorderlessButton } from 'react-native-gesture-handler'; | ||
| import { getInfoAsync } from 'expo-file-system'; | ||
| import { deactivateKeepAwake, activateKeepAwake } from 'expo-keep-awake'; | ||
|
|
||
| import styles from './styles'; | ||
| import I18n from '../../i18n'; | ||
| import { themes } from '../../constants/colors'; | ||
| import { CustomIcon } from '../../lib/Icons'; | ||
|
|
||
| const RECORDING_EXTENSION = '.aac'; | ||
| const RECORDING_SETTINGS = { | ||
| android: { | ||
| extension: RECORDING_EXTENSION, | ||
| outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_AAC_ADTS, | ||
| 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, | ||
| staysActiveInBackground: false, | ||
| shouldDuckAndroid: true, | ||
| playThroughEarpieceAndroid: false, | ||
| interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX, | ||
| interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX | ||
| }; | ||
|
|
||
| const formatTime = function(seconds) { | ||
| let minutes = Math.floor(seconds / 60); | ||
| seconds %= 60; | ||
| if (minutes < 10) { minutes = `0${ minutes }`; } | ||
| if (seconds < 10) { seconds = `0${ seconds }`; } | ||
| return `${ minutes }:${ seconds }`; | ||
| }; | ||
|
|
||
| export default class RecordAudio extends React.PureComponent { | ||
| static propTypes = { | ||
| theme: PropTypes.string, | ||
| recordingCallback: PropTypes.func, | ||
| onFinish: PropTypes.func | ||
| } | ||
|
|
||
| constructor(props) { | ||
| super(props); | ||
| this.recording = null; | ||
| this.state = { | ||
| isRecording: false, | ||
| isRecorderBusy: false, | ||
|
regalstreak marked this conversation as resolved.
Outdated
|
||
| 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)); | ||
| } | ||
|
djorkaeffalexandre marked this conversation as resolved.
|
||
|
|
||
| startRecordingAudio = async() => { | ||
| const { isRecorderBusy } = this.state; | ||
|
|
||
| if (!isRecorderBusy) { | ||
| this.setState({ isRecorderBusy: true }); | ||
| try { | ||
| const permissions = await Audio.getPermissionsAsync(); | ||
|
|
||
| if (permissions.status === 'granted') { | ||
|
regalstreak marked this conversation as resolved.
Outdated
|
||
| await Audio.setAudioModeAsync(RECORDING_MODE); | ||
|
|
||
| if (this.recording !== null) { | ||
| this.recording.setOnRecordingStatusUpdate(null); | ||
| this.recording = null; | ||
| } | ||
|
|
||
| const recording = new Audio.Recording(); | ||
| await recording.prepareToRecordAsync(RECORDING_SETTINGS); | ||
| recording.setOnRecordingStatusUpdate((status) => { | ||
| this.setState({ | ||
| isRecording: status.isRecording, | ||
| recordingDurationMillis: status.durationMillis | ||
| }); | ||
| }); | ||
|
regalstreak marked this conversation as resolved.
Outdated
|
||
|
|
||
| this.recording = recording; | ||
|
|
||
| await this.recording.startAsync(); | ||
|
regalstreak marked this conversation as resolved.
|
||
| activateKeepAwake(); | ||
| } else { | ||
| await Audio.requestPermissionsAsync(); | ||
| } | ||
| } catch (error) { | ||
| // Do nothing | ||
|
regalstreak marked this conversation as resolved.
Outdated
|
||
| } | ||
| this.setState({ isRecorderBusy: false }); | ||
| } | ||
| }; | ||
|
|
||
| finishRecordingAudio = async() => { | ||
| const { isRecorderBusy } = this.state; | ||
|
|
||
| if (!isRecorderBusy) { | ||
| const { onFinish } = this.props; | ||
|
|
||
| this.setState({ isRecorderBusy: true }); | ||
| try { | ||
| await this.recording.stopAndUnloadAsync(); | ||
|
regalstreak marked this conversation as resolved.
|
||
|
|
||
| const fileURI = this.recording.getURI(); | ||
| const fileData = await getInfoAsync(fileURI); | ||
| const fileInfo = { | ||
| name: `${ Date.now() }.aac`, | ||
| mime: 'audio/aac', | ||
| type: 'audio/aac', | ||
| store: 'Uploads', | ||
| path: fileURI, | ||
| size: fileData.size | ||
| }; | ||
|
|
||
| onFinish(fileInfo); | ||
| } catch (error) { | ||
| // Do nothing | ||
| } | ||
| deactivateKeepAwake(); | ||
| this.setState({ isRecorderBusy: false }); | ||
| } | ||
| }; | ||
|
|
||
| cancelRecordingAudio = async() => { | ||
| const { isRecorderBusy } = this.state; | ||
|
|
||
| if (!isRecorderBusy) { | ||
| this.setState({ isRecorderBusy: true }); | ||
| try { | ||
| await this.recording.stopAndUnloadAsync(); | ||
| } catch (error) { | ||
| // Do nothing | ||
| } | ||
| deactivateKeepAwake(); | ||
| this.setState({ 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' | ||
| accessibilityLabel={I18n.t('Send_audio_message')} | ||
| accessibilityTraits='button' | ||
| > | ||
| <CustomIcon name='mic' size={23} color={themes[theme].tintColor} /> | ||
| </BorderlessButton> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <View style={styles.recordingContent}> | ||
| <View style={styles.textArea}> | ||
| <BorderlessButton | ||
| onPress={this.cancelRecordingAudio} | ||
| accessibilityLabel={I18n.t('Cancel_recording')} | ||
| accessibilityTraits='button' | ||
| style={styles.actionButton} | ||
| > | ||
| <CustomIcon | ||
| size={22} | ||
| color={themes[theme].dangerColor} | ||
| name='Cross' | ||
| /> | ||
| </BorderlessButton> | ||
| <Text | ||
| key='currentTime' | ||
|
regalstreak marked this conversation as resolved.
Outdated
|
||
| style={[styles.recordingCancelText, { color: themes[theme].titleText }]} | ||
| > | ||
| {this.duration} | ||
| </Text> | ||
| </View> | ||
| <View style={styles.recordingContentFinish}> | ||
| <BorderlessButton | ||
| onPress={this.finishRecordingAudio} | ||
| accessibilityLabel={I18n.t('Finish_recording')} | ||
| accessibilityTraits='button' | ||
| style={styles.actionButton} | ||
| > | ||
| <CustomIcon | ||
| size={22} | ||
| color={themes[theme].successColor} | ||
| name='check' | ||
| /> | ||
| </BorderlessButton> | ||
| </View> | ||
| </View> | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.