简体   繁体   English

React Native Audio将语音笔记发送到后端

[英]React Native Audio send voice note to backend

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 . 我有一个聊天屏幕,我想通过该屏幕使用react-native-audio将语音笔记发送到后端,并使用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. 我遇到的问题是在录制语音笔记时, react-native-audio的自述文件不太清楚,所以我想我的实现存在问题。

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 另外,请检查此示例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 我根据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" . POST文件到服务器记得添加file://你的路径像以前那样"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! 在Android Finished recording ,大小似乎仍为0 kb,但可以使用!
I'm saving in AudioUtils.DocumentDirectoryPath atm 我要保存在AudioUtils.DocumentDirectoryPath atm中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM