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