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.
Requirements
- Swift 3+
- iOS 8+
Integration
CocoaPods
You can use CocoaPods to install SwiftVideoBackground
by adding it to your Podfile
:
For Swift 4:
pod 'SwiftVideoBackground', '~> 2.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:
- for Projects, just drag VideoBackground.swift to the project tree
- for Workspaces, include the whole SwiftVideoBackground.xcodeproj
Migration Guide
Version 2.0.0 brings improvements and breaking changes. See the quick migration guide here.
Usage
Example
import UIKit
import SwiftVideoBackground
class MyViewController: UIViewController {
private let videoBackground = VideoBackground()
override func viewDidLoad() {
super.viewDidLoad()
videoBackground.play(view: view, videoName: "myvideo", videoType: "mp4")
}
}
Documentation for Version 0.06 (Swift 3) can be found here.
Customization Options
play()
has three additional optional parameters for customization:
isMuted
: Bool - Indicates whether video is muted. Defaults totrue
.alpha
: CGFloat - Value between 0 and 1. The higher the value, the darker the video. Defaults to0
.willLoopVideo
: Bool - Indicates whether video should restart when finished. Defaults totrue
.
So for example:
videoBackground.play(view: view,
videoName: "myvideo",
videoType: "mp4",
isMuted: false,
alpha: 0.25,
willLoopVideo: true)
-> will play the video with the sound on, slightly darkened, and will continuously loop.
Any combination of the three can be included or left out.
Adding Videos To Your Project
You must properly add videos to your project in order to play them. To do this:
- Open your project navigator
- Select your target
- Select
Build Phases
- Select
Copy Bundle Resources
- Click
+
to add a video
Preventing Video From Muting Other Audio On Device
By default, audio played on your app will mute all other audio playing on the device. To prevent this, configure the AVAudioSession
by adding the following code to your app’s application(_:didFinishLaunchingWithOptions)
function in the AppDelegate
class:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 10.0, *) {
try? AVAudioSession.sharedInstance().setCategory(
AVAudioSessionCategoryAmbient,
mode: AVAudioSessionModeDefault
)
try? AVAudioSession.sharedInstance().setActive(true)
}
return true
}
For more information, see the AVPlayer docs.
License
SwiftVideoBackground
is released under an MIT License. See LICENSE for details.
Authors
Copyright © 2016-present Wilson Ding.
Please provide attribution, it is greatly appreciated.