我有一个聊天屏幕,我想使用react-native-audio
向后端发送语音注释并使用react-native-sound
播放。
我遇到的问题是在录制语音注释时,react-native-audio
中的自述文件不是那么清楚所以我猜我的实现有问题。
这是我的渲染方法
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
}
)
}
这是记录和发送的功能
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)
}
我在后端函数中记录了该文件但是我一直在得到一个空数组,所以我在这里做错了什么?
在您的表单数据中,您传递的是:
AudioUtils.DocumentDirectoryPath + '/test.aac'
作为文件的路径?你当前的片段有点不清楚。
另外,请查看此示例https://github.com/jsierles/react-native-audio/blob/master/AudioExample/AudioExample.js
我从2016年开始修改我的音频播放器:https://github.com/zmxv/react-native-sound/issues/20#issuecomment-205683378
并且POST
你的文件到服务器记得在你的路径之前添加file://
像"file:///data/user/0/bla.bla/files/1695695e-aaaa-4e0b-9b57-998cb6b50608.aac"
。在Android Finished recording
上,大小似乎仍为0 kb,但它确实有效!
我在AudioUtils.DocumentDirectoryPath
atm中存钱