0 Replies
      Latest reply on Mar 15, 2019 3:12 AM by Ferdi
      Ferdi Level 2 Level 2 (40 points)

        How to access the camera in Swift playgrounds on Mac OS Mojave? This used to work with no problem on High Sierra however the code does not seem to execute correctly any more as the playgrounds running symbol is spinning continuously as well as the camera lights staying off.

        I got an example of the code which used to work fine last year but not any more today does anyone know what's wrong and how to fix it?

         

         

        import Cocoa
        import AVFoundation
        import AVKit
        import QuartzCore
        import PlaygroundSupport
        
        let view = NSView(frame: NSRect(x: 0.0, y: 0.0, width: 640.0, height: 480.0))
        
        let session = AVCaptureSession()
        
        session.sessionPreset = AVCaptureSession.Preset.vga640x480
        session.beginConfiguration()
        session.commitConfiguration()
        
        
        var input : AVCaptureDeviceInput
        if let devices : [AVCaptureDevice] = AVCaptureDevice.devices() as? [AVCaptureDevice] {
        for device in devices {
            if device.hasMediaType(AVMediaType.video) && device.supportsSessionPreset(AVCaptureSession.Preset.vga640x480) {
                do {
                    input = try AVCaptureDeviceInput(device: device as AVCaptureDevice) as AVCaptureDeviceInput
        
                    if session.canAddInput(input) {
                        session.addInput(input)
                        break
                    }
                }
                catch {
                    error
                }
            }
        }
        
        let output = AVCaptureVideoDataOutput()
        output.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable as! String: Int(kCVPixelFormatType_32BGRA)]
        output.alwaysDiscardsLateVideoFrames = true
        
        if session.canAddOutput(output) {
            session.addOutput(output)
        }
        
        let captureLayer = AVCaptureVideoPreviewLayer(session: session)
        view.wantsLayer = true
        view.layer = captureLayer
        
        session.startRunning()
        
        //View -> Assistant Editor -> Show Assistant Editor
        PlaygroundPage.current.liveView = view
        
        }
         

        Looked at apples documentation on how to request access to the camera I tried it however it does not seem to work in playgrounds as I'm still having the same issue when this is implemented.

        https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/requesting_authorization_for_media_capture_on_macos

         

         

        switch AVCaptureDevice.authorizationStatus(for: .video) {
        case .authorized: // The user has previously granted access to the camera.
            self.setupCaptureSession()
        
        case .notDetermined: // The user has not yet been asked for camera access.
            AVCaptureDevice.requestAccess(for: .video) { granted in
                if granted {
                    self.setupCaptureSession()
                }
            }
        
        case .denied: // The user has previously denied access.
            return
        case .restricted: // The user can't grant access due to restrictions.
            return
        }