Browse Source

update orientation

Caijinglong 6 years ago
parent
commit
d2565091ec

+ 4 - 4
example/ios/Podfile.lock

@@ -9,8 +9,8 @@ PODS:
 
 DEPENDENCIES:
   - Flutter (from `.symlinks/flutter/ios`)
-  - flutter_ijkplayer (from `/Users/caijinglong/Documents/GitHub/flutter_ijkplayer/ios/flutter_ijkplayer.podspec`)
-  - photo_manager (from `/Users/caijinglong/.pub-cache/hosted/pub.flutter-io.cn/photo_manager-0.3.3/ios/photo_manager.podspec`)
+  - flutter_ijkplayer (from `/Users/cai/Documents/GitHub/flutter_ijkplayer/ios/flutter_ijkplayer.podspec`)
+  - photo_manager (from `/Users/cai/.pub-cache/hosted/pub.flutter-io.cn/photo_manager-0.3.3/ios/photo_manager.podspec`)
 
 SPEC REPOS:
   https://github.com/cocoapods/specs.git:
@@ -20,9 +20,9 @@ EXTERNAL SOURCES:
   Flutter:
     :path: ".symlinks/flutter/ios"
   flutter_ijkplayer:
-    :path: "/Users/caijinglong/Documents/GitHub/flutter_ijkplayer/ios/flutter_ijkplayer.podspec"
+    :path: "/Users/cai/Documents/GitHub/flutter_ijkplayer/ios/flutter_ijkplayer.podspec"
   photo_manager:
-    :path: "/Users/caijinglong/.pub-cache/hosted/pub.flutter-io.cn/photo_manager-0.3.3/ios/photo_manager.podspec"
+    :path: "/Users/cai/.pub-cache/hosted/pub.flutter-io.cn/photo_manager-0.3.3/ios/photo_manager.podspec"
 
 SPEC CHECKSUMS:
   Flutter: 58dd7d1b27887414a370fcccb9e645c08ffd7a6a

+ 6 - 15
example/lib/page/full_screen.dart

@@ -170,24 +170,15 @@ class _FullScreen2State extends State<FullScreen2> {
     );
   }
 
-  void setLandScapeLeft() async {
-    await SystemChrome.setPreferredOrientations(
-      [DeviceOrientation.landscapeLeft],
-    );
+  setLandScapeLeft() async {
+    await IjkManager.setLandScapeLeft();
   }
 
-  void portraitUp() async {
-    await SystemChrome.setPreferredOrientations([
-      DeviceOrientation.portraitUp,
-    ]);
-    await SystemChrome.restoreSystemUIOverlays();
+  portraitUp() async {
+    await IjkManager.portraitUp();
   }
 
-  void unlockOrientation() {
-    SystemChrome.setPreferredOrientations([
-      DeviceOrientation.landscapeLeft,
-      DeviceOrientation.landscapeRight,
-      DeviceOrientation.portraitUp,
-    ]);
+  unlockOrientation() async {
+    await IjkManager.unlockOrientation();
   }
 }

+ 16 - 0
ios/Classes/CoolOrientation.h

@@ -0,0 +1,16 @@
+//
+//  CoolOrientation.h
+//  flutter_ijkplayer
+//
+//  Created by Caijinglong on 2019/3/29.
+//
+
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface CoolOrientation : NSObject
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 41 - 0
ios/Classes/CoolOrientation.m

@@ -0,0 +1,41 @@
+//
+//  CoolOrientation.m
+//  flutter_ijkplayer
+//
+//  Created by Caijinglong on 2019/3/29.
+//
+
+#import "CoolOrientation.h"
+
+const char* const kOrientationUpdateNotificationName = "io.flutter.plugin.platform.SystemChromeOrientationNotificationName";
+const char* const kOrientationUpdateNotificationKey = "io.flutter.plugin.platform.SystemChromeOrientationNotificationKey";
+
+@implementation CoolOrientation
+
+- (void)setSystemChromePreferredOrientations:(NSArray*)orientations {
+    UIInterfaceOrientationMask mask = 0;
+    
+    if (orientations.count == 0) {
+        mask |= UIInterfaceOrientationMaskAll;
+    } else {
+        for (NSString* orientation in orientations) {
+            if ([orientation isEqualToString:@"DeviceOrientation.portraitUp"])
+                mask |= UIInterfaceOrientationMaskPortrait;
+            else if ([orientation isEqualToString:@"DeviceOrientation.portraitDown"])
+                mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
+            else if ([orientation isEqualToString:@"DeviceOrientation.landscapeLeft"])
+                mask |= UIInterfaceOrientationMaskLandscapeLeft;
+            else if ([orientation isEqualToString:@"DeviceOrientation.landscapeRight"])
+                mask |= UIInterfaceOrientationMaskLandscapeRight;
+        }
+    }
+    
+    if (!mask)
+        return;
+    [[NSNotificationCenter defaultCenter] postNotificationName:@(kOrientationUpdateNotificationName)
+                                                        object:nil
+                                                      userInfo:@{@(kOrientationUpdateNotificationKey)
+                                                              :@(mask)}];
+}
+
+@end

+ 72 - 0
ios/Classes/IjkplayerPlugin.m

@@ -3,6 +3,9 @@
 #import "CoolFlutterIjkManager.h"
 #import "CoolFlutterIJK.h"
 
+const char* const kOrientationUpdateNotificationName = "io.flutter.plugin.platform.SystemChromeOrientationNotificationName";
+const char* const kOrientationUpdateNotificationKey = "io.flutter.plugin.platform.SystemChromeOrientationNotificationKey";
+
 @interface FlutterMethodCall (Ijk)
 - (int64_t)getId;
 
@@ -103,12 +106,81 @@ static IjkplayerPlugin *__sharedInstance;
         } else if ([@"resetBrightness" isEqualToString:call.method]) {
 //            CGFloat brightness = [UIScreen mainScreen].brightness;
             result(@YES);
+        } else if([@"setOrientation" isEqualToString:call.method]){
+            [self setOrientationWithCall:call];
+            result(@YES);
+        } else if([@"unlockOrientation" isEqualToString:call.method]){
+            [self unlockOrientation];
+            result(@YES);
         } 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",orientation);
+        if (value == UIDeviceOrientationPortrait){
+            mask |= UIInterfaceOrientationMaskPortrait;
+        }else if (value == UIDeviceOrientationLandscapeLeft) {
+            mask |= UIInterfaceOrientationMaskLandscapeLeft;
+        }else if (value == UIDeviceOrientationPortraitUpsideDown) {
+            mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
+        }else if (value == UIDeviceOrientationLandscapeRight) {
+            mask |= UIInterfaceOrientationMaskLandscapeRight;
+        }
+    }
+    
+    if (!mask)
+        return;
+    [[NSNotificationCenter defaultCenter] postNotificationName:@(kOrientationUpdateNotificationName)
+                                                        object:nil
+                                                      userInfo:@{@(kOrientationUpdateNotificationKey)
+                                                               :@(mask)}];
+}
+
+- (void) unlockOrientation {
+    if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]){
+        SEL selector = NSSelectorFromString(@"setOrientation:");
+        int val = UIDeviceOrientationUnknown;
+        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
+        [invocation setSelector:selector];
+        [invocation setTarget:[UIDevice currentDevice]];
+        [invocation setArgument:&val atIndex:2];
+        [invocation invoke];
+    }
+    
+    [[NSNotificationCenter defaultCenter] postNotificationName:@(kOrientationUpdateNotificationName)
+                                                        object:nil
+                                                      userInfo:@{@(kOrientationUpdateNotificationKey)
+                                                                 :@(UIInterfaceOrientationMaskAll)}];
+}
+
+
 - (int) getSystemVolume{
 //    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
 //    CGFloat currentVol = audioSession.outputVolume * 100;

+ 57 - 0
lib/src/engine/manager.dart

@@ -52,4 +52,61 @@ class IjkManager {
   static Future<void> resetBrightness() async {
     await _globalChannel.invokeMethod("resetBrightness");
   }
+
+  static Future<void> _setOrientation(List<DeviceOrientation> list) async {
+    if (Platform.isAndroid) {
+      SystemChrome.setPreferredOrientations(list);
+    } else if (Platform.isIOS) {
+      var orientations = list.map((v) => v.index).toList();
+      if (list.isEmpty) {
+        _globalChannel.invokeMethod("unlockOrientation");
+      } else {
+        _globalChannel.invokeMethod(
+          "setOrientation",
+          {
+            "orientation": orientations,
+          },
+        );
+      }
+    }
+  }
+
+  static setLandScapeLeft() {
+    if (Platform.isAndroid) {
+      SystemChrome.setPreferredOrientations(
+        [DeviceOrientation.landscapeLeft],
+      );
+    } else if (Platform.isIOS) {
+      _setOrientation(
+        [DeviceOrientation.landscapeLeft],
+      );
+    }
+  }
+
+  static portraitUp() async {
+    if (Platform.isAndroid) {
+      await SystemChrome.setPreferredOrientations([
+        DeviceOrientation.portraitUp,
+      ]);
+      await SystemChrome.restoreSystemUIOverlays();
+    } else {
+      _setOrientation(
+        [DeviceOrientation.portraitUp],
+      );
+    }
+  }
+
+  static unlockOrientation() async {
+    if (Platform.isAndroid) {
+      await SystemChrome.setPreferredOrientations([
+        DeviceOrientation.landscapeLeft,
+        DeviceOrientation.landscapeRight,
+        DeviceOrientation.portraitUp,
+      ]);
+      await SystemChrome.restoreSystemUIOverlays();
+    } else if (Platform.isIOS) {
+      await _setOrientation([]);
+      await SystemChrome.restoreSystemUIOverlays();
+    }
+  }
 }