I have a chat screen from which I want to send voice note to the backend using react-native-audio
and play it using react-native-sound
.
The problem I am having is in the recording of the voice note, the Readme in the react-native-audio
isn't that clear so I guess there is something wrong with my implementation.
Here is my render method
render() {
return (
<TouchableOpacity onPress={ this.start_timer }>
<Icon name="microphone-outline" type="MaterialCommunityIcons" style={ styles.microphone_icon } />
</TouchableOpacity>
{
this.state.is_recording
? <View style={ styles.recorder_container }>
<TouchableOpacity onPress={ this.clear_time }>
<Icon name="ios-trash" type="Ionicons" style={ styles.trash_icon } />
</TouchableOpacity>
<Text style={ styles.timer }>{ this.state.count_up.format('mm:ss') }</Text>
<TouchableOpacity onPress={ this.send }>
<Icon name="md-send" type="Ionicons" style={ styles.send_icon } />
</TouchableOpacity>
</View>
: null
}
)
}
and here are the functions to record and send
start_timer = () => {
this.setState({ is_recording: true })
let audioPath = AudioUtils.DocumentDirectoryPath + '/test.aac';
this.audio = AudioRecorder.prepareRecordingAtPath(audioPath, {
SampleRate: 22050,
Channels: 1,
AudioQuality: "Low",
AudioEncoding: "aac"
});
this.setState({ audio: audioPath })
this.interval = setInterval(() => this.setState(prev => ({ count_up: prev.count_up.add(1, 'second') })), 1000)
}
send = async () => {
await AudioRecorder.stopRecording();
this.setState({ is_recording: false })
AudioRecorder.onFinished = async (data) => {
let fd = new FormData();
await fd.append('file', data.audioFileURL)
let sound = await Api.post('api/chats/upload-vc', fd)
}
}
clear_time = () => {
this.setState({ is_recording: false, count_up: moment().minute(0).second(0) })
clearInterval(this.interval)
}
I logged the file in the backend function but I keep getting an empty array so what am I doing wrong here?
In your form data are you passing:
AudioUtils.DocumentDirectoryPath + '/test.aac'
as the path of the file? it's a bit unclear with your current snippet.
Also, checkout this example https://github.com/jsierles/react-native-audio/blob/master/AudioExample/AudioExample.js
I fixed my audio PLAYER following this tip from 2016: https://github.com/zmxv/react-native-sound/issues/20#issuecomment-205683378
And to POST
your file to a server remember to add file://
before your path like "file:///data/user/0/bla.bla/files/1695695e-aaaa-4e0b-9b57-998cb6b50608.aac"
. The size still appears to be 0 kb on Android Finished recording
but it works!
I'm saving in AudioUtils.DocumentDirectoryPath
atm
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.