Created
February 17, 2021 00:50
-
-
Save followthemoney1/2f589d0a3a8fb6a8113a29b6be4fa20a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ARVideoContainer.swift | |
// ARConcept | |
// | |
// Created by Dmitry Dyachenko on 2/1/21. | |
// | |
import SwiftUI | |
import AVKit | |
import SceneKit | |
import ARKit | |
import SpriteKit | |
struct ARVideoContainer: View { | |
@State private var arData:ARData | |
@State private var scene:SKScene = SKScene() | |
var hostingController: (UIHostingController<ARVideoContainer>)? = nil | |
var player:AVPlayer | |
var onTap:(AVPlayer,ARData)-> Void | |
func scene(naturalSize:CGSize)-> SKScene{ | |
let videoNode = SKVideoNode(avPlayer: player) | |
let aspectRation = naturalSize.height/naturalSize.width | |
let videoScene = SKScene(size:CGSize(width:naturalSize.width * aspectRation,height: naturalSize.height)) | |
videoScene.scaleMode = .aspectFill | |
// center our video to the size of our video scene | |
videoNode.position = CGPoint(x: videoScene.size.width / 2, y: videoScene.size.height / 2) | |
//videoNode.yScale = -1.0 | |
videoScene.addChild(videoNode) | |
return videoScene | |
} | |
init(arData:ARData, action:@escaping (AVPlayer,ARData)-> Void) { | |
print("arContainer create") | |
self._arData = State(initialValue: arData) | |
self.onTap = action | |
player = AVPlayer(url: arData.directVideoUrl!) | |
// player.isMuted = true | |
// let playerLayer = AVPlayerLayer(player: player) | |
// playerLayer.sublayers = nil | |
// playerLayer.shouldRasterize = true | |
// playerLayer.rasterizationScale = UIScreen.main.scale | |
// playerLayer.videoGravity = .resizeAspect | |
// self._naturalSize = State(initialValue:.zero) | |
} | |
var body: some View { | |
GeometryReader { geometry in | |
Group{ | |
ZStack(alignment: .bottomLeading, content: { | |
// VideoPlayer(player: player) | |
// PlayerContainerView(player: player, gravity: .aspectFill) | |
// MyAVPlayer(videoUrl: arData.directVideoUrl!, startTime: .zero) | |
SpriteView(scene: scene) | |
Button(action: { | |
onTap(player,arData) | |
}){ | |
VStack{ | |
Image(uiImage: UIImage(named: "unfill_detector_image")!).frame(width: geometry.size.height/10, height: geometry.size.height/10, alignment: .center) | |
.padding() | |
Text("Full Screen") | |
.foregroundColor(.white) | |
.padding() | |
} | |
} | |
.padding() | |
}) | |
}.onAppear(){ | |
//setVideoInfinitive() | |
calculateVideoSize() | |
}.onDisappear(){ | |
player.pause() | |
} | |
.hideNavigationBar() | |
.edgesIgnoringSafeArea(.all) | |
} | |
} | |
private func calculateVideoSize(){ | |
let workItem = DispatchWorkItem { | |
//MARK:calculate aspect ration | |
let videoAsset = AVURLAsset(url : arData.directVideoUrl!) | |
let videoAssetTrack = videoAsset.tracks(withMediaType: .video).first | |
if let size = videoAssetTrack?.naturalSize{ | |
DispatchQueue.main.async { | |
scene = scene(naturalSize: size) | |
} | |
} | |
} | |
DispatchQueue.global().async(execute: workItem) | |
} | |
private func setVideoInfinitive(){ | |
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: nil) { (notification) in | |
player.seek(to: CMTime.zero) | |
player.play() | |
} | |
} | |
func play(){ | |
// if(naturalSize == .zero){ | |
// print("natural size : \(naturalSize.height)") | |
// return | |
// } | |
if (player.status == .readyToPlay) { | |
print("arContainer play") | |
player.play() | |
} | |
} | |
func pause(){ | |
player.pause() | |
} | |
func empty(){ | |
DispatchQueue.main.async { | |
player.cancelPendingPrerolls() | |
player.replaceCurrentItem(with: nil) | |
scene = SKScene() | |
if let hostingController = hostingController{ | |
//MARK: is kind of bug when scene view didn't remove at all | |
let frame = hostingController.view.frame | |
UIApplication.shared.windows.forEach { | |
if $0.frame.width == frame.width && $0.frame.height == frame.height { | |
print($0) | |
$0.isHidden = true | |
} | |
} | |
hostingController.didMove(toParent: nil) | |
hostingController.rootView.onDisappear(perform: { | |
}) | |
hostingController.dismiss(animated: false, completion: nil) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment