- April 27, 2024
- Mins Read
SwiftVideoBackground is an easy to use Swift framework that provides the ability to play a video on any UIView. This provides a beautiful UI for login screens, or splash pages, as implemented by Spotify and many others.
&&
videos from a web URLYou can use CocoaPods to install SwiftVideoBackground
by adding it to your Podfile
:
For Swift 5:
pod ‘SwiftVideoBackground’
For Swift 4:
pod ‘SwiftVideoBackground’, ‘~> 3.0’
For Swift 3:
pod ‘SwiftVideoBackground’, ‘0.06’
You can use Carthage to install SwiftVideoBackground
by adding it to your Cartfile
:
github “dingwilson/SwiftVideoBackground”
To use this library in your project manually you may:
alpha
renamed to darkness
See the quick migration guide.
import UIKit
import SwiftVideoBackground
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
try? VideoBackground.shared.play(view: view, videoName: “myVideo”, videoType: “mp4”)
/* or from URL */
let url = URL(string: “https://coolVids.com/coolVid.mp4”)!
VideoBackground.shared.play(view: view, url: url)
}
}
Documentation for Version 0.06 (Swift 3) can be found here.
play()
has four additional optional parameters for customization:
darkness
: CGFloat – Value between 0
and 1
. The higher the value, the darker the video. Defaults to 0
.isMuted
: Bool – Indicates whether video is muted. Defaults to true
.willLoopVideo
: Bool – Indicates whether video should restart when finished. Defaults to true
.setAudioSessionAmbient
: Bool – Indicates whether to set the shared AVAudioSession
to ambient. If this is not done, audio played from your app will pause other audio playing on the device. Defaults to true
.So for example:
VideoBackground.shared.play(
view: view,
videoName: “myVideo”,
videoType: “mp4”,
darkness: 0.25,
isMuted: false,
willLoopVideo: true,
setAudioSessionAmbient: true
)
> will play the video with the sound on, slightly darkened, continuously looping, and without affecting other sources of audio on the device.
Any combination of the parameters can be included or left out.
setAudioSessionAmbient
only has an effect in iOS 10.0+. For more information, see the docs.
pause()
– Pauses the video.resume()
– Resumes the video.restart()
– Restarts the video.getThumbnailImage(from: URL, at: CMTime)
– Generate an image from the video to show as thumbnail.darkness
– Change this CGFloat
to adjust the darkness of the video. Value 0
to 1
. Higher numbers are darker. Setting to an invalid value does nothing.isMuted
– Change this Bool
to mute/unmute the video.willLoopVideo
– Change this Bool
to set whether the video restarts when it ends.videoGravity
– Default is .resizeAspectFill
. Change to .resizeAspect
(doesn’t fill view) or .resize
(doesn’t conserve aspect ratio).playerLayer
– The AVPlayerLayer
that can be accessed for advanced control and customization of the video.SwiftVideoBackground
includes a singleton instance that can be conveniently accessed with VideoBackground.shared
. An instance of VideoBackground
can only play one video on one UIView
at a time. So if you need to play on multiple UIView
s, you need to retain an instance of VideoBackground
for each UIView
:
let videoBackground1 = VideoBackground()
In order to play local videos, you must add them to your project:
Build Phases
Copy Bundle Resources
+
to add a videoHorizon SDK is a state of the art real-time video recording / photo shooting iOS library. Some of the features ...