juliansteenbakker / mobile_scanner

A universal scanner for Flutter based on MLKit. Uses CameraX on Android and AVFoundation on iOS.
BSD 3-Clause "New" or "Revised" License
906 stars 520 forks source link

Camera Preview Freezes upon Reopening MobileScanner Widget #976

Closed jeremyLACLICK closed 7 months ago

jeremyLACLICK commented 8 months ago

Hi there,

Thank you for the great package! Recently, the camera preview freezes whenever I open the widget containing it for the second time. I have the same issue when running my web app in the debugger and after deploying it. I also have this issue when running the project provided in the 'example' folder of this repository.

It is possible that the problem is due to an incompatibility with the most recent Dart update?

I noticed that when I pass the controller as a parameter of the Widget that contains the MobileScanner, the camera preview no longer freezes when it is opened for the second time. Does the issue then relate to the way I am using the MobileScannerController?

Test.txt

Best, Jeremy

navaronbracke commented 8 months ago

I noticed that when I pass the controller as a parameter of the Widget that contains the MobileScanner, the camera >preview no longer freezes when it is opened for the second time.

That sounds like the bug with the lifecycle management. Are you using version 4.0.x ? You could try validating with the new beta, 5.0.0-beta.1, which removes internal lifecycle management of the controller from the MobileScanner widget.

That bug caused issues with widget tree reparenting, so we removed the lifecycle management from the plugin.

You can use the updated example app from this beta version to validate. It also includes updates to the web support, using the new JS interop that was introduced with Dart 3.3

jeremyLACLICK commented 8 months ago

Hi there,

I am indeed using version 4.0.1. I will give the beta version a try, although I can see it is incompatible with another one of my project's dependencies.

Thank you in any case for the help!

lavinov-mercury commented 8 months ago

Seems like it's related not to lib changes, but to the Flutter upgrade. It started reproducing with 3.19.0 stable version, no matter which version of the lib is used (tried 5.0.0 as well). With 3.16.9 the issue is not reproducible for me. (all tested with the example app)

navaronbracke commented 8 months ago

Strange, that could be an issue with Platform Views then. I will investigate further.

jeremyLACLICK commented 8 months ago

Thanks!

romawizard commented 8 months ago

Any updates?

navaronbracke commented 8 months ago

I noticed that when I pass the controller as a parameter of the Widget that contains the MobileScanner, the camera preview no longer freezes when it is opened for the second time.

For anyone asking for updates: You should provide a MobileScannerController to the MobileScanner widget. This is the recommended way to do things and will be the only way to use the widget starting from version 5.0.0

wkok commented 7 months ago

I noticed that when I pass the controller as a parameter of the Widget that contains the MobileScanner, the camera preview no longer freezes when it is opened for the second time.

For anyone asking for updates: You should provide a MobileScannerController to the MobileScanner widget. This is the recommended way to do things and will be the only way to use the widget starting from version 5.0.0

Hello,

Providing a MobileScannerController to the MobileScanner widget works to not have the camera preview freeze when it's opened the second time, however, the actual scanning of a barcode only works on the first attempt of the preview being opened, not the second etc.. until I refresh the browser, the barcode scans the first time but then not again on subsequent attempts.

fg0611 commented 7 months ago

Hi I have a similar issue in Flutter Web. The camera stops/freezes after mounting the same widget that uses MobileScanner widget. I'm using the controller just like in the example for package version 5.0.0-beta.2 with everything else using WidgetsBindingObserver, StreamSubscription and the function called didChangeAppLifecycleState as is.

With package version 5.0.0-beta.2 I'm using flutter 3.19.5, dart 3.3.3.

Adding to that, I'm getting this browser message library@0.19.1:15 Trying to play video that is already playing. could it be related to the freezing?

I had the same problem using package version 4.0.1 and 3.5.7 with flutter older version and dart older version too.

joymyr commented 7 months ago

Im having the same issue as @fg0611, and it seems like web support was broken in 5.0.0-beta.1

navaronbracke commented 7 months ago

I did fix an issue with an unsupported MediaTrackSettings field earlier this morning. Did you get an Null is not a subtype of String error on the web?

I also need to look into an issue on Android, relating to the new lifecycle handling. On Android, the permission popup triggers a lifecycle change. Which, when combined with the recommendation on "restarting" the scanner when the app is resumed, results in a bug with the permission popup being requested again when tapping "Deny".

joymyr commented 7 months ago

No, I'm getting Trying to play video that is already playing even the first time calling start

navaronbracke commented 7 months ago

No, I'm getting Trying to play video that is already playing even the first time calling start

Then what did you mean by it seems like web support was broken in 5.0.0-beta.1 ? The message above seems harmless (although I still need to figure out what is causing it)

fg0611 commented 7 months ago

I'm using the example with controller. To reproduce:

joymyr commented 7 months ago

Sorry, I just assumed the message was related to why it isn't working The issue is that the MobileScanner widget is just displaying a black screen, with an exclamation mark inside. I'm not seeing any other error messages.

Skjermbilde 2024-04-08 kl  22 46 14
fg0611 commented 7 months ago

Sorry, I just assumed the message was related to why it isn't working The issue is that the MobileScanner widget is just displaying a black screen, with an exclamation mark inside. I'm not seeing any other error messages. Skjermbilde 2024-04-08 kl 22 46 14

may I ask if you inserted ` <script src="https://unpkg.com/@zxing/library@0.19.1" type="application/javascript"

` inside index.html file?

navaronbracke commented 7 months ago

Sorry, I just assumed the message was related to why it isn't working The issue is that the MobileScanner widget is just displaying a black screen, with an exclamation mark inside. I'm not seeing any other error messages. Skjermbilde 2024-04-08 kl 22 46 14

Do you have errors in the browser console?

The white exclamation mark on the black background is the default error builder for the MobileScanner() widget. Can you pass a custom widget, and then get the stack trace from the error details of the MobileScannerException, or Exception if for some reason we didn't catch it properly.

joymyr commented 7 months ago

I tried to deploy to a server now, and it works, so the issue seems to be only when running on localhost/debug mode. Still having the freezing issue on reopening though. When debugging locally I get no errors in the console, but from the errorBuilder I get TypeError: null: type 'Null' is not a subtype of type 'String'.

navaronbracke commented 7 months ago

I tried to deploy to a server now, and it works, so the issue seems to be only when running on localhost/debug mode. Still having the freezing issue on reopening though. When debugging locally I get no errors in the console, but from the errorBuilder I get TypeError: null: type 'Null' is not a subtype of type 'String'.

Yeah, the TypeError: null: type 'Null' is not a subtype of type 'String' is the issue I fixed locally. I still want to prepare some other changes for beta 3 (the iOS privacy manifest being one of those)

I currently have the local fix on

  mobile_scanner:
    git:
      url: https://github.com/navaronbracke/mobile_scanner.git
      ref: 5_0_0-beta-3

if you want to tinker with it.

navaronbracke commented 7 months ago

I'm using the example with controller. To reproduce:

  • Home() with a Nav.push(cxt, scannerWidget)
  • go back with a button Nav.pop()
  • then click again to see scanner (it appears freezed).

I can reproduce this bug with the example app. I don't see any errors in the console, except for a Canvas2D warning.

navaronbracke commented 7 months ago

Small update: I managed to fix the "frozen" camera preview locally. Still need to figure out why the ZXing barcode reader is not scanning the frames on the second attempt, but once that is done, this bug should be fixed.

joymyr commented 7 months ago

Great, both the freezing and the TypeError: null is fixed with beta 3

fg0611 commented 7 months ago

Small update: I managed to fix the "frozen" camera preview locally. Still need to figure out why the ZXing barcode reader is not scanning the frames on the second attempt, but once that is done, this bug should be fixed.

Thank you in advance!. If it's not too much to ask, could you please leave a route with a working demo and access to the code?