将 blob 音频保存在磁盘宽度 Electron 上

Save blob audio on disk width Electron

提问人:Dominique Blanchemain 提问时间:11/18/2023 更新时间:11/20/2023 访问量:29

问:

Electron:使用离线渲染器录制音频序列并在我的渲染器.js中创建blob(audio/wav)后,如何使用main.js将此blob保存到磁盘?

javascript node.js 电子 blob web-audio-api

评论

0赞 Kosh 11/18/2023
到目前为止,您尝试过什么?

答:

1赞 Dominique Blanchemain 11/18/2023 #1

解决!
我将其添加到Preload.js中:

saveAudio: (channel, ...args) => {
            // whitelist channels
            let validChannels = ["saveAudio"];
            if (validChannels.includes(channel)) {
                ipcRenderer.send(channel,'saveAudio',...args)
            }
        }

预加载.js:

// preload.js
const { contextBridge, ipcRenderer } = require('electron')
// Toutes les API Node.js sont disponibles dans le processus de préchargement.
// Il a la même sandbox qu'une extension Chrome.
window.addEventListener('DOMContentLoaded', () => {
  const replaceText = (selector, text) => {
    const element = document.getElementById(selector)
    if (element) element.innerText = text
  }

  for (const dependency of ['chrome', 'node', 'electron']) {
    replaceText(`${dependency}-version`, process.versions[dependency])
  }

})
contextBridge.exposeInMainWorld(
"api", {
        send: (channel, data) => {
            // whitelist channels
            let validChannels = ["toMain"];
            if (validChannels.includes(channel)) {
                ipcRenderer.send(channel, data);
            }
        },
        receive: (channel, func) => {
            let validChannels = ["fromMain",'fromparam'];
            if (validChannels.includes(channel)) {
                // Deliberately strip event as it includes `sender` 
                ipcRenderer.on(channel, (event, ...args) => func(...args));
            }
        },
        contextmenu: (channel, data) => {
            // whitelist channels
            let validChannels = ["showmenu"];
            if (validChannels.includes(channel)) {
                ipcRenderer.send(channel,'showmenu')
            }
        },
        saveAudio: (channel, ...args) => {
            // whitelist channels
            let validChannels = ["saveAudio"];
            if (validChannels.includes(channel)) {
                ipcRenderer.send(channel,'saveAudio',...args)
            }
        }
    }
)

Main.js:

function autoFileSave(event,filePath,audioData) {
    const buffer = Buffer.from(audioData)
    fs.writeFile(filePath, buffer, (err) => {
                       if (err) throw err;
            console.log('Saved!')
                    });
}
ipcMain.on ("saveAudio", (event, ...args) => {                                  // Affichage du menu popup
    console.log(`Restore`+ args[1] +` from param`)
    autoFileSave(event,args[1],args[2])
});

渲染器 .js:

var reader = new FileReader();
reader.readAsArrayBuffer(rwav);
reader.onloadend = (event) => {
// The contents of the BLOB are in reader.result:
//const view = new Int8Array(reader.result);
            
window.api.saveAudio('saveAudio',(audioDirectory+"exports/"+tableObjet[obj].id+".wav"), reader.result);

文件已正确保存并被大胆识别