mobile_scanner icon indicating copy to clipboard operation
mobile_scanner copied to clipboard

Camera Preview Freezes upon Reopening MobileScanner Widget

Open jeremyLACLICK opened this issue 11 months ago • 16 comments

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

jeremyLACLICK avatar Mar 06 '24 12:03 jeremyLACLICK

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

navaronbracke avatar Mar 06 '24 13:03 navaronbracke

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!

jeremyLACLICK avatar Mar 07 '24 07:03 jeremyLACLICK

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)

lavinov-mercury avatar Mar 11 '24 12:03 lavinov-mercury

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

navaronbracke avatar Mar 11 '24 16:03 navaronbracke

Thanks!

jeremyLACLICK avatar Mar 18 '24 09:03 jeremyLACLICK

Any updates?

romawizard avatar Mar 29 '24 11:03 romawizard

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

navaronbracke avatar Mar 29 '24 12:03 navaronbracke

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.

wkok avatar Apr 04 '24 15:04 wkok

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 [email protected]: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.

fg0611 avatar Apr 05 '24 19:04 fg0611

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

joymyr avatar Apr 08 '24 13:04 joymyr

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".

navaronbracke avatar Apr 08 '24 14:04 navaronbracke

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

joymyr avatar Apr 08 '24 20:04 joymyr

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)

navaronbracke avatar Apr 08 '24 20:04 navaronbracke

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).

fg0611 avatar Apr 08 '24 20:04 fg0611

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

joymyr avatar Apr 08 '24 20:04 joymyr

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/[email protected]" type="application/javascript" ></script> inside index.html file?

fg0611 avatar Apr 08 '24 22:04 fg0611

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.

navaronbracke avatar Apr 09 '24 06:04 navaronbracke

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'.

joymyr avatar Apr 09 '24 07:04 joymyr

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 avatar Apr 09 '24 07:04 navaronbracke

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 avatar Apr 09 '24 08:04 navaronbracke

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.

navaronbracke avatar Apr 09 '24 13:04 navaronbracke

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

joymyr avatar Apr 09 '24 21:04 joymyr

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?

fg0611 avatar Apr 10 '24 14:04 fg0611