Im using the 'camera.dart' package to access the web cam. It works perfectly fine on the machine browser, yet the issue arise once it comes to the mobile. Even on android it works. But on iOS after asking the permission the camera stream is not loading. Any help would much appreciate to spot the part I'm missing. My code as follows in the Camera class.
String getRandString() {
var random = Random.secure();
var values = List<int>.generate(8, (i) => random.nextInt(255));
return base64UrlEncode(values);
}
Class implementation
class _WebcamPageState extends State<WebcamPage> {
// VideoElement
late final VideoElement _webcamVideoElement;
final String videoTag = getRandString();
@override
void initState() {
super.initState();
// Create an video element which will be provided with stream source
_webcamVideoElement = VideoElement();
// Register an webcam
ui.platformViewRegistry.registerViewFactory(
videoTag, (int viewId) => _webcamVideoElement);
loadCameraStream();
}
// I suppose the issue is here
void loadCameraStream() {
var constraints = {
"audio": false,
"video": {
'mandatory':
{ 'minAspectRatio': 1.333, 'maxAspectRatio': 1.334 },
'optional':
[{ 'minFrameRate': 60 },
{ 'maxWidth': 400 }]
}
};
window.navigator.mediaDevices?.getUserMedia(constraints).then((MediaStream stream) {
_webcamVideoElement.srcObject = stream;
_webcamVideoElement.play();
});
}
@override
Widget build(BuildContext context) =>
Scaffold(
body: SingleChildScrollView(
child: Container(
height: 500.0,
child: HtmlElementView(key: UniqueKey(),
viewType:videoTag),
)
)//SingleChildScrollView
);//Scaffold
}
I have my dispose method separately
@override
void dispose() {
if (_webcamVideoElement.srcObject != null &&
(_webcamVideoElement.srcObject!.active ?? false)) {
_webcamVideoElement.pause();
var tracks = _webcamVideoElement.srcObject?.getTracks();
_webcamVideoElement.srcObject = null;
tracks?.forEach((track) {
track.stop();
});
}
super.dispose();
}