-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHRVideoPlayerView.m
348 lines (293 loc) · 12.5 KB
/
HRVideoPlayerView.m
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//
// HRVideoPlayerView.m
//
// Created by Hemkaran Raghav on 10/4/13.
// Copyright (c) 2013 Hemkaran Raghav. All rights reserved.
//
#import "HRVideoPlayerView.h"
#import <KeepLayout.h>
@implementation HRVideoPlayerView
{
id playbackObserver;
AVPlayerLayer *playerLayer;
BOOL viewIsShowing;
}
-(id)initWithFrame:(CGRect)frame playerItem:(AVPlayerItem*)playerItem
{
self = [super initWithFrame:frame];
if (self) {
self.moviePlayer = [AVPlayer playerWithPlayerItem:playerItem];
playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.moviePlayer];
[playerLayer setFrame:frame];
[self.moviePlayer seekToTime:kCMTimeZero];
[self.layer addSublayer:playerLayer];
self.contentURL = nil;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerFinishedPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
[self initializePlayer:frame];
}
return self;
}
- (id)initWithFrame:(CGRect)frame contentURL:(NSURL*)contentURL
{
self = [super initWithFrame:frame];
if (self) {
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:contentURL];
self.moviePlayer = [AVPlayer playerWithPlayerItem:playerItem];
playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.moviePlayer];
[playerLayer setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
[self.moviePlayer seekToTime:kCMTimeZero];
[self.layer addSublayer:playerLayer];
self.contentURL = contentURL;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerFinishedPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
[self initializePlayer:frame];
}
return self;
}
-(void) setFrame:(CGRect)frame
{
[super setFrame:frame];
[playerLayer setFrame:frame];
}
-(void) setupConstraints
{
// bottom HUD view
self.playerHudBottom.keepHorizontalInsets.equal = KeepRequired(0);
self.playerHudBottom.keepBottomInset.equal = KeepRequired(0);
self.playerHudBottomBGView.keepHorizontalInsets.equal = KeepRequired(0);
self.playerHudBottomBGView.keepBottomInset.equal = KeepRequired(0);
self.playerHudBottomBGView.keepHeight.equal = KeepRequired(15);
// play/pause button
[self.playPauseButton keepHorizontallyCentered];
[self.playPauseButton keepVerticallyCentered];
// current time label
self.playBackTime.keepLeftInset.equal = KeepRequired(5);
[self.playBackTime keepVerticallyCentered];
// progress bar
self.progressBar.keepLeftOffsetTo(self.playBackTime).equal = KeepRequired(5);
self.progressBar.keepBottomInset.equal = KeepRequired(0);
[self.progressBar keepHorizontallyCentered];
[self.progressBar keepVerticallyCentered];
// total time label
self.playBackTotalTime.keepLeftOffsetTo(self.progressBar).equal = KeepRequired(5);
[self.playBackTotalTime keepVerticallyCentered];
// zoom button
self.zoomButton.keepLeftOffsetTo(self.playBackTotalTime).equal = KeepRequired(5);
self.zoomButton.keepRightInset.equal = KeepRequired(5);
[self.zoomButton keepVerticallyCentered];
}
-(void)initializePlayer:(CGRect)frame
{
int frameWidth = frame.size.width;
int frameHeight = frame.size.height;
self.backgroundColor = [UIColor blackColor];
viewIsShowing = NO;
[self.layer setMasksToBounds:YES];
self.playerHudBottom = [[UIView alloc] init];
self.playerHudBottom.frame = CGRectMake(0, 0, frameWidth, 25);
[self.playerHudBottom setBackgroundColor:[UIColor clearColor]];
[self addSubview:self.playerHudBottom];
self.playerHudBottomBGView = [[UIView alloc] init];
self.playerHudBottomBGView.frame = CGRectMake(0, 0, frameWidth, 48*frameHeight/160);
self.playerHudBottomBGView.backgroundColor = [UIColor blackColor];
// Create the colors for our gradient.
UIColor *transparent = [UIColor colorWithWhite:1.0f alpha:0.f];
UIColor *opaque = [UIColor colorWithWhite:1.0f alpha:1.0f];
// Create a masklayer.
CALayer *maskLayer = [[CALayer alloc]init];
maskLayer.frame = self.playerHudBottomBGView.bounds;
CAGradientLayer *gradientLayer = [[CAGradientLayer alloc]init];
gradientLayer.frame = CGRectMake(0,0,self.playerHudBottomBGView.bounds.size.width, self.playerHudBottomBGView.bounds.size.height);
gradientLayer.colors = @[(id)transparent.CGColor, (id)transparent.CGColor, (id)opaque.CGColor, (id)opaque.CGColor];
gradientLayer.locations = @[@0.0f, @0.09f, @0.8f, @1.0f];
// Add the mask.
[maskLayer addSublayer:gradientLayer];
self.playerHudBottomBGView.layer.mask = maskLayer;
[self.playerHudBottom addSubview:self.playerHudBottomBGView];
//Play Pause Button
self.playPauseButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.playPauseButton.frame = CGRectMake(5*frameWidth/240, 6*frameHeight/160, 16*frameWidth/240, 16*frameHeight/160);
[self.playPauseButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.playPauseButton setSelected:NO];
[self.playPauseButton setBackgroundImage:[UIImage imageNamed:@"avplayer.bundle/playback_pause"] forState:UIControlStateSelected];
[self.playPauseButton setBackgroundImage:[UIImage imageNamed:@"avplayer.bundle/playback_play"] forState:UIControlStateNormal];
[self.playPauseButton setTintColor:[UIColor clearColor]];
self.playPauseButton.layer.opacity = 0;
[self addSubview:self.playPauseButton];
//Seek Time Progress Bar
self.progressBar = [[UISlider alloc] init];
self.progressBar.frame = CGRectMake(0, 0, frameWidth, 15);
[self.progressBar addTarget:self action:@selector(progressBarChanged:) forControlEvents:UIControlEventValueChanged];
[self.progressBar addTarget:self action:@selector(proressBarChangeEnded:) forControlEvents:UIControlEventTouchUpInside];
// [self.progressBar setThumbImage:[UIImage imageNamed:@"Slider_button"] forState:UIControlStateNormal];
[self.playerHudBottom addSubview:self.progressBar];
//Current Time Label
self.playBackTime = [[UILabel alloc] init];
[self.playBackTime sizeToFit];
self.playBackTime.text = [self getStringFromCMTime:self.moviePlayer.currentTime];
[self.playBackTime setTextAlignment:NSTextAlignmentLeft];
[self.playBackTime setTextColor:[UIColor whiteColor]];
self.playBackTime.font = [UIFont systemFontOfSize:12*frameWidth/240];
[self.playerHudBottom addSubview:self.playBackTime];
//Total Time label
self.playBackTotalTime = [[UILabel alloc] init];
[self.playBackTotalTime sizeToFit];
self.playBackTotalTime.text = [self getStringFromCMTime:self.moviePlayer.currentItem.asset.duration];
[self.playBackTotalTime setTextAlignment:NSTextAlignmentRight];
[self.playBackTotalTime setTextColor:[UIColor whiteColor]];
self.playBackTotalTime.font = [UIFont systemFontOfSize:12*frameWidth/240];
[self.playerHudBottom addSubview:self.playBackTotalTime];
//zoom button
UIImage *image = [UIImage imageNamed:@"avplayer.bundle/zoomin"];
self.zoomButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.zoomButton.frame = CGRectMake(0,0,image.size.width, image.size.height);
[self.zoomButton addTarget:self action:@selector(zoomButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.zoomButton setBackgroundImage:image forState:UIControlStateNormal];
[self.playerHudBottom addSubview:self.zoomButton];
for (UIView *view in [self subviews]) {
view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
}
CMTime interval = CMTimeMake(33, 1000);
__weak __typeof(self) weakself = self;
playbackObserver = [self.moviePlayer addPeriodicTimeObserverForInterval:interval queue:dispatch_get_main_queue() usingBlock: ^(CMTime time) {
CMTime endTime = CMTimeConvertScale (weakself.moviePlayer.currentItem.asset.duration, weakself.moviePlayer.currentTime.timescale, kCMTimeRoundingMethod_RoundHalfAwayFromZero);
if (CMTimeCompare(endTime, kCMTimeZero) != 0) {
double normalizedTime = (double) weakself.moviePlayer.currentTime.value / (double) endTime.value;
weakself.progressBar.value = normalizedTime;
}
weakself.playBackTime.text = [weakself getStringFromCMTime:weakself.moviePlayer.currentTime];
}];
[self setupConstraints];
[self showHud:NO];
}
-(void)zoomButtonPressed:(UIButton*)sender
{
// [UIView animateWithDuration:0.5 animations:^{
// [self setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)];
// }];
// [self.delegate playerViewZoomButtonClicked:self];
}
-(void)layoutSubviews
{
[super layoutSubviews];
[playerLayer setFrame:self.frame];
}
-(void)setIsFullScreenMode:(BOOL)isFullScreenMode
{
_isFullScreenMode = isFullScreenMode;
if (isFullScreenMode) {
self.backgroundColor = [UIColor blackColor];
} else {
self.backgroundColor = [UIColor clearColor];
}
}
-(void)playerFinishedPlaying
{
[self.moviePlayer pause];
[self.moviePlayer seekToTime:kCMTimeZero];
[self.playPauseButton setSelected:NO];
self.isPlaying = NO;
if ([self.delegate respondsToSelector:@selector(playerFinishedPlayback:)]) {
[self.delegate playerFinishedPlayback:self];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint point = [(UITouch*)[touches anyObject] locationInView:self];
if (CGRectContainsPoint(playerLayer.frame, point)) {
[self showHud:!viewIsShowing];
}
}
-(void) showHud:(BOOL)show
{
__weak __typeof(self) weakself = self;
if(show) {
CGRect frame = self.playerHudBottom.frame;
frame.origin.y = self.bounds.size.height;
[UIView animateWithDuration:0.3 animations:^{
weakself.playerHudBottom.frame = frame;
weakself.playPauseButton.layer.opacity = 0;
viewIsShowing = show;
}];
} else {
CGRect frame = self.playerHudBottom.frame;
frame.origin.y = self.bounds.size.height-self.playerHudBottom.frame.size.height;
[UIView animateWithDuration:0.3 animations:^{
weakself.playerHudBottom.frame = frame;
weakself.playPauseButton.layer.opacity = 1;
viewIsShowing = show;
}];
}
}
-(NSString*)getStringFromCMTime:(CMTime)time
{
Float64 currentSeconds = CMTimeGetSeconds(time);
int mins = currentSeconds/60.0;
int secs = fmodf(currentSeconds, 60.0);
NSString *minsString = mins < 10 ? [NSString stringWithFormat:@"0%d", mins] : [NSString stringWithFormat:@"%d", mins];
NSString *secsString = secs < 10 ? [NSString stringWithFormat:@"0%d", secs] : [NSString stringWithFormat:@"%d", secs];
return [NSString stringWithFormat:@"%@:%@", minsString, secsString];
}
//-(void)volumeButtonPressed:(UIButton*)sender
//{
// if (sender.isSelected) {
// [self.moviePlayer setMuted:YES];
// [sender setSelected:NO];
// } else {
// [self.moviePlayer setMuted:NO];
// [sender setSelected:YES];
// }
//}
-(void)playButtonAction:(UIButton*)sender
{
if (self.isPlaying) {
[self pause];
// [sender setSelected:NO];
} else {
[self play];
// [sender setSelected:YES];
}
}
-(void)progressBarChanged:(UISlider*)sender
{
if (self.isPlaying) {
[self.moviePlayer pause];
}
CMTime seekTime = CMTimeMakeWithSeconds(sender.value * (double)self.moviePlayer.currentItem.asset.duration.value/(double)self.moviePlayer.currentItem.asset.duration.timescale, self.moviePlayer.currentTime.timescale);
[self.moviePlayer seekToTime:seekTime];
}
-(void)proressBarChangeEnded:(UISlider*)sender
{
if (self.isPlaying) {
[self.moviePlayer play];
}
}
-(void)volumeBarChanged:(UISlider*)sender
{
[self.moviePlayer setVolume:sender.value];
}
-(void)play
{
[self.moviePlayer play];
self.isPlaying = YES;
[self.playPauseButton setSelected:YES];
}
-(void)pause
{
[self.moviePlayer pause];
self.isPlaying = NO;
[self.playPauseButton setSelected:NO];
}
-(void)dealloc
{
[self.moviePlayer removeTimeObserver:playbackObserver];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end