一、配置tts-play
找到background.js
1.1、引入spawn和ipcMain
"use strict";
const path = require("path");
const { spawn } = require("child_process");
import { app, protocol, BrowserWindow, ipcMain } from "electron";
1.2、添加监听
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(`子进程已退出,返回代码 ${code}`);
});
});
二、方法封装
// 播放语音
const { ipcRenderer } = require("electron");
export function playMsgByTts(msg) {
ipcRenderer.send("tts-play", msg);
}
三、使用
playMsgByTts('你的播报内容')
网友评论