Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AVCaptureDevice.devices()未列出iOS设备,除非打开Quicktime_Ios_Swift_Avfoundation_Quicktime - Fatal编程技术网

AVCaptureDevice.devices()未列出iOS设备,除非打开Quicktime

AVCaptureDevice.devices()未列出iOS设备,除非打开Quicktime,ios,swift,avfoundation,quicktime,Ios,Swift,Avfoundation,Quicktime,我正在尝试在Swift游乐场中使用AVCaptureDevice.devices()列出连接到我的机器的设备 import Cocoa import Foundation import AVFoundation import CoreMediaIO var prop = CMIOObjectPropertyAddress( mSelector: CMIOObjectPropertySelector(kCMIOHardwarePropertyAllowScreenCaptureDevic

我正在尝试在Swift游乐场中使用
AVCaptureDevice.devices()
列出连接到我的机器的设备

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已连接到我的计算机,这也会给我以下结果

04-52-c7-c1-65-c4:input: Bose Tito
AppleHDAEngineInput:1B,0,1,0:1: Built-in Microphone
CC26311ECFEG1HNBA: FaceTime HD Camera
现在我注意到,如果我启动Quicktime Player,选择新建电影录制并选择我的设备作为相机源,那么我的设备将被列出

04-52-c7-c1-65-c4:input: Bose Tito
AppleHDAEngineInput:1B,0,1,0:1: Built-in Microphone
CC26311ECFEG1HNBA: FaceTime HD Camera
12345b7406eeb053e2d5cded2527315d6110a16e: tito

是否有任何方法可以防止这种情况发生?

您需要等待设备出现


注册AVCaptureDeviceWasConnectedNotification,以便在设备可用时收到通知。

您需要等待一段时间,设备才会出现


注册AVCaptureDeviceWasConnectedNotification,以便在其可用时收到通知。

您是如何执行检查以确保iOS设备已准备就绪可供使用的? 我也有同样的问题,我正在做一个项目

目前,如果我打开quicktime,它可以工作,(我还硬编码了设备ID,但需要去掉它)


您是如何检查iOS设备是否已准备好使用的? 我也有同样的问题,我正在做一个项目

目前,如果我打开quicktime,它可以工作,(我还硬编码了设备ID,但需要去掉它)


使用@Valerian的答案来帮助回答@adamprocter,下面是我最终如何解决它的

override func viewDidLoad() {

    super.viewDidLoad()
    enableDalDevices()

    camera.layer = CALayer()
    let session:AVCaptureSession = AVCaptureSession()
    session.sessionPreset = AVCaptureSession.Preset.high

    let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.externalUnknown], mediaType: .muxed, position: .unspecified)
    //You get the devices here and their IDs and do whatever you want
    showDevices(devices: discoverySession.devices)

    //Register for a notification just like @Valerian said earlier
    NotificationCenter.default.addObserver(self, selector: #selector(newDevice), name: NSNotification.Name.AVCaptureDeviceWasConnected, object: nil)

}

@objc func newDevice() {
    let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.externalUnknown], mediaType: .muxed, position: .unspecified)
    showDevices(devices: discoverySession.devices)
}

使用@Valerian的答案来帮助回答@adamprocter,下面是我最终如何解决它的

override func viewDidLoad() {

    super.viewDidLoad()
    enableDalDevices()

    camera.layer = CALayer()
    let session:AVCaptureSession = AVCaptureSession()
    session.sessionPreset = AVCaptureSession.Preset.high

    let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.externalUnknown], mediaType: .muxed, position: .unspecified)
    //You get the devices here and their IDs and do whatever you want
    showDevices(devices: discoverySession.devices)

    //Register for a notification just like @Valerian said earlier
    NotificationCenter.default.addObserver(self, selector: #selector(newDevice), name: NSNotification.Name.AVCaptureDeviceWasConnected, object: nil)

}

@objc func newDevice() {
    let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.externalUnknown], mediaType: .muxed, position: .unspecified)
    showDevices(devices: discoverySession.devices)
}

我认为该设备没有立即出现,您是否尝试注册了
AVCaptureDeviceWasConnectedNotification
?您是对的。如果我在几秒钟后再次打印设备列表,它就会工作。您可以将其添加为答案,我将接受。我认为该设备不会立即出现,您是否尝试注册
AVCaptureDeviceWasConnectedNotification
?您是对的。如果我在几秒钟后再次打印设备列表,它就会工作。您可以将其添加为答案,我将接受。请参阅我的答案。我想我已经给了你一些你可以用的东西,看看我的答案。我想我已经给了你一些你可以使用的东西