IjkplayerPlugin.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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([@"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 (orientation == UIDeviceOrientationPortrait){
  131. mask |= UIInterfaceOrientationMaskPortrait;
  132. }else if (orientation == UIDeviceOrientationLandscapeLeft) {
  133. mask |= UIInterfaceOrientationMaskLandscapeLeft;
  134. }else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
  135. mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
  136. }else if (orientation == UIDeviceOrientationLandscapeRight) {
  137. mask |= UIInterfaceOrientationMaskLandscapeRight;
  138. }
  139. }
  140. [[NSNotificationCenter defaultCenter] postNotificationName:flutterOrientationNotifyName
  141. object:nil
  142. userInfo:@{flutterOrientationNotifyKey
  143. :@(mask)}];
  144. if(orientations.count != 0 && [[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]){
  145. SEL selector = NSSelectorFromString(@"setOrientation:");
  146. int value = [orientations[0] intValue];
  147. UIDeviceOrientation orientation = [self convertIntToOrientation:value];
  148. int val = orientation;
  149. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
  150. [invocation setSelector:selector];
  151. [invocation setTarget:[UIDevice currentDevice]];
  152. [invocation setArgument:&val atIndex:2];
  153. [invocation invoke];
  154. }
  155. }
  156. - (void) unlockOrientation {
  157. UIDevice *device = [UIDevice currentDevice];
  158. if([device respondsToSelector:@selector(setOrientation:)]){
  159. SEL selector = NSSelectorFromString(@"setOrientation:");
  160. int val = device.orientation;
  161. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
  162. [invocation setSelector:selector];
  163. [invocation setTarget:[UIDevice currentDevice]];
  164. [invocation setArgument:&val atIndex:2];
  165. [invocation invoke];
  166. }
  167. [[NSNotificationCenter defaultCenter] postNotificationName:flutterOrientationNotifyName
  168. object:nil
  169. userInfo:@{flutterOrientationNotifyKey
  170. :@(UIInterfaceOrientationMaskAll)}];
  171. }
  172. - (int) getSystemVolume{
  173. // AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  174. // CGFloat currentVol = audioSession.outputVolume * 100;
  175. // NSLog(@"system volume = %.0f",currentVol);
  176. // return (int)currentVol;
  177. return (int)([self getVolumeWithVolumeView] * 100);
  178. }
  179. - (float) getVolumeWithVolumeView{
  180. [self initVolumeView];
  181. if(!volumeViewSlider){
  182. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  183. CGFloat currentVol = audioSession.outputVolume * 100;
  184. NSLog(@"system volume = %.0f",currentVol);
  185. return (int)currentVol;
  186. }
  187. return volumeViewSlider.value;
  188. }
  189. -(void)initVolumeView{
  190. if(!volumeView){
  191. volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, 0, 10, 10)];
  192. }
  193. if(!volumeViewSlider){
  194. for (UIView *view in [volumeView subviews]) {
  195. if ([view.class.description isEqualToString:@"MPVolumeSlider"]) {
  196. volumeViewSlider = (UISlider *) view;
  197. break;
  198. }
  199. }
  200. }
  201. UIWindow *window = UIApplication.sharedApplication.keyWindow;
  202. [window addSubview:volumeView];
  203. }
  204. - (void) checkVolumeViewShouldShow{
  205. int count = [manager ijkCount];
  206. if (count>0){
  207. [self initVolumeView];
  208. }else{
  209. [volumeView removeFromSuperview];
  210. volumeView = nil;
  211. }
  212. }
  213. - (void)setSystemVolume:(int)volume {
  214. [self initVolumeView];
  215. float targetVolume = ((float) volume) / 100;
  216. if (targetVolume > 1){
  217. targetVolume = 1;
  218. } else if(targetVolume < 0){
  219. targetVolume = 0;
  220. }
  221. // change system volume, the value is between 0.0f and 1.0f
  222. [volumeViewSlider setValue:targetVolume animated:NO];
  223. // send UI control event to make the change effect right now. 立即生效
  224. [volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
  225. // [volumeView removeFromSuperview];
  226. }
  227. -(void) hideSystemVolumeBar {
  228. }
  229. @end
  230. @implementation FlutterMethodCall (Ijk)
  231. - (int64_t)getId {
  232. return [[self arguments] intValue];
  233. }
  234. - (int64_t)getIdParamFromDict {
  235. return [[self arguments][@"id"] intValue];
  236. }
  237. - (NSString *)getStringParam:(NSString *)key {
  238. return [self arguments][key];
  239. }
  240. @end