Sitemap
DevTechie

DevTechie.com is all about sharing knowledge by practical means. We focus on solving real world challenges via code.

VideoPlayer in SwiftUI (iOS 14 +)

4 min readNov 2, 2022

--

Press enter or click to view image in full size

iOS 14 brought AVKit to the world of SwiftUI with the introduction of VideoPlayer view. VideoPlayer displays content of an AVPlayer instance. View is defined inside AVKit framework and lets us play videos natively in SwiftUI without the need of porting functionality over from UIKit.

VideoPlayer takes player as an initialization parameter, which is an instance of AVPlayer.

Let’s start with an example. We will begin with the import of AVKit framework.

import AVKit

Videos can be played from bundle or from a url. We will start by playing video from bundle.

We can download sample videos from pexels.com

https://www.pexels.com/videos/

Import video and make sure that we have copy item if needed, create folder reference and add to targets checked.

Ideally, you would want to import videos in a folder, maybe create a Resources folder.

Now, all we gotta do is create VideoPlayer and AVPlayer instance to play this video.

import AVKitstruct DevTechieVideoPlayerExample: View {
var body: some View {
VStack {
Text("DevTechie")
.font(.title3)
VideoPlayer(player: AVPlayer(url

--

--

DevTechie
DevTechie

Published in DevTechie

DevTechie.com is all about sharing knowledge by practical means. We focus on solving real world challenges via code.

Responses (2)