【问题标题】:How to convert audiopath to audio file in react-native and send to node.js如何在 react-native 中将音频路径转换为音频文件并发送到 node.js
【发布时间】:2019-05-04 15:22:56
【问题描述】:

我想在 multipart/form-data 请求中将音频 wav 文件从我的 react-native 应用程序发送到 postnodejs/express 路由。

使用https://github.com/goodatlas/react-native-audio-record 包。我得到了我录制的音频文件的路径

audioFile = await AudioRecord.stop();
console.log(audioFile);

/*
this is what i get
/data/user/0/com.docassistant/files/test.wav
*/

//卡在这个逻辑上 //我需要将此路径转换为文件并发送如下

let file = new File([blob], `test.wav`, {
      type: "audio/wav"
});
const formData = new FormData();
formData.append("file", file);

axios
.post(`http://192.168.0.104:8081/postAudio`, formData, {
    headers: {
          "Content-Type": "multipart/form-data"
     }
})
.then(response => {
     console.log(response);
})
.catch(error => {
        console.log(error);
});

另外,我尝试使用 rn-fetch-blob 发送 blob,但收到错误的请求错误。

这是我在 nodejs 的 postrequest。与postman 一起工作正常。

//Route.js
app.post('/postAudio', (req, res) => {

    const form = new multiparty.Form()
    form.parse(req, (error, fields, files) => {
        if (error) throw new Error(error);
        try {
            const path = files.file[0].path;
            const buffer = Fs.readFileSync(path);
            ....
            ....
            ....

【问题讨论】:

  • 你找到解决办法了吗???

标签: javascript node.js express react-native


【解决方案1】:

您需要将filepathAudio转换为base64。并将base64发送到api,在后端它将编码为音频文件

导入并安装 RNFetchBlob

从'rn-fetch-blob'导入RNFetchBlob

const onStopRecord = async () => {
    const result = await audioRecorderPlayer.stopRecorder();
    audioRecorderPlayer.removeRecordBackListener();
    setRecordSecs(0)
    console.log(result);
    RNFetchBlob.fs.readFile(result, 'base64')
        .then((data) => {
            console.log(data, 'fire')
            setVoice(data)
        })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    相关资源
    最近更新 更多