Giter Site home page Giter Site logo

dingwilson / swiftvideobackground Goto Github PK

View Code? Open in Web Editor NEW
343.0 10.0 55.0 39.66 MB

📹 Framework to Play a Video in the Background of any UIView

Home Page: https://wilsonding.com/SwiftVideoBackground

License: MIT License

Swift 87.85% Objective-C 3.41% Ruby 8.74%
swift video background ui ios background-video video-background uiview

swiftvideobackground's Introduction

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.

swiftvideobackground's People

Contributors

blbnv avatar cruisediary avatar dingwilson avatar jalakoo avatar nebiros avatar quanvo87 avatar yonat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swiftvideobackground's Issues

Dependency "SwiftVideoBackground" has no shared framework schemes for any of the platforms: iOS

When installing with carthage using github "dingwilson/SwiftVideoBackground" and then running:

carthage update --platform iOS

The process fails with:

Dependency "SwiftVideoBackground" has no shared framework schemes for any of the platforms: iOS

I tried with both carthage 0.28.0 and 0.29.0 with the same problem. Not sure what the issue is, but a solution on stack overflow talks about needing the proper targets in xcshareddata/xcschemes/.xcscheme.

If you compare your single scheme to say Alamofire's https://github.com/Alamofire/Alamofire/tree/master/Alamofire.xcodeproj/xcshareddata/xcschemes maybe this provides a clue?

Changing video.

Hello, I'm trying to use this for onboarding videos. As the user hits next I would like to change the video using the same view. Anyway to do this with this plugin? It currently just stays the same video even though I'm trying to update it when the user hits next.

Thanks!

Video stops playing

Video stops playing after exiting to the SpringBoard for few seconds and returning back to the app. The UIView just stops at one frame and "freezes".

Use of unresolved identifier 'VideoBackground()'?

Was this library recently updated?

import UIKit
import SwiftVideoBackground

class MyViewController: UIViewController {
    private let videoBackground = VideoBackground() // error
}

is no longer working: Use of unresolved identifier 'VideoBackground'.

iOS 9

can this pod use for iOS 9 ?

Migrate to singleton

Only one instance of VideoBackground is needed to display any number of video backgrounds on views. Therefore, we will be migrating to a singleton. Note that this will be a breaking change, and will probably occur at release 3.0.0

Deprecate Multiple Videos in Single View

Deprecate ability to play multiple videos one-after-another in the same view, due to issues with how Apple handles each video. This feature will be removed in release 3.0.0.

  • Add deprecation message for next patch release
  • Remove feature in release 3.0.0
  • Add alternative solution using FFMPEG to concatenate video assets

Video not fit

Hi,

I using constraint with all safe area, but my video doesn't fit to full when Im changing iPhone X to iPhone 8 simulator.

How to fix this?

Shared video background?

Hi there, first of all thanks for this great framework, however to display a video in a background on many UIViews without reloading or switch view?

Device auto lock doesn't work when video playing (iOS 11)

The problem is that device does not locking automatically after 30 seconds if user does no input activity in the application with video background (in my case this is login page).

Since we use AVPlayer we can set a property preventsDisplaySleepDuringVideoPlayback but this one is available only on iOS 12.

Is there any ways to support that on iOS 11?

"Unknown class BackgroundVideo in Interface Builder file."

Getting this runtime error. In your example project, when run 'pod install', it works, but gives this warning:

[!] The ExampleLogin [Release] target overrides the ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES build setting defined in `Pods/Target Support Files/Pods-ExampleLogin/Pods-ExampleLogin.release.xcconfig'. This can lead to problems with the CocoaPods installation

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Hi

I tried the VideoBackground when running project facing a error like this ?

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

And the code like this :

backgroundVideo.createBackgroundVideo(name: "Video", type: "mp4")
backgroundVideo.isMuted = true

FYI, I've done to embedded the video and copy if Need to the proejcts

any suggestion ?

Possible memory leak

Playing with instruments shows a 16 kb leak. Going to investigate further. What I've observed:

  • happens if video has sound
  • responsible frame: UISoundNewDevice

Videos without sound don't appear to leak. Could also be a completely separate issue caused by something else in the app I was testing.

Music

So here’s the thing. When I’m playing music and I open the app with this video layer the music stops playing music even though the isMuted property is set to true. Can we make it so that if the video layer is muted this won’t happen? It’s a matter of better UX.

UiView is dependent of the current selected "view as"-option on Storyboard

So I noticed some weird behavior of the UiView, which includes the background video. It is dependent by the current selected "view as:" option on Storyboard (see this screenshot for illustration: http://www2.pic-upload.de/img/32461676/orientation.jpg ). If "iPhone 4" is selected for example, and the program is compiled and started on a iPhone 7 the background video appears to be too small (also AutoLayout of the UiView is set correctly like in the provided example).

Did you know this is a thing? Is this on purpose? Thanks!

add pause and resume

Scenario:

  • a video is being played on a UIView
  • that UIView becomes hidden (ie another view controller is pushed onto the stack)
  • that UIView becomes un-hidden again (ie previously mentioned view controller is closed)

What happens:
It seems that while UIView was hidden, the video wasn't playing, and when it gets un-hidden, it plays the video super fast, up to the point that it would be if it wasn't paused.

Not sure if that makes sense or if that's actually what's happening, but I know there is some super fast video playing after a hidden/un-hidden scenario.

I think a fix would be to implement pause() and resume(). I'm hoping correctly using these methods when you expect your view to be hidden/un-hidden fixes this.

This might also be able to be automated if desired (not sure if possible, not sure of side effects).

Create example application

  • Move sources files into Sources folder
  • Create example project inside xcodeproj, including a mp4 video asset
  • Add tests using mp4 video asset
  • Change podspec to only target source files in Sources folder
  • Investigate changes necessary to Carthage

Swift 4 Compatibility

'AVLayerVideoGravityResizeAspectFill' has been renamed to 'AVLayerVideoGravity.resizeAspectFill'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.