Skip to content

Instantly share code, notes, and snippets.

@eospi
Last active June 25, 2020 20:52
Show Gist options
  • Save eospi/02875fa827c9b9b20c8982d3249fe90b to your computer and use it in GitHub Desktop.
Save eospi/02875fa827c9b9b20c8982d3249fe90b to your computer and use it in GitHub Desktop.
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
var videoPlayer: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
guard let path = Bundle.main.path(forResource: "myFileName", ofType: "mp4") else { return }
let videoURL = URL(fileURLWithPath: path)
let playerItem = AVPlayerItem(url: videoURL)
let videoPlayer = AVPlayer(playerItem: playerItem)
self.videoPlayer = videoPlayer
let videoMaterial = VideoMaterial(avPlayer: videoPlayer)
let videoPlane = ModelEntity(mesh: .generatePlane(width: 1.6, depth: 0.9), materials: [videoMaterial])
let anchor = AnchorEntity(plane: .vertical)
anchor.addChild(videoPlane)
arView.scene.anchors.append(anchor)
videoPlayer.play()
NotificationCenter.default.addObserver(self, selector: #selector(loopVideo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)
}
@objc func loopVideo(notification: Notification) {
guard let playerItem = notification.object as? AVPlayerItem else { return }
playerItem.seek(to: CMTime.zero, completionHandler: nil)
videoPlayer.play()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment