首页 > 解决方案 > 'audioCapture' 只允许用于打包的应用程序,但这是一个扩展

问题描述

我正在尝试编写一个需要使用麦克风权限的谷歌扩展程序。但是,我收到以下错误:

'audioCapture' 只允许用于打包的应用程序,但这是一个扩展。

以下是我的 manifest.json 代码:

{
"update_url": "https://clients2.google.com/service/update2/crx",
"version": "1.1.1",
"manifest_version": 2,
"name": "Google Meet Push To Talk",
"description": "Hold <space> in Google Meet to talk instead of fumbling around trying to mute and unmute",
"icons": {
  "16": "icons/monkey16.png",
  "48": "icons/monkey48.png",
  "128": "icons/monkey128.png"
},
"content_scripts": [
  {
    "matches": [
      "https://meet.google.com/*"
    ],
    "js": [
      "ptt.bundle.js"
    ]
  }
],
"background": {
  "scripts": [
    "background.bundle.js"
  ],
  "persistent": false
},
"page_action": {
  "default_popup": "options.html"
},
"options_page": "options.html",
"permissions": [
  "storage",
  "declarativeContent",
  "audioCapture"
]

}

标签: javascriptgoogle-chromegoogle-chrome-extensionmicrophonemanifest.json

解决方案


您可以使用 MediaDevices.getUserMedia() 或 navigator.webkitGetUserMedia() 录制麦克风音频。但我认为您需要在内容脚本中使用此方法。

例子

let recorderConfig = {
        audio: {
            sampleRate: 48000,
            channelCount: 2,
            volume: 1.0
        },
        video: false,

    }
    navigator.webkitGetUserMedia(recorderConfig, (stream) => {
        const mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm' });
        const audioChunks = [];
        mediaRecorder.addEventListener("dataavailable", event => {
            audioChunks.push(event.data);
            var blob = new Blob(audioChunks, {
                type: 'audio/x-mpeg-3'
            });
            let audioName = document.title.concat(".mp3");
            // to download the recorded file as mp3
            var url = URL.createObjectURL(blob);
            var a = document.createElement('a');
            document.body.appendChild(a);
            a.style = 'display: none';
            a.href = url;
            a.download = audioName
            a.click();
            window.URL.revokeObjectURL(url);
            sendAudioToServer(blob, audioName)
        });
        mediaRecorder.start();

        // recording stop handler
        chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
            if (request.msg == "stop-record") {
                mediaRecorder.stop();
                stream.getAudioTracks()[0].stop();
                sendResponse({ response: "record ended" });
                return true;
            }
        })
    }, (error) => {
        console.log(error)
    });
}

推荐阅读