简体   繁体   中英

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. The application stops listening immediately - it probably has no microphone permission. How to grant permissions?

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();
    })

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:

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. Your best bet is to use either Google Cloud Speech API or any third-party API like Microsoft Cognitive Service .

Are you testing on MacOS? I faced a similar issue in my MBP, and below check solved the issue -

systemPreferences.askForMediaAccess(microphone)

See the Electron doc reference for additional details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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