如何使用AVCaptureDeviceDiscoverySession获取前置摄像头,后置摄像头和音频

时间:2016-10-06 11:20:28

标签: ios swift avfoundation avcapturedevice

在iOS 10问世之前,我使用以下代码为我的录像机拍摄视频和音频:

 for device in AVCaptureDevice.devices()
 {
     if (device as AnyObject).hasMediaType( AVMediaTypeAudio )
     {
         self.audioCapture = device as? AVCaptureDevice
     }
     else if (device as AnyObject).hasMediaType( AVMediaTypeVideo )
     {
         if (device as AnyObject).position == AVCaptureDevicePosition.back
         {
             self.backCameraVideoCapture = device as? AVCaptureDevice
         }
         else
         {
             self.frontCameraVideoCapture = device as? AVCaptureDevice
         }
     }
 }

当iOS 10终于出现时,我在运行代码时收到以下警告。请注意,我的录像机仍能正常工作约2周。

  

'装置()'在iOS 10.0中已弃用:请改用AVCaptureDeviceDiscoverySession。

当我今天早上运行我的代码时,我的录像机停止了工作。 xCode8没有给我任何错误,但摄像头捕获的previewLayer是完全白色的。当我开始录制时,我收到以下错误:

  

错误域= AVFoundationErrorDomain代码= -11800"操作无法完成" UserInfo = {NSLocalizedDescription =操作无法完成,NSUnderlyingError = 0x17554440 {错误域= NSOSStatusErrorDomain代码= -12780"(null)"},NSLocalizedFailureReason =发生未知错误(-12780)}

我认为这与我使用弃用的方法AVCaptureDevice.devices()这一事实有关。因此,我想知道如何使用AVCaptureDeviceDiscoverySession代替?

提前感谢您的帮助!

12 个答案:

答案 0 :(得分:73)

您可以使用以下内容获取前置摄像头:

AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .front)

后置摄像头:

AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)

麦克风:

AVCaptureDevice.default(.builtInMicrophone, for: AVMediaType.audio, position: .unspecified)

答案 1 :(得分:12)

Swift 4,iOS 10+和 Xcode 10.1 取代

if let cameraID = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front)?.localizedName {
       //cameraID = "Front Camera"
}

使用 AVCaptureDevice.DiscoverySession 实施

if let cameraID = AVCaptureDevice.DiscoverySession.init(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: .video, position: .front).devices.first?.localizedName{
        //cameraID = "Front Camera"
} 

需要用 #available(iOS 10,*)检查来包装它。

答案 2 :(得分:11)

这是我的代码(Swift 3)获取相机位置:

// Find a camera with the specified AVCaptureDevicePosition, returning nil if one is not found
func cameraWithPosition(_ position: AVCaptureDevicePosition) -> AVCaptureDevice?
{
    if let deviceDescoverySession = AVCaptureDeviceDiscoverySession.init(deviceTypes: [AVCaptureDeviceType.builtInWideAngleCamera],
                                                          mediaType: AVMediaTypeVideo,
                                                          position: AVCaptureDevicePosition.unspecified) {

        for device in deviceDescoverySession.devices {
            if device.position == position {
                return device
            }
        }
    }

    return nil
}

如果需要,您还可以通过更改deviceTypes数组从iPhone 7+(双摄像头)获取新的devicesTypes。

这里有一个很好的阅读:https://forums.developer.apple.com/thread/63347

答案 3 :(得分:8)

适用于 Xcode 9.2和Swift 4

AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)

https://developer.apple.com/documentation/avfoundation/avcapturedevice/2361508-default

答案 4 :(得分:3)

Swift 3

用于选择后置摄像头:(也可根据需要更改.back)

为了选择另一个deviceType,将其添加到[]中(即:

[deviceTypeCamera,AVCaptureDeviceType.builtInMicrophone]

(或者创建一个私有的...就像我在带有后置摄像头的代码中所做的那样)

var $mainSearchForm = $('.form');
$mainSearchForm.on('change keyup', 'select input', function () {
  var infoForm = $mainSearchForm.serialize();
  var route = $('.form--url select').val();
  $('.btn').attr('href', route + '?' + infoForm);
});

答案 5 :(得分:3)

示例:iOS 11 Swift 4

override func viewDidLoad() {
    super.viewDidLoad()

    // Get the back-facing camera for capturing videos

    // AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)
    let deviceDiscoverySession = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)

   guard let captureDevice = deviceDiscoverySession else {
       print("Failed to get the camera device")
       return
   }

    do {
        // Get an instance of the AVCaptureDeviceInput class using the previous device object.
        let input = try AVCaptureDeviceInput(device: captureDevice)

        // Set the input device on the capture session.
        captureSession.addInput(input)

    } catch {
        // If any error occurs, simply print it out and don't continue any more.
        print(error)
        return
    }

    // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
    videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    videoPreviewLayer?.frame = view.layer.bounds
    view.layer.addSublayer(videoPreviewLayer!)

    // Start video capture.
    captureSession.startRunning()

答案 6 :(得分:2)

尝试下面的代码来获取相机ID:

NSString *cameraID = nil;

NSArray *captureDeviceType = @[AVCaptureDeviceTypeBuiltInWideAngleCamera];
AVCaptureDeviceDiscoverySession *captureDevice = 
              [AVCaptureDeviceDiscoverySession 
                discoverySessionWithDeviceTypes:captureDeviceType 
                mediaType:AVMediaTypeVideo 
                position:AVCaptureDevicePositionUnspecified];

cameraID = [captureDevice.devices.lastObject localizedName];

答案 7 :(得分:1)

对于我的视频捕捉应用程序,我使用以下代码来获取麦克风,前后摄像头,并且我已经在iOS 7到10.0.2中测试了此代码。

        var frontCamera : AVCaptureDevice?
        var rearCamera : AVCaptureDevice?

        captureSession = AVCaptureSession()

        let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)

        let audioDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeAudio)

        for mic in audioDevices {
            audioDevice = mic as? AVCaptureDevice
            audioCapturePossible = true
        }

        for device in devices {
            if device.position == AVCaptureDevicePosition.Front {
                frontCamera = device as? AVCaptureDevice
                hasFrontCamera = true
            }
            else if device.position == AVCaptureDevicePosition.Back {
                rearCamera = device as? AVCaptureDevice
                hasRearCamera = true
            }

        }

答案 8 :(得分:1)

Swift 4(xCode 10.1)

这是最新版本的Swift中对我有用的东西。我没有看到这个答案,花了我一段时间才解决,所以这里是如何获得前置摄像头的方法。

 if let device = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera , mediaType: AVMediaTypeVideo, position: .front)  {
    //Do the camera thing here..
}

答案 9 :(得分:0)

05/2019:

void Sort(List x)
{
start:
    Node current = x.head;
    Node previous = null;
    while (current != null && current.next != null)
    {
        if (StringComparer.Ordinal.Compare(current.data, current.next.data) < 0)
        {
            // Swap current and current.next nodes
            // We need to change three references
            if (previous != null)
            {
                previous.next = current.next;
            }
            else
            {
                x.head = current.next;
            }
            var temp = current.next.next;
            current.next.next = current;
            current.next = temp;
            goto start; // Restart the loop
        }
        // Advance previous and current references
        previous = current;
        current = current.next;
    }
}

答案 10 :(得分:0)

简体:

func getCamera(with position: AVCaptureDevice.Position) -> AVCaptureDevice? {
    let deviceDescoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: .video, position: .unspecified)
    return deviceDescoverySession.devices.first(where: { $0.position == position })
}

// and you can use like that:

guard let device = getCamera(with: .back) else { return }

答案 11 :(得分:0)

在 Swift 5 中更容易

AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .front)