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 = 0
  • Change this Bool to mute/unmute the video.

    Declaration

    Swift

    public var isMuted = true
  • Change this Bool to set whether the video restarts when it ends.

    Declaration

    Swift

    public var willLoopVideo = true
  • The AVPlayerLayer that can be accessed for advanced customization.

    Declaration

    Swift

    public lazy var playerLayer = AVPlayerLayer()
  • 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.

    Declaration

    Swift

    public func play(view: UIView,
                     videoName: String,
                     videoType: String,
                     isMuted: Bool = true,
                     darkness: CGFloat = 0,
                     willLoopVideo: Bool = true,
                     setAudioSessionAmbient: 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)

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

  • 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()