Playing Videos in SwiftUI & AVKit

DevTechie
DevTechie
Published in
2 min readJan 23, 2022

--

Photo by Denise Jans on Unsplash

SwiftUI VideoPlayer view let’s us play video files located either locally within the project or file located at a remote location available via url.

VideoPlayer view is part of AVKit framework and importing the framework is all you need to get started with the video player view 😃

VideoPlayer view takes AVPlayer as parameter where you have an opportunity to set video url either from your app bundle via Bundle.main.url(forResource:withExtension:) or you can create remote URL object(which is what we will do).

Let’s create a view to play video from a url. We will use one of the most famous sample video clip called “Big Buck Bunny” .

import SwiftUI
import AVKit
struct VideoPlayerExample: View {
var body: some View {
VStack {
Text("DevTechie")
.font(.largeTitle)
Text("Presents")
.font(.caption)
Text("Video player demo with Big Buck Bunny")
VideoPlayer(player: AVPlayer(url: URL(string: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")!))
}
}
}

Here is how our view will look like:

--

--

DevTechie
DevTechie

Published in DevTechie

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