| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- #import <AVKit/AVKit.h>
- #import "IjkplayerPlugin.h"
- #import "CoolFlutterIjkManager.h"
- #import "CoolFlutterIJK.h"
- #import "FlutterViewController+CoolStatusBarHidden.h"
- NSString *flutterOrientationNotifyName = @"io.flutter.plugin.platform.SystemChromeOrientationNotificationName";
- const NSString *flutterOrientationNotifyKey = @"io.flutter.plugin.platform.SystemChromeOrientationNotificationKey";
- @interface FlutterMethodCall (Ijk)
- - (int64_t)getId;
- - (int64_t)getIdParamFromDict;
- - (NSString *)getStringParam:(NSString *)key;
- @end
- static IjkplayerPlugin *__sharedInstance;
- @implementation IjkplayerPlugin {
- CoolFlutterIjkManager *manager;
- MPVolumeView *volumeView;
- UISlider *volumeViewSlider;
- }
- + (instancetype)sharedInstance {
- return __sharedInstance;
- }
- - (instancetype)initWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
- self = [super init];
- if (self) {
- self.registrar = registrar;
- manager = [CoolFlutterIjkManager managerWithRegistrar:registrar];
- }
-
- return self;
- }
- + (instancetype)pluginWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
- return [[self alloc] initWithRegistrar:registrar];
- }
- + (void)registerWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
- FlutterMethodChannel *channel = [FlutterMethodChannel
- methodChannelWithName:@"top.kikt/ijkplayer"
- binaryMessenger:[registrar messenger]];
- IjkplayerPlugin *instance = [IjkplayerPlugin pluginWithRegistrar:registrar];
- [registrar addMethodCallDelegate:instance channel:channel];
- __sharedInstance = instance;
- }
- - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
-
- dispatch_queue_t mainQueue = dispatch_get_main_queue();
- // __weak typeof(&*self) weakSelf = self;
- dispatch_async(mainQueue, ^{
- if ([@"create" isEqualToString:call.method]) {
- @try {
- int64_t id = [self->manager createWithCall:call];
- result(@(id));
- }
- @catch (NSException *exception) {
- result([FlutterError errorWithCode:@"1" message:@"创建失败" details:exception]);
- }
- [self checkVolumeViewShouldShow];
- } else if ([@"dispose" isEqualToString:call.method]) {
- NSDictionary *params = [call arguments];
- int id = [params[@"id"] intValue];
- [self->manager disposeWithId:id];
- [self checkVolumeViewShouldShow];
- result(@(YES));
- } else if ([@"init" isEqualToString:call.method]) {
- [self->manager disposeAll];
- [self checkVolumeViewShouldShow];
- result(@YES);
- } else if ([@"setSystemVolume" isEqualToString:call.method]) {
- NSDictionary *params = [call arguments];
- int volume = [params[@"volume"] intValue];
- [self setSystemVolume:volume];
- result(@YES);
- } else if ([@"getSystemVolume" isEqualToString:call.method]) {
- int currentVol = [self getSystemVolume];
- result(@(currentVol));
- } else if ([@"volumeUp" isEqualToString:call.method]) {
- int currentVol = [self getSystemVolume];
- [self setSystemVolume: currentVol + 3];
- currentVol = [self getSystemVolume];
- result(@(currentVol));
- } else if ([@"volumeDown" isEqualToString:call.method]) {
- int currentVol = [self getSystemVolume];
- [self setSystemVolume: currentVol - 3];
- currentVol = [self getSystemVolume];
- result(@(currentVol));
- } else if ([@"hideSystemVolumeBar" isEqualToString:call.method]) {
- [self hideSystemVolumeBar];
- result(@YES);
- } else if ([@"setSystemBrightness" isEqualToString:call.method]) {
- NSDictionary *params = [call arguments];
- CGFloat target = [params[@"brightness"] floatValue];
- [[UIScreen mainScreen] setBrightness:target];
- result(@YES);
- } else if ([@"getSystemBrightness" isEqualToString:call.method]) {
- CGFloat brightness = [UIScreen mainScreen].brightness;
- result(@(brightness));
- } else if ([@"resetBrightness" isEqualToString:call.method]) {
- // CGFloat brightness = [UIScreen mainScreen].brightness;
- result(@YES);
- } else if ([@"setSupportOrientation" isEqualToString:call.method]) {
- [self setSupportOrientationWithCall:call];
- result(@YES);
- } else if([@"setCurrentOrientation" isEqualToString:call.method]){
- [self setCurrentOrientationWithCall:call];
- result(@YES);
- } else if([@"unlockOrientation" isEqualToString:call.method]){
- [self unlockOrientation];
- result(@YES);
- } else if([@"showStatusBar" isEqualToString:call.method]) {
- BOOL showStatusBar = [call.arguments boolValue];
- [self setStatusBarToShow:showStatusBar];
- } else {
- result(FlutterMethodNotImplemented);
- }
- });
- }
- - (UIDeviceOrientation) convertIntToOrientation:(int)orientation{
- switch (orientation) {
- case 0:
- return UIDeviceOrientationPortrait;
- case 1:
- return UIDeviceOrientationLandscapeLeft;
- case 2:
- return UIDeviceOrientationPortraitUpsideDown;
- case 3:
- return UIDeviceOrientationLandscapeRight;
- default:
- return UIDeviceOrientationUnknown;
- }
- }
- - (void) setOrientationWithCall:(FlutterMethodCall *)call{
- NSDictionary *dict = [call arguments];
- NSArray *orientations = dict[@"orientation"];
- UIInterfaceOrientationMask mask = 0;
- if (orientations.count == 0) {
- mask |= UIInterfaceOrientationMaskAll;
- }
- for (id number in orientations) {
- int value = [number intValue];
- UIDeviceOrientation orientation = [self convertIntToOrientation:value];
- NSLog(@"orientation = %ld",(long)orientation);
- if (orientation == UIDeviceOrientationPortrait){
- mask |= UIInterfaceOrientationMaskPortrait;
- }else if (orientation == UIDeviceOrientationLandscapeLeft) {
- mask |= UIInterfaceOrientationMaskLandscapeLeft;
- }else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
- mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
- }else if (orientation == UIDeviceOrientationLandscapeRight) {
- mask |= UIInterfaceOrientationMaskLandscapeRight;
- }
- }
-
- [[NSNotificationCenter defaultCenter] postNotificationName:flutterOrientationNotifyName
- object:nil
- userInfo:@{flutterOrientationNotifyKey
- :@(mask)}];
-
- if(orientations.count != 0 && [[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]){
- SEL selector = NSSelectorFromString(@"setOrientation:");
- int value = [orientations[0] intValue];
- UIDeviceOrientation orientation = [self convertIntToOrientation:value];
- int val = orientation;
- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
- [invocation setSelector:selector];
- [invocation setTarget:[UIDevice currentDevice]];
- [invocation setArgument:&val atIndex:2];
- [invocation invoke];
- }
- }
- - (void) setCurrentOrientationWithCall:(FlutterMethodCall *)call {
- if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]){
- SEL selector = NSSelectorFromString(@"setOrientation:");
- NSDictionary *dict = [call arguments];
- int target = [dict[@"target"] intValue];
- UIDeviceOrientation orientation = [self convertIntToOrientation:target];
- int val = orientation;
- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
- [invocation setSelector:selector];
- [invocation setTarget:[UIDevice currentDevice]];
- [invocation setArgument:&val atIndex:2];
- [invocation invoke];
-
- NSLog(@"target orientation = %d", val);
- }
- }
- - (void) setSupportOrientationWithCall:(FlutterMethodCall *)call {
- NSDictionary *dict = [call arguments];
- NSArray *orientations = dict[@"supportOrientation"];
- UIInterfaceOrientationMask mask = 0;
- if (orientations.count == 0) {
- mask |= UIInterfaceOrientationMaskAll;
- }
- for (id number in orientations) {
- int value = [number intValue];
- UIDeviceOrientation orientation = [self convertIntToOrientation:value];
- NSLog(@"orientation = %ld",orientation);
- if (orientation == UIDeviceOrientationPortrait){
- mask |= UIInterfaceOrientationMaskPortrait;
- }else if (orientation == UIDeviceOrientationLandscapeLeft) {
- mask |= UIInterfaceOrientationMaskLandscapeLeft;
- }else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
- mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
- }else if (orientation == UIDeviceOrientationLandscapeRight) {
- mask |= UIInterfaceOrientationMaskLandscapeRight;
- }
- }
-
- [[NSNotificationCenter defaultCenter] postNotificationName:flutterOrientationNotifyName
- object:nil
- userInfo:@{flutterOrientationNotifyKey
- :@(mask)}];
- }
- - (void) unlockOrientation {
- UIDevice *device = [UIDevice currentDevice];
- if([device respondsToSelector:@selector(setOrientation:)]){
- SEL selector = NSSelectorFromString(@"setOrientation:");
- int val = device.orientation;
- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
- [invocation setSelector:selector];
- [invocation setTarget:[UIDevice currentDevice]];
- [invocation setArgument:&val atIndex:2];
- [invocation invoke];
- }
-
- [[NSNotificationCenter defaultCenter] postNotificationName:flutterOrientationNotifyName
- object:nil
- userInfo:@{flutterOrientationNotifyKey
- :@(UIInterfaceOrientationMaskAll)}];
- }
- - (int) getSystemVolume{
- // AVAudioSession *audioSession = [AVAudioSession sharedInstance];
- // CGFloat currentVol = audioSession.outputVolume * 100;
- // NSLog(@"system volume = %.0f",currentVol);
- // return (int)currentVol;
- return (int)([self getVolumeWithVolumeView] * 100);
- }
- - (float) getVolumeWithVolumeView{
- [self initVolumeView];
- if(!volumeViewSlider){
- AVAudioSession *audioSession = [AVAudioSession sharedInstance];
- CGFloat currentVol = audioSession.outputVolume * 100;
- NSLog(@"system volume = %.0f",currentVol);
- return (int)currentVol;
- }
- return volumeViewSlider.value;
- }
- -(void)initVolumeView{
- if(!volumeView){
- volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, 0, 10, 10)];
- }
- if(!volumeViewSlider){
- for (UIView *view in [volumeView subviews]) {
- if ([view.class.description isEqualToString:@"MPVolumeSlider"]) {
- volumeViewSlider = (UISlider *) view;
- break;
- }
- }
- }
-
- UIWindow *window = UIApplication.sharedApplication.keyWindow;
- [window addSubview:volumeView];
- }
- - (void) checkVolumeViewShouldShow{
- int count = [manager ijkCount];
- if (count>0){
- [self initVolumeView];
- }else{
- [volumeView removeFromSuperview];
- volumeView = nil;
- }
- }
- - (void)setSystemVolume:(int)volume {
- [self initVolumeView];
- float targetVolume = ((float) volume) / 100;
- if (targetVolume > 1){
- targetVolume = 1;
- } else if(targetVolume < 0){
- targetVolume = 0;
- }
-
- // change system volume, the value is between 0.0f and 1.0f
- [volumeViewSlider setValue:targetVolume animated:NO];
- // send UI control event to make the change effect right now. 立即生效
- [volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
-
- // [volumeView removeFromSuperview];
- }
- -(void)hideSystemVolumeBar {
-
- }
- -(void)setStatusBarToShow:(BOOL)show{
- UIViewController *ctl = [self getCurrentUIViewController];
- if([ctl isKindOfClass:[FlutterViewController class] ]){
- FlutterViewController *fCtl = (FlutterViewController*)ctl;
- if(show){
- [fCtl showStatusBar];
- }else{
- [fCtl hideStatusBar];
- }
- }
- }
- -(UIViewController*) getCurrentUIViewController{
- UIViewController *result = nil;
- UIWindow *window = [[UIApplication sharedApplication] keyWindow];
-
- if(window.windowLevel != UIWindowLevelNormal){
- NSArray *windows =[[UIApplication sharedApplication ]windows];
- for(UIWindow *tmp in windows){
- window = tmp;
- break;
- }
- }
-
- result = window.rootViewController;
-
- while (result.presentedViewController) {
- result = result.presentedViewController;
- }
-
- if([result isKindOfClass:[UITabBarController class]]){
- result = [(UITabBarController*)result selectedViewController];
- }
-
- if([result isKindOfClass:[UINavigationController class]]){
- result = [(UINavigationController*) result visibleViewController];
- }
-
- return result;
- }
- @end
- @implementation FlutterMethodCall (Ijk)
- - (int64_t)getId {
- return [[self arguments] intValue];
- }
- - (int64_t)getIdParamFromDict {
- return [[self arguments][@"id"] intValue];
- }
- - (NSString *)getStringParam:(NSString *)key {
- return [self arguments][key];
- }
- @end
|