• Stars
    star
    343
  • Rank 118,909 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ“น Framework to Play a Video in the Background of any UIView

Build Status codecov doccov Platform Swift MIT License CocoaPods Version Status Carthage compatible

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.

Features

  • Play a video with one line of code
  • Supports local videos && videos from a web URL
  • Automatically adjusts when device orientation changes
  • Automatically resumes video when app re-enters foreground
  • Pause, resume, restart, and other controls
  • Loop videos (optional)
  • Mute sound (optional)
  • Darken videos so overlying UI stands out more (optional)
  • Documentation

Contents

  1. Integration
  2. Migration Guide
  3. Usage
  4. License
  5. Authors

Integration

CocoaPods

You 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'

Carthage

You can use Carthage to install SwiftVideoBackground by adding it to your Cartfile:

github "dingwilson/SwiftVideoBackground"

Manually

To use this library in your project manually you may:

  1. for Projects, just drag VideoBackground.swift to the project tree
  2. for Workspaces, include the whole SwiftVideoBackground.xcodeproj

Migration Guide

Version 3.0.0

Version 2.0.0

See the quick migration guide.

Usage

Example

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.

Customization

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.

Controls

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

Singleton

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 UIViews, you need to retain an instance of VideoBackground for each UIView:

let videoBackground1 = VideoBackground()

Adding Videos To Your Project

In order to play local videos, you must add them to your project:

  1. Open project navigator
  2. Select your target
  3. Select Build Phases
  4. Select Copy Bundle Resources
  5. Click + to add a video

add video to project

License

SwiftVideoBackground is released under an MIT License. See LICENSE for details.

Authors

Wilson Ding, Quan Vo

Copyright ยฉ 2016-present Wilson Ding.

Please provide attribution, it is greatly appreciated.

More Repositories

1

MultiPeer

๐Ÿ“ฑ๐Ÿ“ฒ A wrapper for the MultipeerConnectivity framework for automatic offline data transmission between devices
Swift
224
star
2

RandomUserSwift

๐Ÿ‘ค Framework to Generate Random Users - An Unofficial Swift SDK for randomuser.me
Swift
96
star
3

Hackathon-Packing-List

๐Ÿ“‹ Hackathon Packing List - A Handy Guide
18
star
4

MultiPeer_Sample

Sample application for MultiPeer Framework
Swift
16
star
5

CleverCalorie

๐ŸดCodeRED Hackathon Project - iOS Health App. Point your camera at a plate of food and find out the nutritional information in realtime.
Objective-C
8
star
6

ProxyChat

๐Ÿ’ฌTAMUHack Hackathon Project - Offline Swift iOS Social Communicator
Swift
4
star
7

BackTrack

๐ŸŽ™IncubateX Hackathon Project - iOS Passive Audio Recorder w/ IBM Watson STT and AlchemyAPI NLP Integrations
Swift
4
star
8

SousChef

๐Ÿ‘จ๐Ÿปโ€๐Ÿณ HackUTD Hackathon Project - iOS Swift personal assistant for finding and reading recipes
Swift
3
star
9

DivBin

iOS Swift app using object recognition and machine learning to classify objects as recyclable, compostable, donatable, or trash
Swift
2
star
10

Seek

๐Ÿ”Ž PennApps Hackathon Project - iOS app to search YouTube via object recognition and speech-to-text
Swift
2
star
11

Motivation.safariextension

New Safari tab page showing your age
JavaScript
2
star
12

todoist-scripts

Python
2
star
13

CTF-AI

Java
2
star
14

dotfiles

๐Ÿ”ง My dotfiles and system config
Shell
2
star
15

BreathEZ

๐Ÿ’Š SASEHack Hackathon Project - iOS App to regulate breathing for those who suffer from panic or anxiety attacks
Swift
1
star
16

world-mapkit-geojson

Swift
1
star
17

FlightBuddy

Swift
1
star
18

Bridgit

๐Ÿ”Ž Facebook Global Hackathon Finale Hackathon Project - Crowd-Sourced License Plate Detection via OCR
Swift
1
star
19

Database-Project-Bonus

Swift
1
star
20

FitBuddy

Swift
1
star