forked from Guzlan/BackgroundVideoiOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackgroundVideoObjC.m
More file actions
110 lines (89 loc) · 3.94 KB
/
BackgroundVideoObjC.m
File metadata and controls
110 lines (89 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// BackgroundVideoObjC.m
// BackgroundVideoDemo
//
// Created by Adam Albarghouthi on 2016-06-26.
// Copyright © 2016 backgroundVideo. All rights reserved.
//
#import "BackgroundVideoObjC.h"
static BOOL hasBeenUsed = false;
@implementation BackgroundVideoObjC
- (id)initOnViewController:(UIViewController *)onViewController withVideoURL:(NSString *)url {
self = [super init];
if (self) {
viewController = onViewController;
// parse the video string to split it into name and extension
NSArray *videoNameAndExtension = [url componentsSeparatedByString:@"."];
if (videoNameAndExtension.count == 2) {
__weak NSString *videoName = videoNameAndExtension[0];
__weak NSString *videoExtension = videoNameAndExtension[1];
if ([[NSBundle mainBundle] URLForResource:videoName withExtension:videoExtension]) {
videoURL = [[NSBundle mainBundle] URLForResource:videoName withExtension:videoExtension];
// initialize our player with our fetched video url
self.backgroundPlayer = [AVPlayer playerWithURL:videoURL];
}
else {
NSLog(@"Invalid Video");
}
}
else {
NSLog(@"Wrong video name format");
}
}
return self;
}
- (void)dealloc {
if (hasBeenUsed) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
}
}
// setUpBackground is a function that should be called in viewDidLoad to load a local background video to play as your background
- (void)setUpBackground {
self.backgroundPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
self.backgroundPlayer.muted = true;
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.backgroundPlayer];
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; // preserve aspect ratio and resize to fill screen
playerLayer.zPosition = -1; // set position behind anything in our view
playerLayer.frame = viewController.view.frame; // set our player frame to our view's frame
[viewController.view.layer addSublayer:playerLayer];
// prevent video from disturbing audio services from other apps
@try {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
}
@catch (NSException *exception) {
// deal with the exception
NSLog(@"%@", exception.reason);
}
@finally {
// optional block of clean-up code
// executed whether or not an exception occurred
}
// start video
[self.backgroundPlayer play];
// Loop the video when it ends using NSNotifcationCenter
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(loopVideo)
name:AVPlayerItemDidPlayToEndTimeNotification
object:nil];
// call the background video again if your application goes to background and foreground again
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(loopVideo)
name:UIApplicationWillEnterForegroundNotification
object:nil];
hasBeenUsed = true;
}
// private function
// A function that will restarts the video for the purpose of looping
- (void)loopVideo {
[self.backgroundPlayer seekToTime:kCMTimeZero];
[self.backgroundPlayer play];
}
// incase you want to pause or play the video at any moment
- (void)play {
[self.backgroundPlayer play];
}
- (void)pause {
[self.backgroundPlayer pause];
}
@end