如何在Windows中的电子应用程序中授予音频权限? [英] How to grant permission to audio in electron app in Windows?

查看:57
本文介绍了如何在Windows中的电子应用程序中授予音频权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在电子应用程序中实现语音识别.该解决方案可在chrome浏览器中使用,但不适用于电子设备.该应用程序立即停止监听-它可能没有麦克风权限.如何授予权限?

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

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

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

更新

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

I found a reason to stop recognizing - error - network

推荐答案

我自己也遇到了类似的问题,结果发现Google并未为基于CLI的应用程序(例如electronic)提供SpeechAPI.最好的选择是使用 Google Cloud Speech API 或任何第三方API像 Microsoft认知服务.

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.

这篇关于如何在Windows中的电子应用程序中授予音频权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆