【问题标题】:How to grant permission to audio in electron app in Windows?如何在 Windows 的电子应用程序中授予音频权限?
【发布时间】:2020-06-29 02:46:06
【问题描述】:

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

更新


我找到了停止识别的原因 - 错误 - 网络

【问题讨论】:

  • 我遇到了同样的问题,你最后找到解决这个问题的方法了吗?
  • 这个问题有什么解决办法吗?
  • 很遗憾没有
  • 我在 Linux 上遇到了类似的问题。你找到解决办法了吗?
  • 很遗憾没有

标签: javascript audio permissions electron microphone


【解决方案1】:

我想你会想在你的会话中使用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
        }
    });
});

【讨论】:

【解决方案2】:

我自己也遇到过类似的问题,发现谷歌没有为基于 CLI 的应用程序(如电子)提供语音 API。最好的办法是使用 Google Cloud Speech API 或任何第三方 API,例如 Microsoft Cognitive Service

【讨论】:

    【解决方案3】:

    您是否在 MacOS 上进行测试?我在 MBP 中遇到了类似的问题,下面的检查解决了这个问题 -

    systemPreferences.askForMediaAccess(microphone)
    

    有关更多详细信息,请参阅Electron doc reference

    【讨论】:

    • 不幸的是我在 Windows 上测试
    猜你喜欢
    • 2012-04-29
    • 2020-01-03
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    相关资源
    最近更新 更多