-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoViewController.m
More file actions
103 lines (80 loc) · 3.14 KB
/
VideoViewController.m
File metadata and controls
103 lines (80 loc) · 3.14 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
//
// TestVideoViewController.m
// Memento
//
// Created by Ömer Faruk Gül on 22/05/15.
// Copyright (c) 2015 Ömer Faruk Gül. All rights reserved.
//
#import "VideoViewController.h"
@import AVFoundation;
@interface VideoViewController ()
@property (strong, nonatomic) NSURL *videoUrl;
@property (strong, nonatomic) AVPlayer *avPlayer;
@property (strong, nonatomic) AVPlayerLayer *avPlayerLayer;
@property (strong, nonatomic) UIButton *cancelButton;
@end
@implementation VideoViewController
- (instancetype)initWithVideoUrl:(NSURL *)url {
self = [super init];
if(self) {
_videoUrl = url;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// the video player
self.avPlayer = [AVPlayer playerWithURL:self.videoUrl];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
//self.avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avPlayer currentItem]];
CGRect screenRect = [[UIScreen mainScreen] bounds];
self.avPlayerLayer.frame = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);
[self.view.layer addSublayer:self.avPlayerLayer];
// cancel button
[self.view addSubview:self.cancelButton];
[self.cancelButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.cancelButton.frame = CGRectMake(0, 0, 44, 44);
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.avPlayer play];
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
- (UIButton *)cancelButton {
if(!_cancelButton) {
UIImage *cancelImage = [UIImage imageNamed:@"cancel.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.tintColor = [UIColor whiteColor];
[button setImage:cancelImage forState:UIControlStateNormal];
button.imageView.clipsToBounds = NO;
button.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
button.layer.shadowColor = [UIColor blackColor].CGColor;
button.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
button.layer.shadowOpacity = 0.4f;
button.layer.shadowRadius = 1.0f;
button.clipsToBounds = NO;
_cancelButton = button;
}
return _cancelButton;
}
- (void)cancelButtonPressed:(UIButton *)button {
NSLog(@"cancel button pressed!");
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end