microphonePermission static method
Requests the permission to use a microphone and holds it until unsubscribed.
Implementation
static Future<StreamSubscription<void>> microphonePermission() async {
bool granted = false;
// Firefox doesn't allow to check whether app has microphone permission:
// https://searchfox.org/mozilla-central/source/dom/webidl/Permissions.webidl#10
if (!isFirefox) {
final permission =
await web.window.navigator.permissions
.query({'name': 'microphone'}.jsify() as JSObject)
.toDart;
granted = permission.state == 'granted';
}
if (!granted) {
final web.MediaStream stream =
await web.window.navigator.mediaDevices
.getUserMedia(web.MediaStreamConstraints(audio: true.toJS))
.toDart;
if (isFirefox) {
final StreamController controller = StreamController(
onCancel: () {
for (var e in stream.getTracks().toDart) {
e.stop();
}
},
);
return controller.stream.listen((_) {});
} else {
for (var e in stream.getTracks().toDart) {
e.stop();
}
}
}
return (const Stream.empty()).listen((_) {});
}