IjkplayerPlugin.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. } else if ([@"dispose" isEqualToString:call.method]) {
  50. NSDictionary *params = [call arguments];
  51. int id = [params[@"id"] intValue];
  52. [self->manager disposeWithId:id];
  53. result(@(YES));
  54. } else if ([@"init" isEqualToString:call.method]) {
  55. [self->manager disposeAll];
  56. result(@YES);
  57. } else if ([@"setSystemVolume" isEqualToString:call.method]) {
  58. NSDictionary *params = [call arguments];
  59. int volume = [params[@"volume"] intValue];
  60. [self setSystemVolume:volume];
  61. result(@YES);
  62. } else if ([@"getSystemVolume" isEqualToString:call.method]) {
  63. int currentVol = [self getSystemVolume];
  64. result(@(currentVol));
  65. } else if ([@"volumeUp" isEqualToString:call.method]) {
  66. int currentVol = [self getSystemVolume];
  67. [self setSystemVolume: currentVol + 3];
  68. currentVol = [self getSystemVolume];
  69. result(@(currentVol));
  70. } else if ([@"volumeDown" isEqualToString:call.method]) {
  71. int currentVol = [self getSystemVolume];
  72. [self setSystemVolume: currentVol - 3];
  73. currentVol = [self getSystemVolume];
  74. result(@(currentVol));
  75. } else if ([@"hideSystemVolumeBar" isEqualToString:call.method]) {
  76. [self hideSystemVolumeBar];
  77. result(@YES);
  78. } else {
  79. result(FlutterMethodNotImplemented);
  80. }
  81. });
  82. }
  83. - (int) getSystemVolume{
  84. // AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  85. // CGFloat currentVol = audioSession.outputVolume * 100;
  86. // NSLog(@"system volume = %.0f",currentVol);
  87. // return (int)currentVol;
  88. return (int)([self getVolumeWithVolumeView] * 100);
  89. }
  90. - (float) getVolumeWithVolumeView{
  91. [self initVolumeView];
  92. if(!volumeViewSlider){
  93. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  94. CGFloat currentVol = audioSession.outputVolume * 100;
  95. NSLog(@"system volume = %.0f",currentVol);
  96. return (int)currentVol;
  97. }
  98. return volumeViewSlider.value;
  99. }
  100. -(void)initVolumeView{
  101. if(!volumeView){
  102. volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, 0, 10, 10)];
  103. }
  104. if(!volumeViewSlider){
  105. for (UIView *view in [volumeView subviews]) {
  106. if ([view.class.description isEqualToString:@"MPVolumeSlider"]) {
  107. volumeViewSlider = (UISlider *) view;
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. - (void)setSystemVolume:(int)volume {
  114. [self initVolumeView];
  115. float targetVolume = ((float) volume) / 100;
  116. if(volumeView && !volumeView.superview){
  117. UIWindow *window = UIApplication.sharedApplication.keyWindow;
  118. [window addSubview:volumeView];
  119. }
  120. if (targetVolume > 1){
  121. targetVolume = 1;
  122. } else if(targetVolume < 0){
  123. targetVolume = 0;
  124. }
  125. // change system volume, the value is between 0.0f and 1.0f
  126. [volumeViewSlider setValue:targetVolume animated:NO];
  127. // send UI control event to make the change effect right now. 立即生效
  128. [volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
  129. // [volumeView removeFromSuperview];
  130. }
  131. -(void) hideSystemVolumeBar {
  132. if(volumeView && volumeView.superview) {
  133. [volumeView removeFromSuperview];
  134. volumeView = nil;
  135. }
  136. }
  137. @end
  138. @implementation FlutterMethodCall (Ijk)
  139. - (int64_t)getId {
  140. return [[self arguments] intValue];
  141. }
  142. - (int64_t)getIdParamFromDict {
  143. return [[self arguments][@"id"] intValue];
  144. }
  145. - (NSString *)getStringParam:(NSString *)key {
  146. return [self arguments][key];
  147. }
  148. @end