       }
     }
 
-
-    metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
-    metadataOutput.metadataObjectTypes = metadataObjectTypes
-    previewLayer.videoGravity          = AVLayerVideoGravity.resizeAspectFill
+    // Add metadata output
+    session.addOutput(metadataOutput)
+    metadataOutput.setMetadataObjectsDelegate(self, queue: metadataObjectsQueue)
+    metadataOutput.metadataObjectTypes = metadataOutput.availableMetadataObjectTypes//metadataObjectTypes
+    previewLayer.videoGravity          = .resizeAspectFill
+
+    session.commitConfiguration()
   }
 
   /// Switch between the back and the front camera.


    super.init()

    configureDefaultComponents(withCaptureDevicePosition: captureDevicePosition)
  }

  // MARK: - Initializing the AV Components

  private func configureDefaultComponents(withCaptureDevicePosition: AVCaptureDevice.Position) {
    session.addOutput(metadataOutput)

    switch withCaptureDevicePosition {
    case .front:
      if let _frontDeviceInput = frontDeviceInput {

   */
  public func startScanning() {
    if !session.isRunning {
      sessionQueue.async {
        self.session.startRunning()
      }
    }
  }

  /// Stops scanning the codes.
  public func stopScanning() {
    if session.isRunning {
      sessionQueue.async {
        self.session.stopRunning()
      }
    }
  }

