UIImageView subclass that allows you to display a looped video and dynamically switch it.

Overview

AKVideoImageView

Build Test Platform CocoaPods

Motivation

AKVideoImageView was created because I wasn't satisfied with the standard AVPlayer when I was implementing a video background for one of my apps. The main issue is that AVPlayer doesn't let the phone go to sleep mode. Additionally, you can't insensibly start the video from the first frame when the app enters the background. This class solves these problems and has a less noticeable lag when switching from the last to the first frame of the video.

AKVideoImageView Example

Features

  • Allows the phone to go to the sleep mode
  • Ability to dynamically switch videos
  • Automatically switches to the first frame of video to have a seamless transition when the app returns from the background
  • Minimal memory footprint
  • Good performance
  • Ability to use mp4 files as the video source
  • Interface Builder support

Usage

Compressing your video file

Before starting using this class, you need to properly compress the video.
Here is an example of libx264 compression options on OS X system using FFmpeg utility:

ffmpeg -i input.mov -vcodec libx264 -level 3.1 -pix_fmt yuv420p -threads 1 -preset placebo -crf 19 -tune film -x264opts colorprim=bt709:transfer=bt709:colormatrix=bt709:fullrange=off output.mp4

Using Interface Builder

Just drag the UIImageView to your UIView and set its class to the AKVideoImageView. In the Attributes Inspector set the Video File Name to the name of your .mp4 video file without extension. See an example project for more info if needed.

Using Code

Objective-C:

#import "AKVideoImageView.h"

NSURL *videoURL = [[NSBundle mainBundle] URLForResource:@"videoName" withExtension:@"mp4"];
AKVideoImageView *videoBG = [[AKVideoImageView alloc] initWithFrame:self.view.bounds
                                                           videoURL:videoURL];
[self.view addSubview:videoBG];
[self.view sendSubviewToBack:videoBG];

Swift:

import AKVideoImageView

let url = Bundle.main.url(forResource: "video_1", withExtension: "mp4")!
let videoView = AKVideoImageView(frame: view.bounds, videoURL: url)!
view.addSubview(videoView)

Dynamically changing video

NSURL *videoURL = [[NSBundle mainBundle] URLForResource:@"anotherVideoName" withExtension:@"mp4"];
self.videoBG.videoURL = videoURL;

Installation

Manually

Just add AKVideoImageView.h and AKVideoImageView.m files to your project.

CocoaPods

Add the following line to your Podfile.

pod "AKVideoImageView", "~> 1.1"

Then run pod install.

License (MIT)

Copyright (c) 2017 Oleksandr Kirichenko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Comments
  • app crash

    app crash

    crash issue like below: Thumbnail image generation error Error Domain=AVFoundationErrorDomain Code=-11839 "無法解碼" UserInfo={NSUnderlyingError=0x1356ce6a0 {Error Domain=NSOSStatusErrorDomain Code=-12913 "(null)"}, NSLocalizedFailureReason=此媒體所需的解碼器忙碌中。, NSLocalizedRecoverySuggestion=停止其他解碼媒體的動作,然後再試一次。, NSLocalizedDescription=無法解碼} Please give me some help thx

    opened by furueili 6
  • not compatible with Swift 3??

    not compatible with Swift 3??

    I'm having issues when trying to use this library with swift 3. First, complainig about NSURL (now named URL) and also, once removed the asserts for NSURL, I receive an error: "[AVAssetReaderTrackOutput initWithTrack:outputSettings:] invalid parameter not satisfying: track != ((void *)0)"

    help?

    opened by zfrankz 4
  • Memory leaks

    Memory leaks

    When leaving AKVideoImageView runnin & runnin, and worse with 2 or 3 differents AKVideoImageView looping on a video, the memory used is always increasing...

    opened by cutmaster 2
  • Add some code for a better integration in storyboards

    Add some code for a better integration in storyboards

    Your class is very cool. Otherwise, it can be better if used by the integration directly in the storyboard (change UIImageView into AKVideoImageView). To do so, it is necessary to test that the videoURL can be nul inside the playvideo method, then to add the ability to call playVideo on the setVideoURL method if the previous instance of videoURL was nil.

    opened by cutmaster 1
  • load video with URL not working

    load video with URL not working

    When I tried to use video via link then it is crashing

      NSURL *videoURL = [NSURL URLWithString:@"http://www.webestools.com/page/media/videoTag/BigBuckBunny.mp4"];
    
      AKVideoImageView *videoBG = [[AKVideoImageView alloc] initWithFrame:self.view.bounds
                                                               videoURL:videoURL];
    

    Crash log

    Thumbnail image generation error Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x600000240e70 {Error Domain=NSOSStatusErrorDomain Code=-1022 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-1022), NSLocalizedDescription=The operation could not be completed} 2017-08-09 11:57:15.747 Example[15354:280879] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVAssetReaderTrackOutput initWithTrack:outputSettings:] invalid parameter not satisfying: track != ((void *)0)' *** First throw call stack: ( 0 CoreFoundation 0x0000000105832b0b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x0000000104f27141 objc_exception_throw + 48 2 AVFoundation 0x00000001054815ea -[AVAssetReaderTrackOutput dealloc] + 0 3 AVFoundation 0x000000010548118d +[AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:outputSettings:] + 62 4 Example 0x0000000104951ba5 -[AKVideoImageView createAssetReader] + 1045 5 Example 0x00000001049520a6 -[AKVideoImageView playVideo] + 870 6 Example 0x000000010495150b -[AKVideoImageView didMoveToSuperview] + 43 7 UIKit 0x000000010650fb53 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 932 8 UIKit 0x000000010650f72d -[UIView(Hierarchy) _postMovedFromSuperview:] + 828 9 UIKit 0x000000010651f6ba -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1927 10 UIKit 0x000000010650d9a8 -[UIView(Hierarchy) addSubview:] + 838 11 Example 0x0000000104950812 -[ViewController viewDidLoad] + 370 12 UIKit 0x0000000106602cca -[UIViewController loadViewIfRequired] + 1235 13 UIKit 0x000000010660310a -[UIViewController view] + 27 14 UIKit 0x00000001064cb63a -[UIWindow addRootViewControllerViewIfPossible] + 65 15 UIKit 0x00000001064cbd20 -[UIWindow _setHidden:forced:] + 294 16 UIKit 0x00000001064deb6e -[UIWindow makeKeyAndVisible] + 42 17 UIKit 0x000000010645831f -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4346 18 UIKit 0x000000010645e584 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1709 19 UIKit 0x000000010645b793 -[UIApplication workspaceDidEndTransaction:] + 182 20 FrontBoardServices 0x000000010b0de5f6 FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK + 24 21 FrontBoardServices 0x000000010b0de46d -[FBSSerialQueue _performNext] + 186 22 FrontBoardServices 0x000000010b0de7f6 -[FBSSerialQueue _performNextFromRunLoopSource] + 45 23 CoreFoundation 0x00000001057d8c01 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 24 CoreFoundation 0x00000001057be0cf __CFRunLoopDoSources0 + 527 25 CoreFoundation 0x00000001057bd5ff __CFRunLoopRun + 911 26 CoreFoundation 0x00000001057bd016 CFRunLoopRunSpecific + 406 27 UIKit 0x000000010645a02f -[UIApplication _run] + 468 28 UIKit 0x00000001064600d4 UIApplicationMain + 159 29 Example 0x0000000104953acf main + 111 30 libdyld.dylib 0x0000000108f3165d start + 1 31 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

    opened by patelmayank 1
Releases(1.1.4)
Owner
Oleksandr Kirichenko
Mobile app developer specialising in the native iOS and cross-platform Flutter app development.
Oleksandr Kirichenko
Letters animation allows you to click on different letters and accordingly it will animate letters in a cool way. It has a very attractive UI and is very easy to use.

Letters Animation Cool Letters Animation in iOS written in Swift. Preview Table of content :- Description How to add in your project Requirement Licen

MindInventory 31 Oct 4, 2022
Numbers animation allows you to click on different numbers and accordingly it will animate numbers in a cool way. It has a very attractive UI and is very easy to use.

Numbers Animation Cool Numbers Animation in iOS written in Swift. Preview Table of content :- Description How to add in your project Requirement Licen

MindInventory 31 Oct 4, 2022
Reading animation allows you to click on the different page numbers and accordingly it will animate page changes in a cool way. It has a very attractive UI and is very easy to use.

Reading Animation Cool Reading Animation in iOS written in Swift. Preview Table of content :- Description How to add in your project Requirement Licen

MindInventory 42 Oct 4, 2022
Simple UIButton subclass with additional state change animations (e.g. backgroundColor) and missing features

SimpleButton UIButton subclass with animated, state-aware attributes. Easy to subclass and configure! Full API docs Usage Just create your own SimpleB

Andreas Tinoco Lobo 169 Sep 14, 2022
DynamicBlurView is a dynamic and high performance UIView subclass for Blur.

DynamicBlurView DynamicBlurView is a dynamic and high performance UIView subclass for Blur. Appetize's Demo Since using the CADisplayLink, it is a hig

Kyohei Ito 929 Jan 5, 2023
ButtonClickStyle - This is a Customizable/Designable Button View, with 15 animated click styles, that allows you to design your own buttons from subviews, in storyboard and xib right away.

ButtonClickStyle - This is a Customizable/Designable Button View, with 15 animated click styles, that allows you to design your own buttons from subviews, in storyboard and xib right away.

Rustam 25 Oct 10, 2022
A UICollectionViewLayout subclass that adds custom transitions/animations to the UICollectionView without effecting your existing code.

AnimatedCollectionViewLayout Normally a UICollectionView has no transition effects when you scroll from one item to another. There are lots of ways to

Jin Wang 4.5k Jan 1, 2023
UIView subclass that bends its edges when its position changes.

AHKBendableView BendableView is a UIView subclass that bends its edges when its position change is animated. Internally, BendableView contains CAShape

Arek Holko 591 Jul 24, 2022
MotionBlur allows you to add motion blur effect to iOS animations.

MotionBlur MotionBlur allows you to add motion blur effect to your animations (currently only position's change). See the accompanying blog post to le

Arek Holko 1.5k Nov 3, 2022
Various view's effects for iOS, written in Swift. Allows you to animate views nicely with easy to use extensions

Various view's effects for iOS, written in Swift. Allows you to animate views nicely with easy to use extensions. Every animation is randomized. Currently supported animations:

Artur Rymarz 23 Aug 23, 2022
Each step you take reveals a new horizon. You have taken the first step today.

The story Seeing the animations behind Paper, or the transitions behind Mail, being in a world of flat design and transitions, user interaction, app b

Ramon Gilabert 152 Nov 3, 2022
The Effects Library allows developers to create sophisticated and realistic particle systems such as snow, fire, rain, confetti, fireworks, and smoke with no or minimal effort.

The Effects Library allows developers to create sophisticated and realistic particle systems such as snow, fire, rain, confetti, fireworks, and smoke with no or minimal effort.

Stream 182 Jan 6, 2023
MGFlipView allows to create flipping view in easy way without worrying about flipping animation and flipping logic.

MGFlipView About If you are looking for an easy way of implement 3D flipping view, you are in the right place. MGFlipView allows to create flipping vi

Maciej Gomółka 47 Sep 28, 2022
Allows a swipe on any part of the screen to start an interruptible pop animation to the previous view

Lazy Pop SwiftUI Swiping on any part of the screen starts an interruptible pop animation to the previous view. Forked from https://github.com/rishi420

Joe Hinkle 150 Dec 18, 2022
LottieView - Wrapper around Lottie in SwiftUI that allows chaining animations & triggering callbacks at certain frames

LottieView is a Wrapper around Lottie in SwiftUI that allows chaining animations & triggering callbacks at certain frames

Ahmed Ramy 0 Jan 23, 2022
Presentation helps you to make tutorials, release notes and animated pages.

Presentation helps you to make tutorials, release notes and animated pages.

HyperRedink 3k Dec 28, 2022
Protoyping-Project WallAngle - With this App, you can measure the angle without searching for tools and calculations

Protoyping-Project_WallAngle This is the App that I made for the Prototyping Pro

null 1 Apr 4, 2022
Poi - You can use tinder UI like tableview method

Poi You can use tinder UI like tableview method Installation Manual Installation Use this command git clone [email protected]:HideakiTouhara/Poi.git Imp

null 65 Nov 7, 2022