VideoKit is a SwiftUI library with a configurable VideoPlayer
view, and other utilities like a video splash screen.
Unlike AVKit's VideoPlayer, VideoKit's VideoPlayer
can be configured to great extent. It also lets you observe the current player time and can trigger a custom action when the player reaches the end.
VideoKit can also add customizable, video-based splash screens to your app. This makes it easy to add powerful launch effects, like we see in many video streaming apps.
VideoKit can be installed with the Swift Package Manager:
https://github.com/danielsaidi/VideoKit.git
Important
For remote video playback to work in macOS Catalyst, you must enable "Outgoing Connections (Client)" under "Signing & Capabilities > App Sandbox" and enable "App Transport Security Settings > Allow Arbitrary Loads" (for more security, specify allowed domains) under the app's "Info" configuration.
VideoKit supports iOS 15, iPadOS 15, macOS Catalyst 15, tvOS 15, and visionOS 1.
You can become a sponsor to help me dedicate more time on my various open-source tools. Every contribution, no matter the size, makes a real difference in keeping these tools free and actively developed.
To add video content to your app, just add a VideoPlayer
with a URL to the content you want to play:
struct ContentView: View {
var body: some View {
VideoPlayer(videoURL: VideoPlayer.sampleVideoURL)
.aspectRatio(16/9, contentMode: .fit)
}
}
You can injecting a time
binding, and trigger an action when the video stops playing:
struct ContentView: View {
@State var isVideoPresented = false
@State var videoTime = TimeInterval.zero
var body: some View {
Button("Play video") {
isVideoPresented = true
}
.fullScreenCover(isPresented: $isVideoPresented) {
VideoPlayer(
videoURL: VideoPlayer.sampleVideoURL,
time: $videoTime,
didPlayToEndAction: { isVideoPresented = false }
)
.ignoresSafeArea()
}
}
}
You can inject a VideoPlayerConfiguration
and a controller configuration to customize the player and its underlying controller:
struct ContentView: View {
var body: some View {
VideoPlayer(
videoURL: VideoPlayer.sampleVideoURL,
configuration: .init(autoPlay: false),
controllerConfiguration: { controller in
controller.showsPlaybackControls = false
}
)
}
}
These options make it easy to add powerful video-based features to your app.
VideoKit makes it easy to add a video-based splash screen to your app, that is automatically presented on launch and dismissed when the embedded video stops playing.
To add a video splash screen to your app, just add a .videoSplashScreen(videoURL:configuration:)
view modifier to your app's root view:
struct ContentView: View {
var body: some View {
Text("Hello, world")
.videoSplashScreen(
videoURL: VideoPlayer.sampleVideoURL
)
}
}
You can pass in a VideoSplashScreenConfiguration
to customize the splash screen:
struct ContentView: View {
var body: some View {
Text("Hello, world")
.videoSplashScreen(
videoURL: VideoPlayer.sampleVideoURL,
configuration: .init(
dismissAnimation: .linear(duration: 2),
maxDisplayDuration: 2
)
)
}
}
You can also customize the video player view, for instance to add a custom background view to it:
struct ContentView: View {
var body: some View {
Text("Hello, world")
.videoSplashScreen(
videoURL: VideoPlayer.sampleVideoURL,
videoPlayerView: { videoPlayer in
Color.red
videoPlayer.aspectRatio(contentMode: .fit)
}
)
}
}
These options make it easy to add customizable video splash screens to your app.
The library comes with a SampleVideo
type that can be used to test the player, and a .librarySampleVideos
collection that is parsed from a JSON file that is embedded within the library.
The online documentation has more information, articles, code examples, etc.
The Demo
folder has a demo app that lets you explore the library.
Feel free to reach out if you have questions, or want to contribute in any way:
- Website: danielsaidi.com
- E-mail: daniel.saidi@gmail.com
- Bluesky: @danielsaidi@bsky.social
- Mastodon: @danielsaidi@mastodon.social
VideoKit is available under the MIT license. See the LICENSE file for more info.