I am creating a plane with VideoMaterial when AR Reference Image is detected. I play the video inside an AVPlayer and I want to load the video from URL, instead of local resources. It all works well, however when the image is detected and the place is created, the app freezes for couple of seconds while the video gets loaded.
I don't know how to get rid of the freezing, instead I would like to show SimpleMaterial plane until the video is loaded.
Can anybody help me with this, please?
Code that I use to play the video when reference image is detected:
class Coordinator: NSObject, ARSessionDelegate{
var parent: ARViewContainer
var videoPlayer: AVPlayer!
init(parent: ARViewContainer) {
self.parent = parent
}
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
guard let imageAnchor = anchors[0] as? ARImageAnchor else { return }
let playerItem = AVPlayerItem(url: URL(string: "https://swiftanytime-content.s3.ap-south-1.amazonaws.com/SwiftUI-Beginner/Video-Player/iMacAdvertisement.mp4")!)
videoPlayer = AVPlayer(playerItem: playerItem)
let videoMaterial = VideoMaterial(avPlayer: videoPlayer)
let videoPlane = ModelEntity(mesh: .generatePlane(width: Float(imageAnchor.referenceImage.physicalSize.width), depth: Float(imageAnchor.referenceImage.physicalSize.height), cornerRadius: 10), materials: [videoMaterial])
if let imageName = imageAnchor.name, imageName == "referenceimg1" {
let anchor = AnchorEntity(anchor: imageAnchor)
anchor.addChild(videoPlane)
parent.arView.scene.addAnchor(anchor)
videoPlayer.play()
}
}