swiftui视频
SwiftUI in iOS 14 introduced native support for playing movie files. The new VideoPlayer
control lets you play movie files from URLs or local resources.
iOS 14中的SwiftUI引入了对播放电影文件的本机支持。 新的VideoPlayer
控件使您可以从URL或本地资源播放电影文件。
All you need to do is import AVKit
and pass the instance of AVPlayer
as shown below:
您需要做的就是import AVKit
并传递AVPlayer
实例,如下所示:
VideoPlayer(player: AVPlayer(url: enter_url_here)
Video Player works great on devices but can cause problems with simulators, especially when loading from a web URL.
Video Player在设备上运行良好,但会导致模拟器出现问题,尤其是从Web URL加载时。
You can also add custom overlay SwiftUI views over the video player. At the time of writing, it isn’t possible to get rid of default playback controls.
您还可以在视频播放器上添加自定义叠加SwiftUI视图。 在撰写本文时,不可能摆脱默认的播放控件。
It’s worth noting that YouTube URLs won’t work in AVPlayer
as they are exactly movie files.
值得注意的是,YouTube网址完全是电影文件,因此无法在AVPlayer
中使用。
Video Player opens up a door for doing video processing in pure SwiftUI applications. This means you can write a smart AI-based video player in less than 50 lines of code.
Video Player为纯SwiftUI应用程序中的视频处理打开了一扇门。 这意味着您可以用少于50行的代码编写一个基于AI的智能视频播放器。
In the next sections, we’ll see how to apply filters to Video Player and will implement a bunch of Core Image filters to run on movie files in real-time.
在接下来的部分中,我们将看到如何将过滤器应用于Video Player,并将实现一堆Core Image过滤器以在电影文件上实时运行。
如何在SwiftUI视频播放器中访问视频帧 (How to Access Video Frames in SwiftUI Video Player)
The most common way of accessing video frames from the AV player is by using AVPlayerItemVideoOutput
. To do so, we’d typically use the CADisplayLink
timer to get hold of frames after certain intervals.
从AV播放器访问视频帧的最常见方法是使用AVPlayerItemVideoOutput
。 为此,我们通常使用CADisplayLink
计时器在特定间隔后获取帧。
But CADisplayLink
doesn’t support native listeners in SwiftUI. And selectors for listening to changes don’t work without bringing UIKit into the scene.
但是CADisplayLink
在CADisplayLink
中不支持本机侦听器。 如果不将UIKit引入场景,则用于侦听更改的选择器将不起作用。
So, we’d fall onto the other option, which is AVVideoComposition
.
因此,我们将使用另一个选项AVVideoComposition
。
AVVideoComposition
contains the pixel buffer of every frame. By setting it on the AVPlayerItem
property, we can retrieve frames and apply CIFilter
to give the video a totally different look.
AVVideoComposition
包含每个帧的像素缓冲区。 通过在AVPlayerItem
属性上进行设置,我们可以检索帧并应用CIFilter
来赋予视频完全不同的外观。
将CIFilter应用于SwiftUI中的Video Stream (Apply CIFilter to Video Stream in SwiftUI)
Let’s create a new SwiftUI project and add the following contents to it:
让我们创建一个新的SwiftUI项目并向其中添加以下内容:
There are two main things happening in the above code:
上面的代码中发生了两件事:
The
AVPlayerItem
containsvideoComposition
property over which we’re setting the instance ofAVVideoComposition
. InsideAVVideoComposition
we pass themedia
property, which is of the typeAVAsset
.AVPlayerItem
包含videoComposition
属性,通过它我们可以设置AVVideoComposition
的实例。 在AVVideoComposition
内部,我们传递了media
属性,该属性的类型为AVAsset
。applyingCIFiltersWithHandler
is where the image processing of every frame takes place. We’re setting passing thesourceImage
into the currently selectedCIFilter
(which is picked from theSegmentedControl
).applyingCIFiltersWithHandler
是每帧图像处理的地方。 我们正在设置将sourceImage
传递到当前选择的CIFilter
(从SegmentedControl
)。
We ran the above application on a local video and got the following results with the four CIFilters
:
我们在本地视频上运行了上述应用程序,并通过四个CIFilters
获得了以下结果:
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/514a387f14430975cf7add6b5eccf390.png)
You can add more filters and set slider control for using different intensities. Adding a remote video URL works almost as smoothly as using a local one (if the network connection is good).
您可以添加更多滤镜并设置滑块控件以使用不同的强度。 添加远程视频URL的效果几乎与使用本地视频URL一样流畅(如果网络连接良好)。
结论 (Conclusion)
The full source code of this SwiftUI application is available in the GitHub repository.
该SwiftUI应用程序的完整源代码可在GitHub存储库中找到 。
SwiftUI Video Player has opened the door for many amazing effects, such as applying a Style transfer.
SwiftUI Video Player为许多惊人的效果打开了大门,例如应用了样式转换。
That’s it for this one. Thanks for reading.
这就是它了。 谢谢阅读。
swiftui视频