/
ScreenRecordModule.mm
165 lines (127 loc) · 5.69 KB
/
ScreenRecordModule.mm
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
#import "ScreenRecordModule.h"
// the actaul module part!
@implementation ScreenRecordModule
+(long long)buttonStyle {
return 1;
}
-(id)contentViewController {
buttonController = (TVSMButtonViewController*)[super contentViewController];
[buttonController setStyle:1];
[self setupImageBasedOnToggleStatus];
return buttonController;
}
-(void)handleAction {
// setup the recorder using its sharedRecorder instance
self.screenRecorder = [RPScreenRecorder sharedRecorder];
// check to see if it's recording or not!
// if it isn't start recording!
if (!self.screenRecorder.isRecording) {
[self startRecording];
[self performSelector:@selector(playRecordingStarted) withObject:nil afterDelay:0.5];
if (@available(tvOS 14.0, *)) {
[buttonController setSymbolTintColor:[UIColor redColor]];
[buttonController setToggledOn:true];
}
}
if (self.screenRecorder.isRecording) {
// if the screen recorder is recording stop it and save it to the camera roll! by default Apple does this!
[self playRecordingStopped];
[self stopRecording];
[self showAlertToUser];
}
}
-(void)setupImageBasedOnToggleStatus {
packageFile = [[self bundle] pathForResource:@"Record" ofType:@"png"];
theImage = [[UIImage imageWithContentsOfFile:packageFile] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[buttonController setImage:theImage];
if([[RPScreenRecorder sharedRecorder] isRecording]) {
if (@available(tvOS 14.0, *)) {
[buttonController setSymbolTintColor:[UIColor redColor]];
[buttonController setToggledOn:true];
}
} else {
if (@available(tvOS 14.0, *)) {
[buttonController setSymbolTintColor:nil];
[buttonController setToggledOn:false];
}
}
}
-(void)startRecording {
// Starts recording with the microphone enabled (by default this must be enabled in order for us to get audio from the Apple TV.)
[self.screenRecorder startSystemRecordingWithMicrophoneEnabled:YES handler:nil];
/*record the screen by setting the UIWindow to record the contentView of the TVSMModuleContentViewController.
This is how Apple does it with their private one found in internal installs! */
[self.screenRecorder setWindowToRecord:self.recordingContentViewController.view.window];
// dismiss the cc after 3 seconds have past.
[self performSelector:@selector(dismissControlCenterAfterRecording) withObject:nil afterDelay:3.0];
}
-(void)stopRecording {
[self.screenRecorder stopSystemRecordingWithURLHandler:^(id videoURL, NSError *error) {
videoPath = [videoURL path];
dispatch_async(dispatch_get_main_queue(), ^{
// save the video then airdrop it!
UISaveVideoAtPathToSavedPhotosAlbum(videoPath, nil, nil, nil);
[self airdropVideo:[videoURL path]];
});
}];
// dismiss the cc after 1 second has past.
[self dismissControlCenterAfterRecording];
[self playRecordingStopped];
if (@available(tvOS 14.0, *)) {
[buttonController setSymbolTintColor:defaultColor];
[buttonController setToggledOn:false];
}
}
//easier to track light/dark mode (thanks nitoTV)
-(BOOL)darkMode {
return (buttonController.view.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark);
}
-(void)airdropVideo:(NSString *)path {
// here we are initializing Breezy and getting ready to AirDrop the screen recording(s)
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"airdropper://%@", [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
UIApplication *application = [UIApplication sharedApplication];
[application openURL:url options:@{} completionHandler:nil];
}
-(void)playRecordingStarted {
if (!player) {
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:recordingStarted] error:nil];
player.volume = 1.0;
[player prepareToPlay];
[player play];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
}
}
-(void)playRecordingStopped {
if (!player2) {
player2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:recordingStopped] error:nil];
player2.volume = 1.0;
[player2 prepareToPlay];
[player2 play];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
}
}
-(void)showAlertToUser {
// thanks nitoTV!
// let user know recording has finished!
dict = [NSMutableDictionary new];
dict[@"message"] = @"AirDrop Package Inbound";
dict[@"title"] = @"Screen Recording Done!";
dict[@"timeout"] = @3;
notificationImageFile =[[self bundle] pathForResource:@"RecordNotif" ofType:@"png"];
notificationImage = [UIImage imageWithContentsOfFile:notificationImageFile];
imageData = UIImagePNGRepresentation(notificationImage);
if (imageData) {
dict[@"imageData"] = imageData;
}
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.nito.bulletinh4x/displayBulletin" object:nil userInfo:dict];
}
// dismisses the control center when we want it to
-(void)dismissControlCenterAfterRecording {
id controlCenter = [NSClassFromString(@"TVSMSystemMenuManager") sharedInstance];
[controlCenter dismissSystemMenu];
}
// I'm returning no on this because i want the control center open for at least 3 seconds before it closes.
-(BOOL)dismissAfterAction {
return NO;
}
@end