requestMicAccess method
Request microphone permission. Returns true if permission is granted, false otherwise. Documentation: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/permissions/request Documentation: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia This is a 'hack' to acquire media permissions. The permissions API is not supported in all browsers.
Implementation
@override
Future<bool?> requestMicAccess() async {
logLocalEvent("requesting mic permission");
try {
final isSafariOrFirefox = RegExp(r'^((?!chrome|android).)*safari|firefox', caseSensitive: false).hasMatch(_webNavigatorDelegate.userAgent);
if (isSafariOrFirefox) {
try {
// `Permissions.request` is a non-standard (Firefox) API not exposed by
// package:web, so invoke it dynamically to preserve prior behavior.
final descriptor = {"name": "microphone"}.jsify() as JSObject;
final result = await _webPermissionsDelegate.callMethod<JSPromise<web.PermissionStatus>>("request".toJS, descriptor).toDart;
if (result.state == "granted") return true;
} catch (e) {
printDebug("Failed to request microphone permission");
printDebug(e);
}
}
// Default approach for all browsers (and fallback for Safari & Firefox)
/// This dirty hack to get media stream. Request (to show permissions popup on Chrome
/// and other browsers, then stop the stream to release the permission)
/// TODO(cybex-dev) - check supported media streams
final web.MediaStream mediaStream = await _webMediaDevicesDelegate.getUserMedia(web.MediaStreamConstraints(audio: true.toJS)).toDart;
mediaStream.getTracks().toDart.forEach((track) => track.stop());
return hasMicAccess();
} catch (e) {
printDebug("Failed to request microphone permission");
printDebug(e);
return false;
}
}