IjkplayerPlugin.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #import <AVKit/AVKit.h>
  2. #import "IjkplayerPlugin.h"
  3. #import "CoolFlutterIjkManager.h"
  4. #import "CoolFlutterIJK.h"
  5. #import "FlutterViewController+CoolStatusBarHidden.h"
  6. NSString *flutterOrientationNotifyName = @"io.flutter.plugin.platform.SystemChromeOrientationNotificationName";
  7. const NSString *flutterOrientationNotifyKey = @"io.flutter.plugin.platform.SystemChromeOrientationNotificationKey";
  8. @interface FlutterMethodCall (Ijk)
  9. - (int64_t)getId;
  10. - (int64_t)getIdParamFromDict;
  11. - (NSString *)getStringParam:(NSString *)key;
  12. @end
  13. static IjkplayerPlugin *__sharedInstance;
  14. @implementation IjkplayerPlugin {
  15. CoolFlutterIjkManager *manager;
  16. MPVolumeView *volumeView;
  17. UISlider *volumeViewSlider;
  18. }
  19. + (instancetype)sharedInstance {
  20. return __sharedInstance;
  21. }
  22. - (instancetype)initWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  23. self = [super init];
  24. if (self) {
  25. self.registrar = registrar;
  26. manager = [CoolFlutterIjkManager managerWithRegistrar:registrar];
  27. }
  28. return self;
  29. }
  30. + (instancetype)pluginWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  31. return [[self alloc] initWithRegistrar:registrar];
  32. }
  33. + (void)registerWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  34. FlutterMethodChannel *channel = [FlutterMethodChannel
  35. methodChannelWithName:@"top.kikt/ijkplayer"
  36. binaryMessenger:[registrar messenger]];
  37. IjkplayerPlugin *instance = [IjkplayerPlugin pluginWithRegistrar:registrar];
  38. [registrar addMethodCallDelegate:instance channel:channel];
  39. __sharedInstance = instance;
  40. }
  41. - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
  42. dispatch_queue_t mainQueue = dispatch_get_main_queue();
  43. // __weak typeof(&*self) weakSelf = self;
  44. dispatch_async(mainQueue, ^{
  45. if ([@"create" isEqualToString:call.method]) {
  46. @try {
  47. int64_t id = [self->manager createWithCall:call];
  48. result(@(id));
  49. }
  50. @catch (NSException *exception) {
  51. result([FlutterError errorWithCode:@"1" message:@"创建失败" details:exception]);
  52. }
  53. [self checkVolumeViewShouldShow];
  54. } else if ([@"dispose" isEqualToString:call.method]) {
  55. NSDictionary *params = [call arguments];
  56. int id = [params[@"id"] intValue];
  57. [self->manager disposeWithId:id];
  58. [self checkVolumeViewShouldShow];
  59. result(@(YES));
  60. } else if ([@"init" isEqualToString:call.method]) {
  61. [self->manager disposeAll];
  62. [self checkVolumeViewShouldShow];
  63. result(@YES);
  64. } else if ([@"setSystemVolume" isEqualToString:call.method]) {
  65. NSDictionary *params = [call arguments];
  66. int volume = [params[@"volume"] intValue];
  67. [self setSystemVolume:volume];
  68. result(@YES);
  69. } else if ([@"getSystemVolume" isEqualToString:call.method]) {
  70. int currentVol = [self getSystemVolume];
  71. result(@(currentVol));
  72. } else if ([@"volumeUp" isEqualToString:call.method]) {
  73. int currentVol = [self getSystemVolume];
  74. [self setSystemVolume: currentVol + 3];
  75. currentVol = [self getSystemVolume];
  76. result(@(currentVol));
  77. } else if ([@"volumeDown" isEqualToString:call.method]) {
  78. int currentVol = [self getSystemVolume];
  79. [self setSystemVolume: currentVol - 3];
  80. currentVol = [self getSystemVolume];
  81. result(@(currentVol));
  82. } else if ([@"hideSystemVolumeBar" isEqualToString:call.method]) {
  83. [self hideSystemVolumeBar];
  84. result(@YES);
  85. } else if ([@"setSystemBrightness" isEqualToString:call.method]) {
  86. NSDictionary *params = [call arguments];
  87. CGFloat target = [params[@"brightness"] floatValue];
  88. [[UIScreen mainScreen] setBrightness:target];
  89. result(@YES);
  90. } else if ([@"getSystemBrightness" isEqualToString:call.method]) {
  91. CGFloat brightness = [UIScreen mainScreen].brightness;
  92. result(@(brightness));
  93. } else if ([@"resetBrightness" isEqualToString:call.method]) {
  94. // CGFloat brightness = [UIScreen mainScreen].brightness;
  95. result(@YES);
  96. } else if ([@"setSupportOrientation" isEqualToString:call.method]) {
  97. [self setSupportOrientationWithCall:call];
  98. result(@YES);
  99. } else if([@"setCurrentOrientation" isEqualToString:call.method]){
  100. [self setCurrentOrientationWithCall:call];
  101. result(@YES);
  102. } else if([@"unlockOrientation" isEqualToString:call.method]){
  103. [self unlockOrientation];
  104. result(@YES);
  105. } else if([@"showStatusBar" isEqualToString:call.method]) {
  106. BOOL showStatusBar = [call.arguments boolValue];
  107. [self setStatusBarToShow:showStatusBar];
  108. } else {
  109. result(FlutterMethodNotImplemented);
  110. }
  111. });
  112. }
  113. - (UIDeviceOrientation) convertIntToOrientation:(int)orientation{
  114. switch (orientation) {
  115. case 0:
  116. return UIDeviceOrientationPortrait;
  117. case 1:
  118. return UIDeviceOrientationLandscapeLeft;
  119. case 2:
  120. return UIDeviceOrientationPortraitUpsideDown;
  121. case 3:
  122. return UIDeviceOrientationLandscapeRight;
  123. default:
  124. return UIDeviceOrientationUnknown;
  125. }
  126. }
  127. - (void) setOrientationWithCall:(FlutterMethodCall *)call{
  128. NSDictionary *dict = [call arguments];
  129. NSArray *orientations = dict[@"orientation"];
  130. UIInterfaceOrientationMask mask = 0;
  131. if (orientations.count == 0) {
  132. mask |= UIInterfaceOrientationMaskAll;
  133. }
  134. for (id number in orientations) {
  135. int value = [number intValue];
  136. UIDeviceOrientation orientation = [self convertIntToOrientation:value];
  137. NSLog(@"orientation = %ld",(long)orientation);
  138. if (orientation == UIDeviceOrientationPortrait){
  139. mask |= UIInterfaceOrientationMaskPortrait;
  140. }else if (orientation == UIDeviceOrientationLandscapeLeft) {
  141. mask |= UIInterfaceOrientationMaskLandscapeLeft;
  142. }else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
  143. mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
  144. }else if (orientation == UIDeviceOrientationLandscapeRight) {
  145. mask |= UIInterfaceOrientationMaskLandscapeRight;
  146. }
  147. }
  148. [[NSNotificationCenter defaultCenter] postNotificationName:flutterOrientationNotifyName
  149. object:nil
  150. userInfo:@{flutterOrientationNotifyKey
  151. :@(mask)}];
  152. if(orientations.count != 0 && [[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]){
  153. SEL selector = NSSelectorFromString(@"setOrientation:");
  154. int value = [orientations[0] intValue];
  155. UIDeviceOrientation orientation = [self convertIntToOrientation:value];
  156. int val = orientation;
  157. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
  158. [invocation setSelector:selector];
  159. [invocation setTarget:[UIDevice currentDevice]];
  160. [invocation setArgument:&val atIndex:2];
  161. [invocation invoke];
  162. }
  163. }
  164. - (void) setCurrentOrientationWithCall:(FlutterMethodCall *)call {
  165. if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]){
  166. SEL selector = NSSelectorFromString(@"setOrientation:");
  167. NSDictionary *dict = [call arguments];
  168. int target = [dict[@"target"] intValue];
  169. UIDeviceOrientation orientation = [self convertIntToOrientation:target];
  170. int val = orientation;
  171. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
  172. [invocation setSelector:selector];
  173. [invocation setTarget:[UIDevice currentDevice]];
  174. [invocation setArgument:&val atIndex:2];
  175. [invocation invoke];
  176. NSLog(@"target orientation = %d", val);
  177. }
  178. }
  179. - (void) setSupportOrientationWithCall:(FlutterMethodCall *)call {
  180. NSDictionary *dict = [call arguments];
  181. NSArray *orientations = dict[@"supportOrientation"];
  182. UIInterfaceOrientationMask mask = 0;
  183. if (orientations.count == 0) {
  184. mask |= UIInterfaceOrientationMaskAll;
  185. }
  186. for (id number in orientations) {
  187. int value = [number intValue];
  188. UIDeviceOrientation orientation = [self convertIntToOrientation:value];
  189. NSLog(@"orientation = %ld",orientation);
  190. if (orientation == UIDeviceOrientationPortrait){
  191. mask |= UIInterfaceOrientationMaskPortrait;
  192. }else if (orientation == UIDeviceOrientationLandscapeLeft) {
  193. mask |= UIInterfaceOrientationMaskLandscapeLeft;
  194. }else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
  195. mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
  196. }else if (orientation == UIDeviceOrientationLandscapeRight) {
  197. mask |= UIInterfaceOrientationMaskLandscapeRight;
  198. }
  199. }
  200. [[NSNotificationCenter defaultCenter] postNotificationName:flutterOrientationNotifyName
  201. object:nil
  202. userInfo:@{flutterOrientationNotifyKey
  203. :@(mask)}];
  204. }
  205. - (void) unlockOrientation {
  206. UIDevice *device = [UIDevice currentDevice];
  207. if([device respondsToSelector:@selector(setOrientation:)]){
  208. SEL selector = NSSelectorFromString(@"setOrientation:");
  209. int val = device.orientation;
  210. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
  211. [invocation setSelector:selector];
  212. [invocation setTarget:[UIDevice currentDevice]];
  213. [invocation setArgument:&val atIndex:2];
  214. [invocation invoke];
  215. }
  216. [[NSNotificationCenter defaultCenter] postNotificationName:flutterOrientationNotifyName
  217. object:nil
  218. userInfo:@{flutterOrientationNotifyKey
  219. :@(UIInterfaceOrientationMaskAll)}];
  220. }
  221. - (int) getSystemVolume{
  222. // AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  223. // CGFloat currentVol = audioSession.outputVolume * 100;
  224. // NSLog(@"system volume = %.0f",currentVol);
  225. // return (int)currentVol;
  226. return (int)([self getVolumeWithVolumeView] * 100);
  227. }
  228. - (float) getVolumeWithVolumeView{
  229. [self initVolumeView];
  230. if(!volumeViewSlider){
  231. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  232. CGFloat currentVol = audioSession.outputVolume * 100;
  233. NSLog(@"system volume = %.0f",currentVol);
  234. return (int)currentVol;
  235. }
  236. return volumeViewSlider.value;
  237. }
  238. -(void)initVolumeView{
  239. if(!volumeView){
  240. volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, 0, 10, 10)];
  241. }
  242. if(!volumeViewSlider){
  243. for (UIView *view in [volumeView subviews]) {
  244. if ([view.class.description isEqualToString:@"MPVolumeSlider"]) {
  245. volumeViewSlider = (UISlider *) view;
  246. break;
  247. }
  248. }
  249. }
  250. UIWindow *window = UIApplication.sharedApplication.keyWindow;
  251. [window addSubview:volumeView];
  252. }
  253. - (void) checkVolumeViewShouldShow{
  254. int count = [manager ijkCount];
  255. if (count>0){
  256. [self initVolumeView];
  257. }else{
  258. [volumeView removeFromSuperview];
  259. volumeView = nil;
  260. }
  261. }
  262. - (void)setSystemVolume:(int)volume {
  263. [self initVolumeView];
  264. float targetVolume = ((float) volume) / 100;
  265. if (targetVolume > 1){
  266. targetVolume = 1;
  267. } else if(targetVolume < 0){
  268. targetVolume = 0;
  269. }
  270. // change system volume, the value is between 0.0f and 1.0f
  271. [volumeViewSlider setValue:targetVolume animated:NO];
  272. // send UI control event to make the change effect right now. 立即生效
  273. [volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
  274. // [volumeView removeFromSuperview];
  275. }
  276. -(void)hideSystemVolumeBar {
  277. }
  278. -(void)setStatusBarToShow:(BOOL)show{
  279. UIViewController *ctl = [self getCurrentUIViewController];
  280. if([ctl isKindOfClass:[FlutterViewController class] ]){
  281. FlutterViewController *fCtl = (FlutterViewController*)ctl;
  282. if(show){
  283. [fCtl showStatusBar];
  284. }else{
  285. [fCtl hideStatusBar];
  286. }
  287. }
  288. }
  289. -(UIViewController*) getCurrentUIViewController{
  290. UIViewController *result = nil;
  291. UIWindow *window = [[UIApplication sharedApplication] keyWindow];
  292. if(window.windowLevel != UIWindowLevelNormal){
  293. NSArray *windows =[[UIApplication sharedApplication ]windows];
  294. for(UIWindow *tmp in windows){
  295. window = tmp;
  296. break;
  297. }
  298. }
  299. result = window.rootViewController;
  300. while (result.presentedViewController) {
  301. result = result.presentedViewController;
  302. }
  303. if([result isKindOfClass:[UITabBarController class]]){
  304. result = [(UITabBarController*)result selectedViewController];
  305. }
  306. if([result isKindOfClass:[UINavigationController class]]){
  307. result = [(UINavigationController*) result visibleViewController];
  308. }
  309. return result;
  310. }
  311. @end
  312. @implementation FlutterMethodCall (Ijk)
  313. - (int64_t)getId {
  314. return [[self arguments] intValue];
  315. }
  316. - (int64_t)getIdParamFromDict {
  317. return [[self arguments][@"id"] intValue];
  318. }
  319. - (NSString *)getStringParam:(NSString *)key {
  320. return [self arguments][key];
  321. }
  322. @end