VideoBackground
public class VideoBackground
Class that plays and manages control of a video on a UIView.
-
Singleton that can play one video on one
UIViewat a time.Declaration
Swift
public static let shared: VideoBackground -
Change this
CGFloatto adjust the darkness of the video. Value0to1. Higher numbers are darker. Setting to an invalid value does nothing.Declaration
Swift
public var darkness: CGFloat { get set } -
Change this
Boolto mute/unmute the video.Declaration
Swift
public var isMuted: Bool { get set } -
Change this
Boolto set whether the video restarts when it ends.Declaration
Swift
public var willLoopVideo: Bool -
Default is
.resizeAspectFill. Change to.resizeAspect(doesn’t fill view) or.resize(doesn’t conserve aspect ratio)Declaration
Swift
public var videoGravity: AVLayerVideoGravity -
The
AVPlayerLayerthat can be accessed for advanced customization.Declaration
Swift
public lazy var playerLayer: AVPlayerLayer { get set } -
You only need to initialize your own instance of
VideoBackgroundif you are playing multiple videos on multipleUIViews. Otherwise just use thesharedsingleton.Declaration
Swift
public init() -
play(view:videoName:videoType:isMuted:darkness:willLoopVideo:setAudioSessionAmbient:preventsDisplaySleepDuringVideoPlayback:)Plays a local video.
Throws
VideoBackgroundError.videoNotFoundif the video cannot be found.- preventsDisplaySleepDuringVideoPlayback: If automatic lock is being used on the device setting this property to false will not hold the device awake. default value for iOS is true. Only has an effect in iOS 12.0+
Declaration
Swift
public func play(view: UIView, videoName: String, videoType: String, isMuted: Bool = true, darkness: CGFloat = 0, willLoopVideo: Bool = true, setAudioSessionAmbient: Bool = true, preventsDisplaySleepDuringVideoPlayback: Bool = true) throwsParameters
viewUIView that the video will be played on.
videoNameString name of video that you have added to your project.
videoTypeString type of the video. e.g. “mp4”
darknessCGFloat between 0 and 1. The higher the value, the darker the video. Defaults to 0.
isMutedBool indicating whether video is muted. Defaults to true.
willLoopVideoBool indicating whether video should restart when finished. Defaults to true.
setAudioSessionAmbientBool indicating whether to set the shared
AVAudioSessionto ambient. If this is not done, audio played from your app will pause other audio playing on the device. Defaults to true. Only has an effect in iOS 10.0+. -
play(view:url:darkness:isMuted:willLoopVideo:setAudioSessionAmbient:preventsDisplaySleepDuringVideoPlayback:)Plays a video from a local or remote URL.
Declaration
Swift
public func play(view: UIView, url: URL, darkness: CGFloat = 0, isMuted: Bool = true, willLoopVideo: Bool = true, setAudioSessionAmbient: Bool = true, preventsDisplaySleepDuringVideoPlayback: Bool = true)Parameters
viewUIView that the video will be played on.
urlURL of the video. Can be from your local file system or the web. Invalid URLs will not be played but do not return any error.
darknessCGFloat between 0 and 1. The higher the value, the darker the video. Defaults to 0.
isMutedBool indicating whether video is muted. Defaults to true.
willLoopVideoBool indicating whether video should restart when finished. Defaults to true.
setAudioSessionAmbientBool indicating whether to set the shared
AVAudioSessionto ambient. If this is not done, audio played from your app will pause other audio playing on the device. Defaults to true. Only has an effect in iOS 10.0+.preventsDisplaySleepDuringVideoPlaybackIf automatic lock is being used on the device setting this property to false will not hold the device awake. default value for iOS is true. Only has an effect in iOS 12.0+
-
Pauses the video.
Declaration
Swift
public func pause() -
Resumes the video.
Declaration
Swift
public func resume() -
Restarts the video from the beginning.
Declaration
Swift
public func restart() -
Generate an image from the video to show as thumbnail
Declaration
Swift
public func getThumbnailImage(from url: URL, at time: CMTime) throws -> UIImageParameters
urlvideo file URL
timetime of video frame to make into thumbnail image
View on GitHub
VideoBackground Class Reference