我有一个聊天屏幕,我想在其中使用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)
}我在后端函数中记录了这个文件,但是我一直得到一个空数组,那么我在这里做错了什么呢?
发布于 2019-04-26 15:09:54
我按照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上看起来仍然是0kb,但它可以工作!
我在AudioUtils.DocumentDirectoryPath自动取款机里存钱
发布于 2018-12-03 20:57:11
在您的表单数据中传递:
AudioUtils.DocumentDirectoryPath + '/test.aac'作为文件的路径?对于您当前的代码片段,它有点不清楚。
另外,请查看此示例https://github.com/jsierles/react-native-audio/blob/master/AudioExample/AudioExample.js
https://stackoverflow.com/questions/53593703
复制相似问题