VideoBackground

public class VideoBackground

Class that plays and manages control of a video on a UIView.

  • Singleton that can play one video on one UIView at a time.

    Declaration

    Swift

    public static let shared: VideoBackground
  • 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.

    Declaration

    Swift

    public var darkness: CGFloat { get set }
  • Change this Bool to mute/unmute the video.

    Declaration

    Swift

    public var isMuted: Bool { get set }
  • Change this Bool to 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 AVPlayerLayer that can be accessed for advanced customization.

    Declaration

    Swift

    public lazy var playerLayer: AVPlayerLayer { get set }
  • You only need to initialize your own instance of VideoBackground if you are playing multiple videos on multiple UIViews. Otherwise just use the shared singleton.

    Declaration

    Swift

    public init()
  • Plays a local video.

    Throws

    VideoBackgroundError.videoNotFound if 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) throws

    Parameters

    view

    UIView that the video will be played on.

    videoName

    String name of video that you have added to your project.

    videoType

    String type of the video. e.g. “mp4”

    darkness

    CGFloat between 0 and 1. The higher the value, the darker the video. Defaults to 0.

    isMuted

    Bool indicating whether video is muted. Defaults to true.

    willLoopVideo

    Bool indicating whether video should restart when finished. Defaults to true.

    setAudioSessionAmbient

    Bool indicating 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. Only has an effect in iOS 10.0+.

  • 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

    view

    UIView that the video will be played on.

    url

    URL 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.

    darkness

    CGFloat between 0 and 1. The higher the value, the darker the video. Defaults to 0.

    isMuted

    Bool indicating whether video is muted. Defaults to true.

    willLoopVideo

    Bool indicating whether video should restart when finished. Defaults to true.

    setAudioSessionAmbient

    Bool indicating 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. Only has an effect in iOS 10.0+.

    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+

  • 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 -> UIImage

    Parameters

    url

    video file URL

    time

    time of video frame to make into thumbnail image