gpt4 book ai didi

ios - ARKIT : SKVideoNode continue playing while node is out of scene, 暂停不起作用

转载 作者:行者123 更新时间:2023-11-28 13:29:57 24 4
gpt4 key购买 nike

我正在使用 SceneKit 和 ARKit 实现一个演示项目,我将相机放在一些图片上并从 url 播放视频。一切正常,但我无法停止之前的视频并开始播放另一个视频。以前的视频在后台播放。当我切换图像时有时会发生这种情况。我的实现是:

@IBOutlet var sceneView: ARSCNView!
var videosNames = [String]()
var player = AVPlayer()
var videoNode = SKVideoNode()

在viewDidload中,

sceneView.delegate = self

// Create a session configuration
let configuration = ARImageTrackingConfiguration()

// first see if there is a folder called "ARImages" Resource Group in our Assets Folder
if let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "ARImages", bundle: Bundle.main) {

// if there is, set the images to track
configuration.trackingImages = trackedImages
// at any point in time, only 1 image will be tracked
configuration.maximumNumberOfTrackedImages = 1
}

// Run the view's session
sceneView.session.run(configuration)

回调方法如下:

     func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

// if the anchor is not of type ARImageAnchor (which means image is not detected), just return
if let _ : Bool = videosNames.contains(anchor.name!) {

guard let imageAnchor = anchor as? ARImageAnchor, var fileUrlString = Bundle.main.path(forResource: "baby1", ofType: "mp4") else {return}

print("didAdd node:\(anchor.name!)")
//find our video file
fileUrlString = "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" // i set the file URL path directly here


let videoItem = AVPlayerItem(url: URL(fileURLWithPath: fileUrlString))

self.player.seek(to: CMTime.zero)
self.player.pause() // Tried to pause the video, set seek time zero. Nothing works.
self.videoNode.pause()

player = AVPlayer(playerItem: videoItem)
//initialize video node with avplayer
videoNode = SKVideoNode(avPlayer: self.player)


//videoNode.removeAllChildren()
self.player.play()
//self.videoNode.play()
// add observer when our player.currentItem finishes player, then start playing from the beginning
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: nil) { (notification) in
self.player.seek(to: CMTime.zero)
self.player.play()
//self.videoNode.play()
print("Looping Video")
}


// set the size (just a rough one will do)
let videoScene = SKScene(size: CGSize(width: 480, height: 360))
// center our video to the size of our video scene
self.videoNode.position = CGPoint(x: videoScene.size.width / 2, y: videoScene.size.height / 2)
// invert our video so it does not look upside down
self.videoNode.yScale = -1.0
// add the video to our scene
videoScene.addChild(self.videoNode)
// create a plan that has the same real world height and width as our detected image
let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
// set the first materials content to be our video scene
plane.firstMaterial?.diffuse.contents = videoScene
// create a node out of the plane
let planeNode = SCNNode(geometry: plane)
// since the created node will be vertical, rotate it along the x axis to have it be horizontal or parallel to our detected image
planeNode.eulerAngles.x = -Float.pi / 2
// finally add the plane node (which contains the video node) to the added node
node.addChildNode(planeNode)

}
}

当任何节点获得更新时

   func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {

if node.isHidden == true {
print("Node is out of view:\(anchor.name!) :")
self.player.seek(to: CMTime.zero)
videoNode.pause()

} else {
print("Node is not out of view:\(anchor.name!)")
}
}

在这里,当任何新视频开始播放时,我都会收到日志,但我无法停止之前开始播放的视频,该视频在后台(仅音频)与新视频一起播放。它完全搞砸了我的过程。如果我将相机远离两个图像,但声音会继续播放。谁能帮我解决这个问题?

提前致谢。

最佳答案

必须从 ARSCNView 的 session 中删除节点。以下方法不会从 session 中删除节点。

node.removeFromParentNode() 

只有 sceneView.session.remove() 在 didUpdate() 方法中完美运行。

func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {

if node.isHidden == true {
if let imageAnchor = anchor as? ARImageAnchor {
sceneView.session.remove(anchor: imageAnchor)
}
} else {
if !isNodeAdded {
isNodeAdded = true
}
}
}

关于ios - ARKIT : SKVideoNode continue playing while node is out of scene, 暂停不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57543919/

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