IjkplayerPlugin.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. }
  14. + (instancetype)sharedInstance {
  15. return __sharedInstance;
  16. }
  17. - (instancetype)initWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  18. self = [super init];
  19. if (self) {
  20. self.registrar = registrar;
  21. manager = [CoolFlutterIjkManager managerWithRegistrar:registrar];
  22. }
  23. return self;
  24. }
  25. + (instancetype)pluginWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  26. return [[self alloc] initWithRegistrar:registrar];
  27. }
  28. + (void)registerWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  29. FlutterMethodChannel *channel = [FlutterMethodChannel
  30. methodChannelWithName:@"top.kikt/ijkplayer"
  31. binaryMessenger:[registrar messenger]];
  32. IjkplayerPlugin *instance = [IjkplayerPlugin pluginWithRegistrar:registrar];
  33. [registrar addMethodCallDelegate:instance channel:channel];
  34. __sharedInstance = instance;
  35. }
  36. - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
  37. dispatch_queue_t mainQueue = dispatch_get_main_queue();
  38. dispatch_async(mainQueue, ^{
  39. if ([@"create" isEqualToString:call.method]) {
  40. @try {
  41. int64_t id = [self->manager create];
  42. result(@(id));
  43. }
  44. @catch (NSException *exception) {
  45. result([FlutterError errorWithCode:@"1" message:@"创建失败" details:exception]);
  46. }
  47. } else if ([@"dispose" isEqualToString:call.method]) {
  48. NSDictionary *params = [call arguments];
  49. int id = [params[@"id"] intValue];
  50. [self->manager disposeWithId:id];
  51. result(@(YES));
  52. } else if ([@"init" isEqualToString:call.method]) {
  53. [self->manager disposeAll];
  54. result(@YES);
  55. } else if ([@"setSystemVolume" isEqualToString:call.method]) {
  56. NSDictionary *params = [call arguments];
  57. int volume = [params[@"volume"] intValue];
  58. [self setSystemVolume:volume];
  59. result(@YES);
  60. } else if ([@"getSystemVolume" isEqualToString:call.method]) {
  61. int currentVol = [self getSystemVolume];
  62. result(@(currentVol));
  63. } else if ([@"volumeUp" isEqualToString:call.method]) {
  64. int currentVol = [self getSystemVolume];
  65. [self setSystemVolume: currentVol - 10];
  66. currentVol = [self getSystemVolume];
  67. result(@(currentVol));
  68. } else if ([@"volumeDown" isEqualToString:call.method]) {
  69. int currentVol = [self getSystemVolume];
  70. [self setSystemVolume: currentVol + 10];
  71. currentVol = [self getSystemVolume];
  72. result(@(currentVol));
  73. } else {
  74. result(FlutterMethodNotImplemented);
  75. }
  76. });
  77. }
  78. - (int) getSystemVolume{
  79. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  80. CGFloat currentVol = audioSession.outputVolume * 100;
  81. return (int)currentVol;
  82. }
  83. - (void)setSystemVolume:(int)volume {
  84. MPVolumeView *volumeView = [[MPVolumeView alloc] init];
  85. UISlider *volumeViewSlider = nil;
  86. for (UIView *view in [volumeView subviews]) {
  87. if ([view.class.description isEqualToString:@"MPVolumeSlider"]) {
  88. volumeViewSlider = (UISlider *) view;
  89. break;
  90. }
  91. }
  92. float targetVolume = ((float) volume) / 100;
  93. volumeView.frame = CGRectMake(-1000, -1000, 100, 100);
  94. UIWindow *window = UIApplication.sharedApplication.keyWindow;
  95. [window addSubview:volumeView];
  96. if (targetVolume > 1){
  97. targetVolume = 1;
  98. } else if(targetVolume < 0){
  99. targetVolume = 0;
  100. }
  101. // change system volume, the value is between 0.0f and 1.0f
  102. [volumeViewSlider setValue:targetVolume animated:NO];
  103. // send UI control event to make the change effect right now. 立即生效
  104. [volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
  105. [volumeView removeFromSuperview];
  106. }
  107. @end
  108. @implementation FlutterMethodCall (Ijk)
  109. - (int64_t)getId {
  110. return [[self arguments] intValue];
  111. }
  112. - (int64_t)getIdParamFromDict {
  113. return [[self arguments][@"id"] intValue];
  114. }
  115. - (NSString *)getStringParam:(NSString *)key {
  116. return [self arguments][key];
  117. }
  118. @end