realitykit_RealityKit中的视频资料

realitykit

Among the RealityKit updates from WWDC 2020 are video materials. Like other RealityKit materials, VideoMaterial can be applied to any mesh, accepting an AVPlayer to handle playback. Here is a guide on how to add video to any RealityKit AR experience.

WWDC 2020中的RealityKit更新包括视频材料。 像其他RealityKit材质一样, VideoMaterial可以应用于任何网格,并接受AVPlayer处理播放。 这是有关如何将视频添加到任何RealityKit AR体验的指南。

将视频添加到您的项目 (Add a video to your project)

  1. Add a video file to your iOS target’s main folder

    将视频文件添加到iOS目标的主文件夹中
  2. Go to the iOS target’s settings, select Build Phases, and add your video under Copy Bundle Resources.

    转到iOS目标的设置,选择“ 构建阶段” ,然后在“ 复制捆绑资源”下添加您的视频。

Image for post

将视频加载到AVPlayer中 (Load the video in an AVPlayer)

RealityKit uses an AVPlayer for video materials, so make sure to add import AVKit at the top of your ViewController.swift file.

RealityKit使用AVPlayer播放视频材料,因此请确保在ViewController.swift文件顶部添加import AVKit

To get the path of the video file, change the forResource and ofType parameters in the code to match your file. For example, if your file is myFileName.mp4 , the resource is myFileName , and the type is mp4 . With the path, we create a URL and then an AVPlayerItem to manage timing. Finally, create an AVPlayer .

要获取视频文件的路径,请在代码中更改forResourceofType参数以匹配您的文件。 例如,如果您的文件是myFileName.mp4 ,则资源是myFileName ,类型是mp4 。 使用该路径,我们创建一个URL,然后创建一个AVPlayerItem来管理时间。 最后,创建一个AVPlayer

let videoURL = URL(fileURLWithPath: path)let playerItem = AVPlayerItem(url: videoURL)let videoPlayer = AVPlayer(playerItem: playerItem)

Afterwards, creating a VideoMaterial from the AVPlayer is very simple.

然后,从AVPlayer创建VideoMaterial非常简单。

let videoMaterial = VideoMaterial(avPlayer: videoPlayer)

let videoMaterial = VideoMaterial(avPlayer: videoPlayer)

创建材质,网格和锚点。 (Create the material, mesh, and anchor.)

Your video needs a mesh to attach to, and RealityKit provides a number of primitive shapes to choose from. This example uses a plane, but you can create some interesting effects with other shapes. I attached the plane mesh to a vertical plane anchor so that it looks like a TV on the wall. Afterwards, don’t forget to add the anchor to the scene!

您的视频需要附加一个网格,RealityKit提供了许多原始形状可供选择。 本示例使用飞机,但是您可以使用其他形状创建一些有趣的效果。 我将平面网格附加到垂直平面锚上,使其看起来像墙上的电视。 之后,别忘了将锚添加到场景!

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)

播放影片 (Play the video)

Video playback is managed by the AVPlayer rather than the VideoMaterial. To start the video, simply call the play method on the player.

视频播放是由AVPlayer而不是VideoMaterial管理的。 要开始播放视频,只需在play器上调用play方法。

videoPlayer.play()

循环播放 (Looping playback)

This code will only play the video once, but in many cases you will want to video to keep playing. To accomplish this, you can listen to notifications when the AVPlayerItem has reached the end of the video.

此代码只会播放一次视频,但是在许多情况下,您将希望继续播放视频。 为此,您可以在AVPlayerItem到达视频结尾时收听通知。

First, you’ll need to store the AVPlayer as a property on the view controller to access it from outside the method you created it in. After creating a videoPlayer property, set it to the video player you created earlier.

首先,您需要将AVPlayer作为属性存储在视图控制器上,以从创建它的方法外部访问它。创建videoPlayer属性后,将其设置为您先前创建的视频播放器。

class ViewController: UIViewController {
var videoPlayer: AVPlayer! // Add this line

override func viewDidLoad() {
let videoPlayer = AVPlayer(playerItem: playerItem)
self.videoPlayer = videoPlayer // Add this line
}
}

Notification Center allows you to listen to an event, like an AVPlayerItem finishing a video. After creating the video player, add a notification observer. The selector refers to a method that we will create next to handle the looping.

通知中心允许您收听事件,例如AVPlayerItem完成视频。 创建视频播放器后,添加一个通知查看器。 选择器是指我们将在下一步创建的用于处理循环的方法。

NotificationCenter.default.addObserver(
self,
selector: #selector(loopVideo),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: playerItem
)

The loopVideo method simply takes the AVPlayerItem from the notification, moves the video back the the start, and hits play again.

loopVideo方法仅从通知中获取AVPlayerItem ,将视频移回起点,然后再次点击播放。

@objc func loopVideo(notification: Notification) {
guard let playerItem = notification.object as? AVPlayerItem else { return } playerItem.seek(to: CMTime.zero, completionHandler: nil)
videoPlayer.play()
}

完整代码示例 (Full code example)

For more RealityKit or WWDC 2020 coverage, see my articles below. Thank you!

有关RealityKit或WWDC 2020的更多信息,请参阅下面的文章。 谢谢!

翻译自: https://medium.com/@ethansaadia/video-materials-in-realitykit-35038d3758d3

realitykit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值