IjkplayerPlugin.m 12 KB

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