FullScreenVideoPlayer.mm
8.4 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
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
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
#include "UnityAppController.h"
#include "UI/UnityView.h"
#include "UI/UnityViewControllerBase.h"
#include "UI/OrientationSupport.h"
#include "UI/UnityAppController+ViewHandling.h"
#include "Unity/ObjCRuntime.h"
#include "Unity/VideoPlayer.h"
#include "PluginBase/UnityViewControllerListener.h"
@interface UICancelGestureRecognizer : UITapGestureRecognizer
@end
@interface AVKitVideoPlayback : NSObject<VideoPlayerDelegate, UIViewControllerTransitioningDelegate, UIGestureRecognizerDelegate>
{
AVPlayerViewController* videoViewController;
VideoPlayer* videoPlayer;
UIColor* bgColor;
const NSString* videoGravity;
BOOL showControls;
BOOL cancelOnTouch;
}
- (void)onPlayerReady;
- (void)onPlayerDidFinishPlayingVideo;
- (id)initAndPlay:(NSURL*)url bgColor:(UIColor*)color showControls:(BOOL)controls videoGravity:(const NSString*)scaling cancelOnTouch:(BOOL)cot;
- (void)actuallyStartTheMovie:(NSURL*)url;
- (void)finish;
@end
static AVKitVideoPlayback* _AVKitVideoPlayback = nil;
@implementation AVKitVideoPlayback
#if PLATFORM_IOS
static void AVPlayerViewController_SetAllowsPictureInPicturePlayback_OldIOSImpl(id self_, SEL _cmd, BOOL allow) {}
static NSUInteger supportedInterfaceOrientations_DefaultImpl(id self_, SEL _cmd)
{
return GetAppController().rootViewController.supportedInterfaceOrientations;
}
static bool prefersStatusBarHidden_DefaultImpl(id self_, SEL _cmd)
{
if (_AVKitVideoPlayback) // video is still playing
return _AVKitVideoPlayback->videoViewController.showsPlaybackControls ? NO : YES;
else // video has beed stopped
return GetAppController().rootViewController.prefersStatusBarHidden;
}
#endif
+ (void)initialize
{
if (self == [AVKitVideoPlayback class])
{
#if PLATFORM_IOS
class_replaceMethod([AVPlayerViewController class], @selector(supportedInterfaceOrientations), (IMP)&supportedInterfaceOrientations_DefaultImpl, UIViewController_supportedInterfaceOrientations_Enc);
class_replaceMethod([AVPlayerViewController class], @selector(prefersStatusBarHidden), (IMP)&prefersStatusBarHidden_DefaultImpl, UIViewController_prefersStatusBarHidden_Enc);
#endif
}
}
- (id)initAndPlay:(NSURL*)url bgColor:(UIColor*)color showControls:(BOOL)controls videoGravity:(const NSString*)scaling cancelOnTouch:(BOOL)cot
{
if ((self = [super init]))
{
UnityPause(1);
showControls = controls;
videoGravity = scaling;
bgColor = color;
cancelOnTouch = cot;
[self performSelector: @selector(actuallyStartTheMovie:) withObject: url afterDelay: 0];
}
return self;
}
- (void)dealloc
{
[self finish];
}
- (void)actuallyStartTheMovie:(NSURL*)url
{
@autoreleasepool
{
videoViewController = [[AVPlayerViewController alloc] init];
videoViewController.showsPlaybackControls = showControls;
videoViewController.view.backgroundColor = bgColor;
videoViewController.videoGravity = (NSString*)videoGravity;
videoViewController.transitioningDelegate = self;
#if PLATFORM_IOS
videoViewController.allowsPictureInPicturePlayback = NO;
#endif
#if PLATFORM_TVOS
// In tvOS clicking Menu button while video is playing will exit the app. So when
// app disables exiting to menu behavior, we need to catch the click and ignore it.
if (!UnityGetAppleTVRemoteAllowExitToMenu())
{
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(handleTap:)];
tapRecognizer.allowedPressTypes = @[@(UIPressTypeMenu)];
[videoViewController.view addGestureRecognizer: tapRecognizer];
}
#endif
if (cancelOnTouch)
{
UICancelGestureRecognizer *cancelTouch = [[UICancelGestureRecognizer alloc] initWithTarget: self action: @selector(handleTap:)];
cancelTouch.delegate = self;
[videoViewController.view addGestureRecognizer: cancelTouch];
}
videoPlayer = [[VideoPlayer alloc] init];
videoPlayer.delegate = self;
[videoPlayer loadVideo: url];
}
}
- (void)handleTap:(UITapGestureRecognizer*)sender
{
if (cancelOnTouch && (sender.state == UIGestureRecognizerStateEnded))
[self finish];
}
- (void)onPlayerReady
{
videoViewController.player = videoPlayer.player;
CGSize screenSize = GetAppController().rootView.bounds.size;
BOOL ret = [VideoPlayer CheckScalingModeAspectFill: videoPlayer.videoSize screenSize: screenSize];
if (ret == YES && [videoViewController.videoGravity isEqualToString: AVLayerVideoGravityResizeAspect] == YES)
{
videoViewController.videoGravity = AVLayerVideoGravityResizeAspectFill;
}
[videoPlayer playVideoPlayer];
#if PLATFORM_TVOS
GetAppController().window.rootViewController = videoViewController;
#else
UIViewController *viewController = [GetAppController() topMostController];
if ([viewController isEqual: videoViewController] == NO && [videoViewController isBeingPresented] == NO)
[viewController presentViewController: videoViewController animated: NO completion: nil];
#endif
}
- (void)onPlayerDidFinishPlayingVideo
{
[self finish];
}
- (void)onPlayerTryResume
{
if (![videoPlayer isPlaying])
[videoPlayer resume];
}
- (void)onPlayerError:(NSError*)error
{
[self finish];
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
if ([dismissed isEqual: videoViewController] == YES)
{
[self finish];
}
return nil;
}
- (void)finish
{
@synchronized(self)
{
#if PLATFORM_TVOS
GetAppController().window.rootViewController = GetAppController().rootViewController;
#else
UIViewController *viewController = [GetAppController() topMostController];
if ([viewController isEqual: videoViewController] == YES && [viewController isBeingDismissed] == NO)
[viewController dismissViewControllerAnimated: NO completion: nil];
#endif
[videoPlayer unloadPlayer];
videoPlayer = nil;
videoViewController = nil;
_AVKitVideoPlayback = nil;
#if PLATFORM_TVOS
UnityCancelTouches();
#endif
if (UnityIsPaused())
UnityPause(0);
}
}
@end
@implementation UICancelGestureRecognizer
//instead of having lots of UITapGestureRecognizers with different finger numbers
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self setState: UIGestureRecognizerStateRecognized];
}
@end
extern "C" void UnityPlayFullScreenVideo(const char* path, const float* color, unsigned controls, unsigned scaling)
{
const BOOL cancelOnTouch[] = { NO, NO, YES, NO };
UIColor* bgColor = [UIColor colorWithRed: color[0] green: color[1] blue: color[2] alpha: color[3]];
const bool isURL = ::strstr(path, "://") != 0;
NSURL* url = nil;
if (isURL)
{
url = [NSURL URLWithString: [NSString stringWithUTF8String: path]];
}
else
{
NSString* relPath = path[0] == '/' ? [NSString stringWithUTF8String: path] : [NSString stringWithFormat: @"Data/Raw/%s", path];
NSString* fullPath = [[NSString stringWithUTF8String: UnityDataBundleDir()] stringByAppendingPathComponent: relPath];
url = [NSURL fileURLWithPath: fullPath];
}
const BOOL showControls[] = { YES, YES, NO, NO };
const NSString* videoGravity[] =
{
AVLayerVideoGravityResizeAspectFill, // ???
AVLayerVideoGravityResizeAspect,
AVLayerVideoGravityResizeAspectFill,
AVLayerVideoGravityResize,
};
if (_AVKitVideoPlayback)
[_AVKitVideoPlayback finish];
_AVKitVideoPlayback = [[AVKitVideoPlayback alloc] initAndPlay: url bgColor: bgColor
showControls: showControls[controls] videoGravity: videoGravity[scaling] cancelOnTouch: cancelOnTouch[controls]];
}
extern "C" void UnityStopFullScreenVideoIfPlaying()
{
if (_AVKitVideoPlayback)
[_AVKitVideoPlayback finish];
}
extern "C" int UnityIsFullScreenPlaying()
{
return _AVKitVideoPlayback ? 1 : 0;
}
extern "C" void TryResumeFullScreenVideo()
{
if (_AVKitVideoPlayback)
[_AVKitVideoPlayback onPlayerTryResume];
}