electron 如何在Windows中授予电子应用程序中的音频权限?

db2dz4w8  于 2023-04-09  发布在  Electron
关注(0)|答案(3)|浏览(302)

我正在尝试将语音识别实现到电子应用程序中。该解决方案适用于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 })

更新
我找到了停止识别的理由-错误-网络

1rhkuytd

1rhkuytd1#

我想你会想在你的会话中使用setPermissionRequestHandler,像这样:

const electron = require('electron');
const url = require('url');
const path = require('path');

const {
    app,
    BrowserWindow,
    ipcMain,
    session
} = 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);

    session.fromPartition("default").setPermissionRequestHandler((webContents, permission, callback) => {
        let allowedPermissions = ["audioCapture"]; // Full list here: https://developer.chrome.com/extensions/declare_permissions#manifest

        if (allowedPermissions.includes(permission)) {
            callback(true); // Approve permission request
        } else {
            console.error(
                `The application tried to request permission for '${permission}'. This permission was not whitelisted and has been blocked.`
            );

            callback(false); // Deny
        }
    });
});

编辑4/7/2023

现在看来,捕获音频的权限名为desktopCapture

vulvrdjw

vulvrdjw2#

我自己也遇到过类似的问题,发现谷歌不为基于CLI的应用程序提供语音API,比如electron。你最好的选择是使用Google Cloud Speech API或任何第三方API,比如Microsoft Cognitive Service

f4t66c6m

f4t66c6m3#

你在MacOS上测试吗?我在MBP中遇到了类似的问题,下面的检查解决了这个问题-

systemPreferences.askForMediaAccess(microphone)

有关更多详细信息,请参见Electron文档参考。

相关问题