简体   繁体   English

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

[英]How to grant permission to audio in electron app in Windows?

I'm trying to implement speech recognition into the electron application.我正在尝试在电子应用程序中实现语音识别。 The solution works in chrome browser, but does not work in electron.该解决方案适用于 chrome 浏览器,但不适用于电子。 The application stops listening immediately - it probably has no microphone permission.应用程序立即停止收听 - 它可能没有麦克风权限。 How to grant permissions?如何授予权限?

index.js索引.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索引.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语音.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();
    })

I've also tried solutions with我也尝试过解决方案

webview.addEventListener('permissionrequest', function (e) {
     if (e.permission === 'media') {
         e.request.allow();
    }
});

and

navigator.webkitGetUserMedia({ audio: true })

UPDATE更新


I found a reason to stop recognizing - error - network我找到了停止识别的原因 - 错误 - 网络

在此处输入图片说明

I think you'll want to use the setPermissionRequestHandler on your session, like this:我想你会想在你的会话中使用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
        }
    });
});

I've faced similar problem myself and found out that google doesn't provide speechAPI for CLI based apps like electron.我自己也遇到过类似的问题,并发现谷歌没有为基于 CLI 的应用程序(如电子)提供语音 API。 Your best bet is to use either Google Cloud Speech API or any third-party API like Microsoft Cognitive Service .您最好的选择是使用Google Cloud Speech API或任何第三方 API,例如Microsoft Cognitive Service

Are you testing on MacOS?你是在 MacOS 上测试吗? I faced a similar issue in my MBP, and below check solved the issue -我在 MBP 中遇到了类似的问题,下面的检查解决了这个问题 -

systemPreferences.askForMediaAccess(microphone)

See the Electron doc reference for additional details.有关其他详细信息,请参阅电子文档参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM