ARKIT:当节点不在场景中时,SKVideoNode继续播放,暂停不起作用 [英] ARKIT : SKVideoNode continue playing while node is out of scene, pause is not working
问题描述
我正在使用SceneKit和ARKit来实现一个演示项目,在其中我将相机保持在某些图片上并播放来自url的视频.一切正常,但我无法停止上一个视频,并且开始另一个视频.以前的视频在背景上播放.有时当我切换图像时会发生这种情况.我的实现是:
I am implementing a demo project using SceneKit and ARKit where i keep camera on some picture and play video from url. Everything works fine but i could not be able to stop previous video and it starts another video. Previous video plays on background. It happens sometimes when i switch image. My implementation is :
@IBOutlet var sceneView: ARSCNView!
var videosNames = [String]()
var player = AVPlayer()
var videoNode = SKVideoNode()
在viewDidload中,
In 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!)")
}
}
在这里,当任何新视频开始播放时,我会收到日志,但是我无法停止先前启动的视频,该视频在新视频的背景下播放(仅音频).这完全弄乱了我的流程.如果我将相机远离两个图像,但声音仍在播放.有人可以帮我解决这个问题吗?
Here i get log when any new video start playing , but i could not stop previously started video which plays in background (only audio) with the new video. It totally messed up my process. If i put camera away from both image, but the sounds keeps playing. Could anyone please help me for the issue ?
先谢谢了.
推荐答案
必须从ARSCNView的会话中删除节点.以下方法不会从会话中删除该节点.
Node must be removed from ARSCNView's session. Following method does not removed the node from session.
node.removeFromParentNode()
只有sceneView.session.remove()在didUpdate()方法中可以完美地工作.
Only sceneView.session.remove() works perfectly in didUpdate() method.
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
}
}
}
这篇关于ARKIT:当节点不在场景中时,SKVideoNode继续播放,暂停不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!