javascript - '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"
]
}
解决方案
您可以使用 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)
});
}
推荐阅读
- preact - 如何修复 preact 组件不会在具有多个 setState 的 unistore 操作上重新渲染?
- python - 将字符串中的最后一个字母附加到另一个字符串
- javascript - 为什么尝试建立网络套接字时连接被拒绝?
- c++ - 向量 v 的 v[0]、v.begin() 和 v.data() 有什么区别?
- java - Kotlin 找不到正确设置 JAVA_HOME 的所需 JDK 工具
- bash - 重命名 perl 工具:无法重命名,没有这样的文件或目录
- javascript - Highchart中的多个标签问题
- c# - Windows 窗体,哪个是用于接收/存储 ID 并返回整数的正确数据结构定义?
- android - 使用 Either 进行改造 2 响应
- php - Laravel 按值从 api 获取数据