问题描述
我正在尝试在Swift操场上列出使用AVCaptureDevice.devices()
连接到我的机器的设备.
I am trying to list the the devices connected to my machine using AVCaptureDevice.devices()
in a Swift playground.
import Cocoa
import Foundation
import AVFoundation
import CoreMediaIO
var prop = CMIOObjectPropertyAddress(
mSelector: CMIOObjectPropertySelector(kCMIOHardwarePropertyAllowScreenCaptureDevices),
mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeGlobal),
mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementMaster))
var allow : UInt32 = 1
let dataSize : UInt32 = 4
let zero : UInt32 = 0
CMIOObjectSetPropertyData(CMIOObjectID(kCMIOObjectSystemObject), &prop, zero, nil, dataSize, &allow)
var session = AVCaptureSession()
session.sessionPreset = AVCaptureSession.Preset.low
let devices = AVCaptureDevice.devices()
for device in devices {
let deviceID = device.uniqueID
let deviceName = device.localizedName
print("\(deviceID): \(deviceName)")
}
即使我的iPhone已连接到计算机,这也会给我带来以下结果
This gives me the following result even though my iPhone is connected to my computer
04-52-c7-c1-65-c4:input: Bose Tito
AppleHDAEngineInput:1B,0,1,0:1: Built-in Microphone
CC26311ECFEG1HNBA: FaceTime HD Camera
现在我注意到,如果我启动Quicktime Player,请选择新建电影录制"并选择我的设备作为相机"源,然后列出我的设备
Now something I've noticed is that if I launch Quicktime Player, select New Movie Recording and selecting my device as a Camera source, then my device is listed
04-52-c7-c1-65-c4:input: Bose Tito
AppleHDAEngineInput:1B,0,1,0:1: Built-in Microphone
CC26311ECFEG1HNBA: FaceTime HD Camera
12345b7406eeb053e2d5cded2527315d6110a16e: tito
反正有防止这种情况发生吗?
Is there anyway to prevent this?
推荐答案
您需要稍等片刻,设备才会出现.
You need to wait a bit for the device to appear.
注册AVCaptureDeviceWasConnectedNotification
一旦可用,将得到通知.
Register for AVCaptureDeviceWasConnectedNotification
to be notified once it becomes available.
这篇关于除非打开Quicktime,否则AVCaptureDevice.devices()不会列出iOS设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!