Proximity state or Proximity sensor

The proximity state does not print the message back, in other words, it is not working or I just simply implemented the wrong way, who can help me?

The code is between lines 25 and 30… Thank you.

 


/
/ 
import Foundation
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController{
   
   
    func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
       
    }
    /
    @IBOutlet weak var previewView: UIView!
    var captureDevice: AVCaptureDevice?
    let captureSession = AVCaptureSession()
    var previewLayer : AVCaptureVideoPreviewLayer?
    let device = UIDevice.current
   
    override func viewDidLoad() {
        super.viewDidLoad()
       
    /
        device.isProximityMonitoringEnabled = true
       
        if  device.proximityState == true{
           
            print("It works")
        }
    /
        let devices = AVCaptureDevice.devices()
       
        for device in devices {
            if device.hasMediaType(AVMediaType.video){
               
                if device.position == AVCaptureDevice.Position.back {
                    captureDevice = device as? AVCaptureDevice
                   
                    if captureDevice != nil{
                       
                        print("Capture device found")
                        beginSession()
                    }
                   
                }
            }
        }
       
    }
    /
   
    func beginSession() {
        configureDevice()
       
        let err : NSError? = nil
        do {
            try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice!))
        }catch{
            print ("Error")
        }
        if err != nil {
            print ("error: (err?.localizedDescription)")
        }
        /
        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer?.frame = (previewLayer?.bounds)!
        previewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
        previewLayer?.videoGravity = AVLayerVideoGravity.resize
        previewView.layer.addSublayer(previewLayer!)
        captureSession.startRunning()
       
    }
   
    func configureDevice() {
        if let device = captureDevice{
            do{
                try device.lockForConfiguration()
               
            }catch{
                print("error")
            }
        }
    }
   
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        previewLayer!.frame = previewView.bounds
       
    }
   
    func recordAcation (){
       
       
    }
}

Powered by WPeMatico

About

You may also like...

Comments are closed.