之前的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 “(空)”},NSLocalizedFailureReason =一个出现未知错误(-12780)}
我相信,有事情做与我使用的是过时的做法AVCaptureDevice.devices()
的事实。因此,我想知道如何使用代替AVCaptureDeviceDiscoverySession
?
提前谢谢你的帮助!
你可以得到的前置摄像头具有以下内容:
AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .front)
相机背面:
AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)
和麦克风:
AVCaptureDevice.default(.builtInMicrophone, for: AVMediaType.audio, position: .unspecified)
05/2019 :
//video
self.session = AVCaptureSession()
guard
let videoDeviceInput = try? AVCaptureDeviceInput(device: device!),
self.session!.canAddInput(videoDeviceInput)
else { return }
self.session!.addInput(videoDeviceInput)
//audio
guard
let audioDeviceInput = try? AVCaptureDeviceInput(device: mic!),
self.session!.canAddInput(audioDeviceInput)
else { return }
self.session!.addInput(audioDeviceInput)
斯威夫特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,*)检查。
这里是我的代码(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。
它的工作原理上的Xcode 9.2和斯威夫特4
AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)
https://developer.apple.com/documentation/avfoundation/avcapturedevice/2361508-default
斯威夫特3
对于选择相机背面:(也可以根据需要改变.back)
用于选择另一个设备类型简单添加它内部的[](即:
[deviceTypeCamera,AVCaptureDeviceType.builtInMicrophone]
(或创建一个私人让利......像我与背部摄像头的代码所做的那样)
private let position = AVCaptureDevicePosition.back
private let deviceTypeBackCamera = AVCaptureDeviceType.builtInWideAngleCamera
private func selectCaptureDevice() -> AVCaptureDevice? {
return AVCaptureDeviceDiscoverySession(deviceTypes: [deviceTypeBackCamera], mediaType: AVMediaTypeVideo, position: position).devices.first
}
例如:11的iOS夫特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()
尝试下面的代码来获取摄像机ID:
NSString *cameraID = nil;
NSArray *captureDeviceType = @[AVCaptureDeviceTypeBuiltInWideAngleCamera];
AVCaptureDeviceDiscoverySession *captureDevice =
[AVCaptureDeviceDiscoverySession
discoverySessionWithDeviceTypes:captureDeviceType
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionUnspecified];
cameraID = [captureDevice.devices.lastObject localizedName];
对于我的视频捕捉程序我用下面的代码来获取麦克风,前后摄像头和我测试过的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
}
}
迅速4(xcode的10.1)
这是我在斯威夫特的最新版本什么工作。我没有看到这个答案,我花了一段时间来苏斯出来所以在这里是如何得到的前置摄像头。
if let device = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera , mediaType: AVMediaTypeVideo, position: .front) {
//Do the camera thing here..
}