-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRNStringeeVideoView.m
More file actions
102 lines (88 loc) · 3.5 KB
/
RNStringeeVideoView.m
File metadata and controls
102 lines (88 loc) · 3.5 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
#import "RNStringeeVideoView.h"
#import "RNStringeeInstanceManager.h"
#import <React/RCTLog.h>
@implementation RNStringeeVideoView {
BOOL hasDisplayed;
}
- (instancetype)init
{
self = [super init];
if (self) {
[self addObserver:self forKeyPath:@"bounds" options:0 context:nil];
[self addObserver:self forKeyPath:@"frame" options:0 context:nil];
self.videoSize = CGSizeZero;
}
return self;
}
- (void)dealloc
{
[self removeObserver:self forKeyPath:@"bounds"];
[self removeObserver:self forKeyPath:@"frame"];
}
- (void)layoutSubviews {
[super layoutSubviews];
StringeeVideoContentMode mode = StringeeVideoContentModeScaleAspectFill;
if ([_scalingType isEqualToString:@"fit"]) {
mode = StringeeVideoContentModeScaleAspectFit;
}
if (!hasDisplayed) {
if (_callId.length) {
[[RNStringeeInstanceManager instance].rnCall addRenderToView:self callId:_callId isLocal:_local contentMode:mode];
[[RNStringeeInstanceManager instance].rnCall2 addRenderToView:self callId:_callId isLocal:_local contentMode:mode];
hasDisplayed = YES;
}
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == self) {
if (([keyPath isEqualToString:@"bounds"] || [keyPath isEqualToString:@"frame"])) {
if (!CGSizeEqualToSize(self.videoSize, CGSizeZero)) {
if (self.subviews != nil && self.subviews.count > 0 && self.subviews[0] != nil) {
UIView *subView = (UIView *)self.subviews[0];
[self updateFrameToFitVideoSize:self.videoSize subView:subView superView:self];
}
}
}
}
}
- (void)videoView:(StringeeRemoteVideoView *)videoView didChangeVideoSize:(CGSize)size {
// Thay đổi frame của StringeeRemoteVideoView khi kích thước video thay đổi
self.videoSize = size;
[self updateFrameToFitVideoSize:size subView:videoView superView:self];
// CGFloat superWidth = self.bounds.size.width;
// CGFloat superHeight = self.bounds.size.height;
//
// CGFloat newWidth;
// CGFloat newHeight;
//
// if (size.width > size.height) {
// newWidth = superWidth;
// newHeight = newWidth * size.height / size.width;
//
// [videoView setFrame:CGRectMake(0, (superHeight - newHeight) / 2, newWidth, newHeight)];
//
// } else {
// newHeight = superHeight;
// newWidth = newHeight * size.width / size.height;
//
// [videoView setFrame:CGRectMake((superWidth - newWidth) / 2, 0, newWidth, newHeight)];
// }
}
- (void)updateFrameToFitVideoSize:(CGSize)size subView:(UIView *)subView superView:(UIView *)superView {
dispatch_async(dispatch_get_main_queue(), ^{
CGFloat superWidth = superView.frame.size.width;
CGFloat superHeight = superView.frame.size.height;
CGFloat newWidth, newHeight;
if (size.width > size.height) {
newWidth = superWidth;
newHeight = newWidth * size.height / size.width;
subView.frame = CGRectMake(0, (superHeight - newHeight) * 0.5, newWidth, newHeight);
} else {
newHeight = superHeight;
newWidth = newHeight * size.width / size.height;
subView.frame = CGRectMake((superWidth - newWidth) * 0.5, 0, newWidth, newHeight);
}
});
}
@end