如何在Electron中打造实现语音合成播放,让Windows自带TTS助你实现离线流畅语音播报语音朗读功能
生活随笔
收集整理的這篇文章主要介紹了
如何在Electron中打造实现语音合成播放,让Windows自带TTS助你实现离线流畅语音播报语音朗读功能
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用Electron和Windows自帶TTS進(jìn)行語音合成播放
const path = require('path'); const { spawn } = require('child_process'); const { app,BrowserWindow,ipcMain } = require('electron');function createWindow() {let mainWindow = new BrowserWindow({ width: 800, height: 600 });mainWindow.loadURL(url.format({pathname: path.join(__dirname, 'index.html'),}));mainWindow.on('closed', () => {mainWindow = null;}); }app.on('ready', () => {createWindow(); });ipcMain.on('tts-play', (event, message) => {const child = spawn('powershell.exe', ['-command',`Add-Type -AssemblyName System.speech; $synth = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer; $synth.Speak('${message}');`,]);child.on('error', (err) => {console.error(err);});child.on('close', (code) => {console.log(`子進(jìn)程已退出,返回代碼 ${code}`);});});在這里我們使用Electron的ipcMain來監(jiān)聽渲染進(jìn)程中的“tts-play”事件,當(dāng)事件發(fā)生時(shí),使用Windows自帶的TTS API來合成指定的文本,并將其播放出來。
const { ipcRenderer } = require('electron');ipcRenderer.send('tts-play', '阿巴阿巴阿巴~');總結(jié)
以上是生活随笔為你收集整理的如何在Electron中打造实现语音合成播放,让Windows自带TTS助你实现离线流畅语音播报语音朗读功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 胆固醇-聚乙二醇-叠氮,Choleste
- 下一篇: Spring Security并没有那么