-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQHQPlayerView.m
103 lines (85 loc) · 2.89 KB
/
QHQPlayerView.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
//
// QHQPlayView.m
//
// Created by lujianwen on 2018/11/29.
// Copyright © 2018 All rights reserved.
//
#import "QHQPlayerView.h"
@interface QHQPlayerView ()
@property (nonatomic, strong, readwrite) AVPlayer *player;
@property (nonatomic, strong) AVPlayerLayer *playerLayer;
@property (nonatomic, strong) AVPlayerItem *playItem;
@property (nonatomic, strong) UIImageView *placeholderView;
@end
@implementation QHQPlayerView
- (void)dealloc {
[_playItem removeObserver:self forKeyPath:@"status"];
}
- (instancetype)init
{
self = [super init];
if (self) {
_placeholderView = [[UIImageView alloc] init];
[self addSubview:_placeholderView];
}
return self;
}
- (void)setPath:(NSString *)path {
[self setPath:path placeholder:YES];
}
- (void)setPath:(NSString *)path placeholder:(BOOL)placeholder {
_path = path;
NSURL *url;
if (_type == kQHQPlayerViewResourceTypeNative) {
url = [NSURL fileURLWithPath:path];
} else {
url = [NSURL URLWithString:path];
}
if (placeholder) {
_placeholderView.image = [QHQPlayerView getVideoPreViewImage:url];
}
_playItem = [AVPlayerItem playerItemWithURL:url];
_player = [AVPlayer playerWithPlayerItem:_playItem];
[_playItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
_playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
_playerLayer.contentsScale = [UIScreen mainScreen].scale;
[self.layer addSublayer:_playerLayer];
}
- (void)play {
[_playItem seekToTime:kCMTimeZero];
[_player play];
}
- (void)pause {
[_player pause];
[_playItem seekToTime:kCMTimeZero];
}
- (void)layoutSubviews {
[super layoutSubviews];
_playerLayer.frame = self.bounds;
_placeholderView.frame = self.bounds;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
AVPlayerItem *playerItem = (AVPlayerItem *)object;
if ([keyPath isEqualToString:@"status"]){
if (playerItem.status == AVPlayerItemStatusReadyToPlay){
NSLog(@"playerItem is ready");
_placeholderView.hidden = YES;
} else{
NSLog(@"load break");
}
}
}
+ (UIImage*)getVideoPreViewImage:(NSURL *)path {
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:path options:nil];
AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
assetGen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [assetGen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *videoImage = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return videoImage;
}
@end