gpt4 book ai didi

swift - 如何在 macOS 上将 iPhone 设为 AVCaptureDevice?

转载 作者:行者123 更新时间:2023-12-02 03:04:21 25 4
gpt4 key购买 nike

我已经实现了下面链接的帖子中的代码。但是,由于 AVCaptureDevice.devices() 现已弃用,因此它并未更新。

iOS Device not listed by AVCaptureDevice.devices() unless Quicktime is opened

有人有最新的实现吗?

谢谢!

最佳答案

好吧,在结合了其他一些 Stack Overflow 答案后,我能够让它与 AVCaptureDevice.DiscoverySession 的最新实现一起使用。

诀窍是在启用屏幕捕获设备后调用 DiscoverSession 来获取设备。然后添加观察者来查找新连接的设备,它将显示。

前面的答案都使用了旧的 AVCaptureDevice.devices() 方法。我下面的示例使用更新的方法。

import Cocoa
import Foundation
import AVKit
import CoreMediaIO

class PlayerNSView: NSView{
private let playerLayer = AVPlayerLayer()
private let nc = NotificationCenter.default

override init(frame:CGRect){
super.init(frame: frame)


let urlVideo = URL(string: "https://www.radiantmediaplayer.com/media/bbb-360p.mp4")!
let player = AVPlayer(url: urlVideo)
//player.play()
playerLayer.player = player
if layer == nil{
layer = CALayer()
}
layer?.addSublayer(playerLayer)

self.enableDalDevices()

let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.externalUnknown], mediaType: nil, position: .unspecified)



nc.addObserver(self, selector: #selector(newDevice), name: NSNotification.Name.AVCaptureDeviceWasConnected, object: nil)

}

@objc func newDevice() {
let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.externalUnknown], mediaType: nil, position: .unspecified)
print("Devices: \(discoverySession.devices)")
}



public func enableDalDevices() {


var property = CMIOObjectPropertyAddress(mSelector: CMIOObjectPropertySelector(kCMIOHardwarePropertyAllowScreenCaptureDevices), mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeGlobal), mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementMaster))
var allow : UInt32 = 1
let sizeOfAllow = MemoryLayout.size(ofValue: allow)
CMIOObjectSetPropertyData(CMIOObjectID(kCMIOObjectSystemObject), &property, 0, nil, UInt32(sizeOfAllow), &allow)

}


required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layout() {
super.layout()

playerLayer.backgroundColor = .black
playerLayer.frame = bounds
}
}

关于swift - 如何在 macOS 上将 iPhone 设为 AVCaptureDevice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59350500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com