IjkplayerPlugin.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #import <AVKit/AVKit.h>
  2. #import "IjkplayerPlugin.h"
  3. #import "CoolFlutterIjkManager.h"
  4. #import "CoolFlutterIJK.h"
  5. const char* const kOrientationUpdateNotificationName = "io.flutter.plugin.platform.SystemChromeOrientationNotificationName";
  6. const char* const kOrientationUpdateNotificationKey = "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([@"setOrientation" isEqualToString:call.method]){
  95. [self setOrientationWithCall:call];
  96. result(@YES);
  97. } else if([@"unlockOrientation" isEqualToString:call.method]){
  98. [self unlockOrientation];
  99. result(@YES);
  100. } else {
  101. result(FlutterMethodNotImplemented);
  102. }
  103. });
  104. }
  105. - (UIDeviceOrientation) convertIntToOrientation:(int)orientation{
  106. switch (orientation) {
  107. case 0:
  108. return UIDeviceOrientationPortrait;
  109. case 1:
  110. return UIDeviceOrientationLandscapeLeft;
  111. case 2:
  112. return UIDeviceOrientationPortraitUpsideDown;
  113. case 3:
  114. return UIDeviceOrientationLandscapeRight;
  115. default:
  116. return UIDeviceOrientationUnknown;
  117. }
  118. }
  119. - (void) setOrientationWithCall:(FlutterMethodCall *)call{
  120. NSDictionary *dict = [call arguments];
  121. NSArray *orientations = dict[@"orientation"];
  122. UIInterfaceOrientationMask mask = 0;
  123. if (orientations.count == 0) {
  124. mask |= UIInterfaceOrientationMaskAll;
  125. }
  126. for (id number in orientations) {
  127. int value = [number intValue];
  128. UIDeviceOrientation orientation = [self convertIntToOrientation:value];
  129. NSLog(@"orientation = %ld",orientation);
  130. if (value == UIDeviceOrientationPortrait){
  131. mask |= UIInterfaceOrientationMaskPortrait;
  132. }else if (value == UIDeviceOrientationLandscapeLeft) {
  133. mask |= UIInterfaceOrientationMaskLandscapeLeft;
  134. }else if (value == UIDeviceOrientationPortraitUpsideDown) {
  135. mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
  136. }else if (value == UIDeviceOrientationLandscapeRight) {
  137. mask |= UIInterfaceOrientationMaskLandscapeRight;
  138. }
  139. }
  140. if (!mask)
  141. return;
  142. [[NSNotificationCenter defaultCenter] postNotificationName:@(kOrientationUpdateNotificationName)
  143. object:nil
  144. userInfo:@{@(kOrientationUpdateNotificationKey)
  145. :@(mask)}];
  146. }
  147. - (void) unlockOrientation {
  148. if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]){
  149. SEL selector = NSSelectorFromString(@"setOrientation:");
  150. int val = UIDeviceOrientationUnknown;
  151. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
  152. [invocation setSelector:selector];
  153. [invocation setTarget:[UIDevice currentDevice]];
  154. [invocation setArgument:&val atIndex:2];
  155. [invocation invoke];
  156. }
  157. [[NSNotificationCenter defaultCenter] postNotificationName:@(kOrientationUpdateNotificationName)
  158. object:nil
  159. userInfo:@{@(kOrientationUpdateNotificationKey)
  160. :@(UIInterfaceOrientationMaskAll)}];
  161. }
  162. - (int) getSystemVolume{
  163. // AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  164. // CGFloat currentVol = audioSession.outputVolume * 100;
  165. // NSLog(@"system volume = %.0f",currentVol);
  166. // return (int)currentVol;
  167. return (int)([self getVolumeWithVolumeView] * 100);
  168. }
  169. - (float) getVolumeWithVolumeView{
  170. [self initVolumeView];
  171. if(!volumeViewSlider){
  172. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  173. CGFloat currentVol = audioSession.outputVolume * 100;
  174. NSLog(@"system volume = %.0f",currentVol);
  175. return (int)currentVol;
  176. }
  177. return volumeViewSlider.value;
  178. }
  179. -(void)initVolumeView{
  180. if(!volumeView){
  181. volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, 0, 10, 10)];
  182. }
  183. if(!volumeViewSlider){
  184. for (UIView *view in [volumeView subviews]) {
  185. if ([view.class.description isEqualToString:@"MPVolumeSlider"]) {
  186. volumeViewSlider = (UISlider *) view;
  187. break;
  188. }
  189. }
  190. }
  191. UIWindow *window = UIApplication.sharedApplication.keyWindow;
  192. [window addSubview:volumeView];
  193. }
  194. - (void) checkVolumeViewShouldShow{
  195. int count = [manager ijkCount];
  196. if (count>0){
  197. [self initVolumeView];
  198. }else{
  199. [volumeView removeFromSuperview];
  200. volumeView = nil;
  201. }
  202. }
  203. - (void)setSystemVolume:(int)volume {
  204. [self initVolumeView];
  205. float targetVolume = ((float) volume) / 100;
  206. if (targetVolume > 1){
  207. targetVolume = 1;
  208. } else if(targetVolume < 0){
  209. targetVolume = 0;
  210. }
  211. // change system volume, the value is between 0.0f and 1.0f
  212. [volumeViewSlider setValue:targetVolume animated:NO];
  213. // send UI control event to make the change effect right now. 立即生效
  214. [volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
  215. // [volumeView removeFromSuperview];
  216. }
  217. -(void) hideSystemVolumeBar {
  218. }
  219. @end
  220. @implementation FlutterMethodCall (Ijk)
  221. - (int64_t)getId {
  222. return [[self arguments] intValue];
  223. }
  224. - (int64_t)getIdParamFromDict {
  225. return [[self arguments][@"id"] intValue];
  226. }
  227. - (NSString *)getStringParam:(NSString *)key {
  228. return [self arguments][key];
  229. }
  230. @end