我正在尝试将语音识别实现到电子应用程序中。该解决方案适用于chrome浏览器,但不适用于电子。应用程序立即停止侦听-它可能没有麦克风权限。如何授予权限?
index.js
const electron = require('electron');
const url = require('url');
const path = require('path');
const { app, BrowserWindow, ipcMain } = electron;
let mainWindow;
ipcMain.on('close-me', (evt, arg) => {
app.quit()
})
app.on('ready', () => {
mainWindow = new BrowserWindow({
transparent: true,
frame: false,
webPreferences: {
nodeIntegration: true,
webviewTag: true
}
});
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'web/index.html'),
protocol: 'file',
slashes: true
}));
mainWindow.webContents.openDevTools();
mainWindow.setFullScreen(true);
});
index.html
<!doctype html>
<html lang="en">
<head>
<title></title>
<link rel="stylesheet" href="styles/style.css">
</head>
<body>
<div class="container">
<button id="rec"> rec</button>
<button id="endrec"> end</button>
</div>
<script src="scripts/speech.js"></script>
</body>
</html>
speech.js
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.lang = 'pl-PL';
const rec = document.querySelector('#rec');
const endrec = document.querySelector('#endrec');
recognition.onstart = function () {
console.log('I started');
}
recognition.onend = function () {
console.log('I finished');
}
recognition.onresult = function () {
console.log('Take what I recorded');
console.log(event);
const current = event.resultIndex;
const transcript = event.results[current][0].transcript;
console.log(transcript);
}
rec.addEventListener('click', () => {
recognition.start();
console.log('You clicked me');
})
endrec.addEventListener('click', () => {
recognition.stop();
})
我也尝试过解决方案
webview.addEventListener('permissionrequest', function (e) {
if (e.permission === 'media') {
e.request.allow();
}
});
和
navigator.webkitGetUserMedia({ audio: true })
更新
我找到了停止识别的理由-错误-网络
3条答案
按热度按时间1rhkuytd1#
我想你会想在你的会话中使用
setPermissionRequestHandler
,像这样:编辑4/7/2023
现在看来,捕获音频的权限名为
desktopCapture
。vulvrdjw2#
我自己也遇到过类似的问题,发现谷歌不为基于CLI的应用程序提供语音API,比如electron。你最好的选择是使用Google Cloud Speech API或任何第三方API,比如Microsoft Cognitive Service。
f4t66c6m3#
你在MacOS上测试吗?我在MBP中遇到了类似的问题,下面的检查解决了这个问题-
有关更多详细信息,请参见Electron文档参考。