Try again and it still doesn't work:
import SwiftUI
import RealityKit
import ARKit
import AVFoundation
struct ContentView : View {
var body: some View {
ARViewContainer().edgesIgnoringSafeArea(.all)
}
}
struct ARViewContainer: UIViewRepresentable {
let arView = ARView(frame: .zero)
func makeUIView(context: Context) -> ARView {
arView.session.delegate = context.coordinator
let worldConfig = ARWorldTrackingConfiguration()
worldConfig.planeDetection = .horizontal
worldConfig.providesAudioData = true
arView.session.run(worldConfig)
arView.tapGestureRecognizer()
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {}
func makeCoordinator() -> Coordinator {
Coordinator( )
}
class Coordinator: NSObject, ARSessionDelegate, ARSessionObserver {
func session(_ session: ARSession, didOutputAudioSampleBuffer audioSampleBuffer: CMSampleBuffer) {
}
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
}
}
}
func addTestEntity(arView: ARView) {
let mesh = MeshResource.generatePlane(width: 0.1, height: 0.13)
guard let url = Bundle.main.url(forResource: "videoplayback", withExtension: "mp4") else { return }
let player = AVPlayer(url: url)
let videoMaterial = VideoMaterial(avPlayer: player)
let model = ModelEntity(mesh: mesh, materials: [videoMaterial])
model.name = "NewModel"
model.transform.translation.y = 0.05
let anchor = AnchorEntity(.plane(.horizontal, classification: .any, minimumBounds: SIMD2<Float>(0.2, 0.2)))
anchor.children.append(model)
arView.scene.anchors.append(anchor)
}
extension ARView {
func tapGestureRecognizer() {
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_ :)))
tap.numberOfTapsRequired = 1
self.addGestureRecognizer(tap)
}
@objc func handleTap(_ gesture: UITapGestureRecognizer) {
addTestEntity(arView: self)
guard let entity = scene.findEntity(named: "NewModel") else {
return
}
if let modelEntity = entity as? ModelEntity {
modelEntity.model?.materials.forEach { value in
if let videoMaterial = value as? VideoMaterial {
videoMaterial.avPlayer?.play()
print("video___play")
}
}
}
}
}