一、配置tts-play

  • 找到background.js,引入spawnipcMain
"use strict";
const path = require("path");
const { spawn } = require("child_process");
import { app, protocol, BrowserWindow, ipcMain } from "electron";
  • 添加监听
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('你的播报内容')
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐