IjkplayerPlugin.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #import <AVKit/AVKit.h>
  2. #import "IjkplayerPlugin.h"
  3. #import "CoolFlutterIjkManager.h"
  4. #import "CoolFlutterIJK.h"
  5. @interface FlutterMethodCall (Ijk)
  6. - (int64_t)getId;
  7. - (int64_t)getIdParamFromDict;
  8. - (NSString *)getStringParam:(NSString *)key;
  9. @end
  10. static IjkplayerPlugin *__sharedInstance;
  11. @implementation IjkplayerPlugin {
  12. CoolFlutterIjkManager *manager;
  13. MPVolumeView *volumeView;
  14. UISlider *volumeViewSlider;
  15. }
  16. + (instancetype)sharedInstance {
  17. return __sharedInstance;
  18. }
  19. - (instancetype)initWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  20. self = [super init];
  21. if (self) {
  22. self.registrar = registrar;
  23. manager = [CoolFlutterIjkManager managerWithRegistrar:registrar];
  24. }
  25. return self;
  26. }
  27. + (instancetype)pluginWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  28. return [[self alloc] initWithRegistrar:registrar];
  29. }
  30. + (void)registerWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  31. FlutterMethodChannel *channel = [FlutterMethodChannel
  32. methodChannelWithName:@"top.kikt/ijkplayer"
  33. binaryMessenger:[registrar messenger]];
  34. IjkplayerPlugin *instance = [IjkplayerPlugin pluginWithRegistrar:registrar];
  35. [registrar addMethodCallDelegate:instance channel:channel];
  36. __sharedInstance = instance;
  37. }
  38. - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
  39. dispatch_queue_t mainQueue = dispatch_get_main_queue();
  40. dispatch_async(mainQueue, ^{
  41. if ([@"create" isEqualToString:call.method]) {
  42. @try {
  43. int64_t id = [self->manager create];
  44. result(@(id));
  45. }
  46. @catch (NSException *exception) {
  47. result([FlutterError errorWithCode:@"1" message:@"创建失败" details:exception]);
  48. }
  49. [self checkVolumeViewShouldShow];
  50. } else if ([@"dispose" isEqualToString:call.method]) {
  51. NSDictionary *params = [call arguments];
  52. int id = [params[@"id"] intValue];
  53. [self->manager disposeWithId:id];
  54. [self checkVolumeViewShouldShow];
  55. result(@(YES));
  56. } else if ([@"init" isEqualToString:call.method]) {
  57. [self->manager disposeAll];
  58. [self checkVolumeViewShouldShow];
  59. result(@YES);
  60. } else if ([@"setSystemVolume" isEqualToString:call.method]) {
  61. NSDictionary *params = [call arguments];
  62. int volume = [params[@"volume"] intValue];
  63. [self setSystemVolume:volume];
  64. result(@YES);
  65. } else if ([@"getSystemVolume" isEqualToString:call.method]) {
  66. int currentVol = [self getSystemVolume];
  67. result(@(currentVol));
  68. } else if ([@"volumeUp" isEqualToString:call.method]) {
  69. int currentVol = [self getSystemVolume];
  70. [self setSystemVolume: currentVol + 3];
  71. currentVol = [self getSystemVolume];
  72. result(@(currentVol));
  73. } else if ([@"volumeDown" isEqualToString:call.method]) {
  74. int currentVol = [self getSystemVolume];
  75. [self setSystemVolume: currentVol - 3];
  76. currentVol = [self getSystemVolume];
  77. result(@(currentVol));
  78. } else if ([@"hideSystemVolumeBar" isEqualToString:call.method]) {
  79. [self hideSystemVolumeBar];
  80. result(@YES);
  81. } else if ([@"setSystemBrightness" isEqualToString:call.method]) {
  82. NSDictionary *params = [call arguments];
  83. CGFloat target = [params[@"brightness"] floatValue];
  84. [[UIScreen mainScreen] setBrightness:target];
  85. result(@YES);
  86. } else if ([@"getSystemBrightness" isEqualToString:call.method]) {
  87. CGFloat brightness = [UIScreen mainScreen].brightness;
  88. result(@(brightness));
  89. } else if ([@"resetBrightness" isEqualToString:call.method]) {
  90. // CGFloat brightness = [UIScreen mainScreen].brightness;
  91. result(@YES);
  92. } else {
  93. result(FlutterMethodNotImplemented);
  94. }
  95. });
  96. }
  97. - (int) getSystemVolume{
  98. // AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  99. // CGFloat currentVol = audioSession.outputVolume * 100;
  100. // NSLog(@"system volume = %.0f",currentVol);
  101. // return (int)currentVol;
  102. return (int)([self getVolumeWithVolumeView] * 100);
  103. }
  104. - (float) getVolumeWithVolumeView{
  105. [self initVolumeView];
  106. if(!volumeViewSlider){
  107. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  108. CGFloat currentVol = audioSession.outputVolume * 100;
  109. NSLog(@"system volume = %.0f",currentVol);
  110. return (int)currentVol;
  111. }
  112. return volumeViewSlider.value;
  113. }
  114. -(void)initVolumeView{
  115. if(!volumeView){
  116. volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, 0, 10, 10)];
  117. }
  118. if(!volumeViewSlider){
  119. for (UIView *view in [volumeView subviews]) {
  120. if ([view.class.description isEqualToString:@"MPVolumeSlider"]) {
  121. volumeViewSlider = (UISlider *) view;
  122. break;
  123. }
  124. }
  125. }
  126. UIWindow *window = UIApplication.sharedApplication.keyWindow;
  127. [window addSubview:volumeView];
  128. }
  129. - (void) checkVolumeViewShouldShow{
  130. int count = [manager ijkCount];
  131. if (count>0){
  132. [self initVolumeView];
  133. }else{
  134. [volumeView removeFromSuperview];
  135. volumeView = nil;
  136. }
  137. }
  138. - (void)setSystemVolume:(int)volume {
  139. [self initVolumeView];
  140. float targetVolume = ((float) volume) / 100;
  141. if (targetVolume > 1){
  142. targetVolume = 1;
  143. } else if(targetVolume < 0){
  144. targetVolume = 0;
  145. }
  146. // change system volume, the value is between 0.0f and 1.0f
  147. [volumeViewSlider setValue:targetVolume animated:NO];
  148. // send UI control event to make the change effect right now. 立即生效
  149. [volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
  150. // [volumeView removeFromSuperview];
  151. }
  152. -(void) hideSystemVolumeBar {
  153. }
  154. @end
  155. @implementation FlutterMethodCall (Ijk)
  156. - (int64_t)getId {
  157. return [[self arguments] intValue];
  158. }
  159. - (int64_t)getIdParamFromDict {
  160. return [[self arguments][@"id"] intValue];
  161. }
  162. - (NSString *)getStringParam:(NSString *)key {
  163. return [self arguments][key];
  164. }
  165. @end