And*_*kas 40 avfoundation ios avcapturedevice swift
在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
}
}
}
Run Code Online (Sandbox Code Playgroud)
当iOS 10终于问世时,我在运行代码时收到了以下警告.请注意,我的录像机仍然可以正常工作约2周.
在iOS 10.0中不推荐使用'devices()':请改用AVCaptureDeviceDiscoverySession.
当我今天早上运行我的代码时,我的录像机停止了工作.xCode8没有给我任何错误,但摄像头捕获的previewLayer是完全白色的.当我开始录制时,我收到以下错误:
错误域= AVFoundationErrorDomain代码= -11800"操作无法完成"UserInfo = {NSLocalizedDescription =操作无法完成,NSUnderlyingError = 0x17554440 {错误域= NSOSStatusErrorDomain代码= -12780"(null)"},NSLocalizedFailureReason = An发生未知错误(-12780)}
我认为这与我使用弃用的方法有关AVCaptureDevice.devices().因此,我想知道如何使用AVCaptureDeviceDiscoverySession?
提前谢谢你的帮助!
小智 80
您可以使用以下内容获得前置摄像头:
AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .front)
Run Code Online (Sandbox Code Playgroud)
后置摄像头:
AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)
Run Code Online (Sandbox Code Playgroud)
麦克风:
AVCaptureDevice.default(.builtInMicrophone, for: AVMediaType.audio, position: .unspecified)
Run Code Online (Sandbox Code Playgroud)
Raf*_*sun 13
Swift 4,iOS 10+和Xcode 10.1取代
if let cameraID = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front)?.localizedName {
//cameraID = "Front Camera"
}
Run Code Online (Sandbox Code Playgroud)
使用AVCaptureDevice.DiscoverySession实现
if let cameraID = AVCaptureDevice.DiscoverySession.init(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: .video, position: .front).devices.first?.localizedName{
//cameraID = "Front Camera"
}
Run Code Online (Sandbox Code Playgroud)
需要用#available(iOS 10,*)检查来包装它.
hef*_*fgi 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
}
Run Code Online (Sandbox Code Playgroud)
如果需要,您还可以通过更改deviceTypes数组从iPhone 7+(双摄像头)获取新的devicesTypes.
这是一个很好的阅读:https://forums.developer.apple.com/thread/63347
它适用于Xcode 9.2和Swift 4
AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)
Run Code Online (Sandbox Code Playgroud)
https://developer.apple.com/documentation/avfoundation/avcapturedevice/2361508-default
| 归档时间: |
|
| 查看次数: |
31383 次 |
| 最近记录: |