我正在尝试制作一个在 AR 中播放视频的应用程序,视频应该漂浮在用户面前。这是我的代码:
导入UIKit 导入RealityKit 导入场景工具包 导入AVKit 导入ARKit
ViewController 类:UIViewController、ARSCNViewDelegate {
@IBOutlet var arView: ARView!
@IBOutlet var sceneView: ARSCNView!
var player : AVPlayer!
func addARTV() {
let fileURL = URL(fileURLWithPath: Bundle.main.path(forResource: "poolSession", ofType: "m4v")!)
player = AVPlayer(url: fileURL)
let tvGeo = SCNPlane(width: 1.6, height: 0.9)
tvGeo.firstMaterial?.diffuse.contents = player
tvGeo.firstMaterial?.isDoubleSided = true
let tvNode = SCNNode(geometry: tvGeo)
tvNode.position.z = -2
sceneView.scene.rootNode.addChildNode(tvNode)
player.play()
}
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let configuration = ARWorldTrackingConfiguration()
sceneView.session.run(configuration)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
addARTV()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView.session.pause()
}
}
我在使用变量 sceneView 时遇到问题。当我运行代码时,它显示“构建成功”。然而,几秒钟后,在应用程序完全加载之前,我收到(线程 1:致命错误:隐式展开可选值时意外发现 nil)错误弹出,sceneView 位于行 (sceneView.scene.rootNode.addChildNode( tvNode)) 带有红色下划线,并且应用程序构建暂停。我尝试从代码中完全删除 sceneView 。但是,视频未显示。有谁知道如何解决这个问题?