我一直在开发一个应用程序来检测图像,一旦检测到,就会在上面显示视频。我从类似的项目中编译了很多这段代码,这些项目不太符合我的要求。我已经到了检测图像的程度,根据检测到的参考图像调用视频,最后在图像的顶部播放视频。这一切都很完美,我对结果非常满意,但是,我还没有能够完成一个看似相当基本的任务。当我将应用程序加载到测试设备上时,应用程序按预期工作,除非我将相机移离参考图像,否则在丢失检测到的图像后,它会继续播放背景中的音频(以及视频)。如果有人可以帮助暂停并重新启动视频,那么只能停留在暂停状态,直到再次检测到为止。所有东西都是从我的视图控制器运行的(粘贴在下面)
import UIKit
import SceneKit
import ARKit
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSession.Category.playAndRecord, mode: .spokenAudio, options: .defaultToSpeaker)
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
} catch {
print("error.")
}
// Set the view's delegate
sceneView.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create a session configuration
let configuration = ARImageTrackingConfiguration()
// first see if there is a folder called "ARImages" Resource Group in our Assets Folder
if let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "ARImages", bundle: Bundle.main) {
// if there is, set the images to track
configuration.trackingImages = trackedImages
// at any point in time, only 1 image will be tracked
configuration.maximumNumberOfTrackedImages = 1
}
// Run the view's session
sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Pause the view's session
sceneView.session.pause()
}
// MARK: - ARSCNViewDelegate
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let imageAnchor = anchor as? ARImageAnchor else { return }
node.addChildNode(createdVideoPlayerNodeFor(imageAnchor.referenceImage))
let referenceImage = imageAnchor.referenceImage
}
func createdVideoPlayerNodeFor(_ target: ARReferenceImage) -> SCNNode {
let videoNode = SCNNode()
let videoPlayerGeometry = SCNPlane(width: target.physicalSize.width, height: target.physicalSize.height)
var player = AVPlayer()
if let targetName = target.name,
let fileUrlString = Bundle.main.url(forResource: targetName, withExtension: "mp4", subdirectory: "/art.scnassets") {
player = AVPlayer(url: fileUrlString)
player.play()
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: nil) { (notification) in
player.seek(to: CMTime.zero)
player.play()
}
}
videoPlayerGeometry.firstMaterial?.diffuse.contents = player
videoNode.geometry = videoPlayerGeometry
videoNode.eulerAngles.x = -Float.pi / 2
return videoNode
override var prefersHomeIndicatorAutoHidden: Bool {
return true
}
}
感谢任何人和所有能帮我解决这个问题的人!
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。