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


      session.commitConfiguration()
    }
    return session.inputs.first as? AVCaptureDeviceInput
  }



/// Reader object base on the `AVCaptureDevice` to read / scan 1D and 2D codes.
public final class QRCodeReader: NSObject, AVCaptureMetadataOutputObjectsDelegate {
  private let sessionQueue         = DispatchQueue(label: "session queue")
  private let metadataObjectsQueue = DispatchQueue(label: "com.yannickloriot.qr", attributes: [], target: nil)
  
  var defaultDevice: AVCaptureDevice = AVCaptureDevice.default(for: AVMediaType.video)!
  var frontDevice: AVCaptureDevice?  = {
    if #available(iOS 10, *) {
      return AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .front)
    }
    else {
      for device in AVCaptureDevice.devices(for: AVMediaType.video) {
        if device.position == .front {
          return device
        }
      }
