gpt4 book ai didi

swift - ARKit SKVideoNode在渲染上播放

转载 作者:IT王子 更新时间:2023-10-29 05:03:44 26 4
gpt4 key购买 nike

主要问题:

之后,我将添加此部分以澄清问题。 -我可以暂停我的视频(我不想让它循环播放)。当我的节点出现时,即使它处于暂停状态,我的节点也会播放我的视频。如果我的视频播放完毕,并且可以看到,它将重新开始。我想删除此行为。

在我的应用程序中,我使用SKVideoNode对象和AVPlayer(:URL)对象从3D空间内的SCNNode创建了SCNGeometry。我使用ARKit .ImageTracking来确定何时找到特定图像,然后从那里播放视频。一切都是美好而花花公子的,只是每次AVPlayer出现时,玩家决定自己玩。但是,只要附有ARImageAnchorSCNNode都可以看到。无论哪种方式,每次节点进入摄像机镜头时,AVPlayer都会播放。我用

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if(keyPath == "rate") {
print((object as! AVPlayer).rate)
}
}

打印出比率,它是1,但是,它是0。

我使用player.pause()或player.play()为我的所有函数打印了某种打印函数print("Play"),只要在上面的速率上进行更改,就不会调用它们。我如何找到改变播放器速率的原因?

我检查了原始的根节点 self.sceneview.scene.rootNode.childNodes,以确保没有创建额外的VideoNodes/SCNNodes/AVPlayers等,似乎只有1个。

关于为什么使用ARKit在SCNNode进入摄像机后为什么要播放SKVideoNode/AVPlayer的任何想法?提前致谢!

编辑1:

进行了变通,以仅确定用户何时单击此节点
let tap = UITapGestureRecognizer(target: self, action: #selector(self!.tapGesture))
tap.delegate = self!
tap.name = "MyTap"
self!.sceneView.addGestureRecognizer(tap)

然后在下一个函数中,我将
@objc func tapGesture(_ gesture:UITapGestureRecognizer) {
let tappedNodes = self.sceneView.hitTest(gesture.location(in: gesture.view), options: [SCNHitTestOption.searchMode: 1])

if !tappedNodes.isEmpty {
for nodes in tappedNodes {
if nodes.node == videoPlayer3D {
videoPlayer3D.tappedVideoPlayer = true
videoPlayer3D.playOrPause()
break
}
}
}
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if(keyPath == "rate") {
print((object as! AVPlayer).rate)
if(!self.tappedVideoPlayer) {
self.player.pause() //HERE
}
}
}

其中videoPlayer3D是包含SKVideoNode的 SCNNode

但是,在上面标记为“HERE”的部分上出现错误 com.apple.scenekit.scnview-renderer (17): EXC_BAD_ACCESS (code=2, address=0x16d8f7ad0)。似乎场景 View 的渲染器正在尝试在render函数中更改我的视频节点,尽管我什至不使用 renderer(updateAtTime:)函数,而仅使用
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

guard let imageAnchor = anchor as? ARImageAnchor else { return }

createVideoNode(imageAnchor)

}

以确定何时看到图片(即 imageTracking)并创建节点。有小费吗?

思想1

出现错误,表明正在从方法渲染器的SCNView对象调用某些方法(这是我从错误中了解的内容),但是我没有专门调用该节点。我认为可能正在调用该节点时会执行的默认操作,但是,我不确定如何访问它或确定哪种方法是100%的。我正在使用的对象不是SCNView对象,并且我不认为它们是从SCNView对象继承的(请看第一段以了解所使用的变量)。只是希望删除每次查看时正在播放的节点的“ Action ”。

添加:

为了跟随我的视频播放器的创建(如果感兴趣),就在这里。让我知道您还有其他想看的内容(不确定您可能想看的内容),并感谢您的帮助。
func createVideoNode(_ anchor:ARImageAnchor, initialPOV:SCNNode) -> My3DPlayer? {

guard let currentFrame = self.sceneView.session.currentFrame else {
return nil
}

let delegate = UIApplication.shared.delegate as! AppDelegate

var videoPlayer:My3DPlayer!
videoPlayer = delegate.testing ? My3DPlayer(data: nil, currentFrame: currentFrame, anchor: anchor) : My3DPlayer(data: self.urlData, currentFrame: currentFrame, anchor: anchor)

//Create TapGesture
let tap = UITapGestureRecognizer(target: self, action: #selector(self.tapGesture))
tap.delegate = self
tap.name = "MyTap"
self.sceneView.addGestureRecognizer(tap)

return videoPlayer
}

My3dPlayer类别:
class My3DPlayer: SCNNode {

init(geometry: SCNGeometry?) {
super.init()
self.geometry = geometry
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

convenience init(data:Data?, currentFrame:ARFrame, anchor:ARImageAnchor) {
self.init(geometry: nil)
self.createPlayer(currentFrame, data, anchor)
}

private func createPlayer(_ frame:ARFrame, _ data:Data?,_ anchor:ARImageAnchor) {

let physicalSize = anchor.referenceImage.physicalSize

print("Init Player W/ physicalSize: \(physicalSize)")

//Create video
if((UIApplication.shared.delegate! as! AppDelegate).testing) {
let path = Bundle.main.path(forResource: "Bear", ofType: "mov")
self.url = URL(fileURLWithPath: path!)
}
else {
let url = data!.getAVAssetURL(location: "MyLocation")
self.url = url
}
let asset = AVAsset(url: self.url)
let track = asset.tracks(withMediaType: AVMediaType.video).first!
let playerItem = AVPlayerItem(asset: asset)
let player = AVPlayer(playerItem: playerItem)
self.player = player
var videoSize = track.naturalSize.applying(track.preferredTransform)

videoSize = CGSize(width: abs(videoSize.width), height: abs(videoSize.height))
print("Init Video W/ size: \(videoSize)")

//Determine if landscape or portrait
self.landscape = videoSize.width > videoSize.height
print(self.landscape == true ? "Landscape" : "Portrait")

//Do something when video ended
NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying(note:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)

//Add observer to determine when Player is ready
player.addObserver(self, forKeyPath: "status", options: [], context: nil)

//Create video Node
let videoNode = SKVideoNode(avPlayer: player)

//Create 2d scene to put 2d player on - SKScene
videoNode.position = CGPoint(x: videoSize.width/2, y: videoSize.height/2)
videoNode.size = videoSize


//Portrait -- //Landscape doesn't need adjustments??
if(!self.landscape) {
let width = videoNode.size.width
videoNode.size.width = videoNode.size.height
videoNode.size.height = width
videoNode.position = CGPoint(x: videoNode.size.width/2, y: videoNode.size.height/2)
}

let scene = SKScene(size: videoNode.size)

//Add videoNode to scene
scene.addChild(videoNode)

//Create Button-look even though we don't use the button. Just creates the illusion to pressing play and pause
let image = UIImage(named: "PlayButton")!
let texture = SKTexture(image: image)
self.button = SKSpriteNode(texture: texture)
self.button.position = videoNode.position

//Makes the button look like a square
let minimumSize = [videoSize.width, videoSize.height].min()!
self.button.size = CGSize(width: minimumSize/4, height: minimumSize/4)
scene.addChild(button)

//Get ratio difference from physicalsize and video size
let widthRatio = Float(physicalSize.width)/Float(videoSize.width)
let heightRatio = Float(physicalSize.height)/Float(videoSize.height)

let finalRatio = [widthRatio, heightRatio].min()!

//Create a Plane (SCNPlane) to put the SKScene on
let plane = SCNPlane(width: scene.size.width, height: scene.size.height)
plane.firstMaterial?.diffuse.contents = scene
plane.firstMaterial?.isDoubleSided = true

//Set Self.geometry = plane
self.geometry = plane

//Size the node correctly
//Find the real scaling variable
let scale = CGFloat(finalRatio)
let appearanceAction = SCNAction.scale(to: scale, duration: 0.4)
appearanceAction.timingMode = .easeOut

//Set initial scale to 0 then use action to scale up
self.scale = SCNVector3Make(0, 0, 0)
self.runAction(appearanceAction)
}

@objc func playerDidFinishPlaying(note: Notification) {
self.player.seek(to: .zero, toleranceBefore: .zero, toleranceAfter: .zero)
self.player.seek(to: .zero, toleranceBefore: .zero, toleranceAfter: .zero)
self.setButtonAlpha(alpha: 1)
}
}

工作1:

我试图通过以下方式停止跟踪:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

guard let imageAnchor = anchor as? ARImageAnchor else { return }

createVideoNode(imageAnchor)
self.resetConfiguration(turnOnConfig: true, turnOnImageTracking: false)
}

func resetConfiguration(turnOnConfig: Bool = true, turnOnImageTracking:Bool = false) {
let configuration = ARWorldTrackingConfiguration()
if(turnOnImageTracking) {
guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else {
fatalError("Missing expected asset catalog resources.")
}
configuration.planeDetection = .horizontal
configuration.detectionImages = referenceImages
}
else {
configuration.planeDetection = []
}
if(turnOnConfig) {
sceneView.session.run(configuration, options: [.resetTracking])
}
}

上面,我尝试重置配置。由于视频仍在渲染中播放,因此这只会导致它重置看上去似乎是平面的平面。无论是暂停还是结束,它都会重设并重新开始播放,或者从中断处继续播放。

努力2:

我试过了
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

guard let imageAnchor = anchor as? ARImageAnchor else { return }

createVideoNode(imageAnchor)
self.pauseTracking()
}

func pauseTracking() {
self.sceneView.session.pause()
}

这将停止所有操作,因此由于没有追踪任何东西,因此相机甚至冻结。这里完全没有用。

最佳答案

好。所以这里是一个解决方法。参见renderer(_:updateAtTime:)

var player: AVPlayer!
var play = true

@objc func tap(_ recognizer: UITapGestureRecognizer){
if play{
play = false
player.pause()
}else{
play = true
player.play()
}
}

func setVideo() -> SKScene{
let size = CGSize(width: 500, height: 500)
let skScene = SKScene(size: size)

let videoURL = Bundle.main.url(forResource: "video.mp4", withExtension: nil)!
player = AVPlayer(url: videoURL)

skScene.scaleMode = .aspectFit

videoSpriteNode = SKVideoNode(avPlayer: player)
videoSpriteNode.position = CGPoint(x: size.width/2, y: size.height/2)
videoSpriteNode.size = size
videoSpriteNode.yScale = -1
skScene.addChild(videoSpriteNode)

player.play()

return skScene
}

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
if let image = anchor as? ARImageAnchor{
print("found")

let planeGeometry = SCNPlane(width: image.referenceImage.physicalSize.width, height: image.referenceImage.physicalSize.height)
let plane = SCNNode(geometry: planeGeometry)
planeGeometry.materials.first?.diffuse.contents = setVideo()

plane.transform = SCNMatrix4MakeRotation(-.pi/2, 1, 0, 0)

node.addChildNode(plane)
}
}

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
if !play{
player.pause()
}
}

在您的代码中使用这个想法。

关于swift - ARKit SKVideoNode在渲染上播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53256602/

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