open method
Opens the camera with the specified index.
Implementation
@override
Future<bool> open(int index) async {
await release();
if (_videoDevices.isEmpty) {
await getDeviceList();
}
if (index >= _videoDevices.length) {
return false;
}
final deviceId = _videoDevices[index].deviceId;
final constraints = dom.MediaStreamConstraints(
video: dom.MediaTrackConstraints(deviceId: deviceId.toJS),
audio: false.toJS,
);
try {
_stream = await dom.window.navigator.mediaDevices
.getUserMedia(constraints)
.toDart;
} catch (e) {
_stream = null;
return false;
}
final tracks = _stream!.getVideoTracks().toDart;
if (tracks.isEmpty) {
_stopStream();
return false;
}
// The video element feeds both the platform-view preview and the
// captureFrame() draws. muted + playsinline is required for autoplay.
_videoElement = dom.HTMLVideoElement()
..autoplay = true
..muted = true
..setAttribute('playsinline', '')
..srcObject = _stream;
await _play();
return true;
}