3

I have a browser_action extension where the user can press start and stop in order to capture some audio input. After the file has been recorded I would like to dump its url in the console. The problem is that I cannot get access to the microphone. This is what I have tried so far:

navigator.webkitGetUserMedia - does not work, navigator.webkitGetUserMedia({ audio: true },...); calls the error callback with a MediaDeviceFailedDueToShutdown. I tried looking into that error but I found nothing useful about that. That error is nowhere to be found.

Could you please guide me to the right path? Thank you.

Teo
  • 3,394
  • 11
  • 43
  • 73

2 Answers2

11

So it turns out that I have to get the user media from within an html page that is baked into the extension itself. After the user has given access to the microphone, the background script of the extension also gets access to it.

In my case, after installing I launch the welcome.html page where access is being requested:

background.js

chrome.runtime.onInstalled.addListener((details) => {
    if (details.reason.search(/install/g) === -1) {
        return
    }
    chrome.tabs.create({
        url: chrome.runtime.getURL("welcome.html"),
        active: true
    })
})

welcome.js

navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {...})
.catch(err => {...})
Teo
  • 3,394
  • 11
  • 43
  • 73
  • 6
    at least this explain the hell i've been through... did you manage to find something better? – Roey May 25 '17 at 13:01
  • For now this seems to be the only way that works for me(tested on my Chrome 80.0.3987.122 on macOS 10.15.2). While this requires an extra page after user installs the extension. Does anyone know any other alternative means to hack this? I messed with options and popup while no luck. – fishstick Mar 17 '20 at 03:56
  • Maybe this can help: https://stackoverflow.com/questions/50991321/chrome-extension-getusermedia-throws-notallowederror-failed-due-to-shutdown – Rajat Verma Nov 30 '20 at 12:43
-2

Try adding "audioCapture" to your permissions in manifest.json:

"permissions": ["fullscreen", "audioCapture"]
Flawsome
  • 57
  • 7
  • 1
    This permission is for Chrome Apps, not for Chrome Extensions as OP requested. https://developer.chrome.com/extensions/declare_permissions https://developer.chrome.com/apps/declare_permissions – FrakyDale Jul 24 '18 at 08:42