5

I've managed to find some code that would give me access to the devices of a phone (such as the camera). The issue is that when I compile the code (and I'm printing the different devices) using Xcode, I get an empty array.

Here is what I wrote:

import UIKit
import AVFoundation

class ViewController: UIViewController {

  let captureSession = AVCaptureSession()
  var previewLayer : AVCaptureVideoPreviewLayer?

  // If we find a device we'll store it here for later us
  var captureDevice : AVCaptureDevice?

  override func viewDidLoad() {
    super.viewDidLoad()            
    // Do any additional setup after loading the view, typically from a nib.

    captureSession.sessionPreset = AVCaptureSessionPresetHigh

    let devices = AVCaptureDevice.devices()
    println(devices)
    // Loop through all the capture devices on this phone
    for (device in devices) {
        // Make sure this particular device supports video
        if (device.hasMediaType(AVMediaTypeVideo)) {
         // Finally check the position and confirm we've got the back camera
            if(device.position == AVCaptureDevicePosition.Back) {
                captureDevice = device as? AVCaptureDevice
                if captureDevice != nil {
                    println("Capture device found")
                    beginSession()
                }
            }
        }
      }

    }

    func beginSession() {

      var err : NSError? = nil
      captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err))

      if err != nil {
          println("error: \(err?.localizedDescription)")
      }

      previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
      self.view.layer.addSublayer(previewLayer)
      previewLayer?.frame = self.view.layer.frame

      captureSession.startRunning()

    }

  }

Do you have any ideas as to why I am getting an empty array?

Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
Tricks
  • 51
  • 1
  • 4

3 Answers3

7

If you're only running it in the simulator, the array will always be empty because it has no physical hardware to choose from. In fact, if you try to access physical hardware inside the Simulator, it will crash. If you plug a device in and still get an empty array, let me know.

pbush25
  • 5,228
  • 2
  • 26
  • 35
  • 1
    Yes I am because the code that was posted is very similar to what is in Apple's docs, and a common reason to not find abolutely _zero_ `AVCaptureDevices` is because it's being run in the simulator. Here are the docs with code like OP's: https://developer.apple.com/library/prerelease/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html – pbush25 Jun 29 '15 at 15:33
  • 2
    This is absolutely the correct answer.. Who in the world downvoted this?? – rigdonmr Feb 06 '16 at 21:50
4

first check the current status of the authorization

AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)

more detail you can read this article

nathanwhy
  • 5,884
  • 1
  • 12
  • 13
0

Check the current Status of Authorization for Swift 4

 AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
The Doctor
  • 486
  • 1
  • 6
  • 16
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. [How do I write a good answer? ](https://stackoverflow.com/help/how-to-answer) – mrun Apr 19 '18 at 08:09