     }
 
     //MARK: Utility Functions
-    func deviceWithMediaType(_ mediaType : AVMediaType, preferringPosition: AVCaptureDevice.Position) -> AVCaptureDevice{
-        let devices : [AVCaptureDevice] = AVCaptureDevice.devices(for: mediaType)
-        var captureDevice: AVCaptureDevice = devices.first as! AVCaptureDevice
+    func deviceWithMediaType(_ mediaType: NSString, preferringPosition: AVCaptureDevicePosition) -> AVCaptureDevice{
+        let devices: NSArray = AVCaptureDevice.devices(withMediaType: mediaType as String) as NSArray
+        var captureDevice: AVCaptureDevice = devices.firstObject as! AVCaptureDevice
 
         for device in devices{
             if((device as AnyObject).position == preferringPosition){

        do{
            try device.lockForConfiguration();
            if(backCameraDevice!.hasFlash){
                if(backCameraDevice!.isFlashModeSupported(AVCaptureDevice.FlashMode.auto)){
                    device.flashMode = AVCaptureDevice.FlashMode.auto;
                    let image = UIImage(named: "camera_flash_auto.png") as UIImage!
                    flashUIButton.setImage(image, for: UIControlState())
                }
                if(backCameraDevice!.isTorchModeSupported(AVCaptureDevice.TorchMode.off)){
                    device.torchMode = AVCaptureDevice.TorchMode.off;
                }
            }else{
                let image = UIImage(named: "camera_flash_off.png") as UIImage!

                try device.lockForConfiguration();

                if(flashState == 0){
                    if(backCameraDevice!.isFlashModeSupported(AVCaptureFlashMode.off)){
                        device.flashMode = AVCaptureFlashMode.off;
                        let image = UIImage(named: "camera_flash_off.png") as UIImage!
                        flashUIButton.setImage(image, for: UIControlState())
                    }
                    if(backCameraDevice!.isTorchModeSupported(AVCaptureTorchMode.off)){
                        device.torchMode = AVCaptureTorchMode.off;
                    }
                    flashState = 1;
                }

                else if(flashState == 1){
                    if(backCameraDevice!.isFlashModeSupported(AVCaptureFlashMode.auto)){
                        device.flashMode = AVCaptureFlashMode.auto;
                        let image = UIImage(named: "camera_flash_auto.png") as UIImage!
                        flashUIButton.setImage(image, for: UIControlState())
                    }
                    if(backCameraDevice!.isTorchModeSupported(AVCaptureTorchMode.off)){
                        device.torchMode = AVCaptureTorchMode.off;
                    }
                    flashState = 2
                }
                else if(flashState == 2){
                    if(backCameraDevice!.isFlashModeSupported(AVCaptureFlashMode.off)){
                        device.flashMode = AVCaptureFlashMode.off;
                        let image = UIImage(named: "camera_flash_on.png") as UIImage!
                        flashUIButton.setImage(image, for: UIControlState())
                    }
                    if(backCameraDevice!.isTorchModeSupported(AVCaptureTorchMode.on)){
                        device.torchMode = AVCaptureTorchMode.on;
                    }

                    flashState = 0;
