Просмотр исходного кода

锁定屏幕,旋转屏幕的api

Caijinglong 6 лет назад
Родитель
Сommit
f787fca6c9
4 измененных файлов с 113 добавлено и 39 удалено
  1. 4 2
      TODOLIST.md
  2. 2 2
      example/lib/page/full_screen.dart
  3. 50 2
      ios/Classes/IjkplayerPlugin.m
  4. 57 33
      lib/src/engine/manager.dart

+ 4 - 2
TODOLIST.md

@@ -31,8 +31,10 @@
   - [x] 控制亮度
     - [x] 逻辑方法
     - [x] 界面实现
-  - [ ] 屏幕方向锁定 : 这个指当前页面支持什么方向的应用
-  - [ ] 屏幕旋转: 这个指强制当前屏幕旋转至哪个方向
+  - [x] 屏幕方向锁定 : 这个指当前页面支持什么方向的应用
+    - [ ] iPad 无效,暂不知原因
+  - [x] 屏幕旋转: 这个指强制当前屏幕旋转至哪个方向
+    - [ ] iPad 无效,暂不知原因
 - [x] 默认控制器 UI
   - [x] 进度条
   - [x] 播放/暂停按钮

+ 2 - 2
example/lib/page/full_screen.dart

@@ -178,11 +178,11 @@ class _FullScreen2State extends State<FullScreen2> {
   }
 
   setLandScapeLeft() async {
-    await IjkManager.setLandScapeLeft();
+    await IjkManager.setLandScape();
   }
 
   portraitUp() async {
-    await IjkManager.portraitUp();
+    await IjkManager.setPortrait();
   }
 
   unlockOrientation() async {

+ 50 - 2
ios/Classes/IjkplayerPlugin.m

@@ -106,8 +106,11 @@ 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];
+        } 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];
@@ -173,6 +176,51 @@ static IjkplayerPlugin *__sharedInstance;
     }
 }
 
+- (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:)]){

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

@@ -53,48 +53,73 @@ class IjkManager {
     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() {
+  static setLandScape() async {
     if (Platform.isAndroid) {
       SystemChrome.setPreferredOrientations(
         [DeviceOrientation.landscapeLeft],
       );
+      SystemChrome.setEnabledSystemUIOverlays([]);
     } else if (Platform.isIOS) {
-      // OrientationPlugin.forceOrientation(DeviceOrientation.landscapeLeft);
-      _globalChannel.invokeMethod("setOrientation",{"orientation":[DeviceOrientation.landscapeLeft.index]});
+      await setSupportOrientation([
+        DeviceOrientation.landscapeLeft,
+        DeviceOrientation.landscapeRight,
+      ]);
+
+      setCurrentOrientation(DeviceOrientation.landscapeLeft);
     }
   }
 
-  static portraitUp() async {
+  static setPortrait() async {
     if (Platform.isAndroid) {
       await SystemChrome.setPreferredOrientations([
         DeviceOrientation.portraitUp,
       ]);
-      // await SystemChrome.restoreSystemUIOverlays();
-    } else if(Platform.isIOS){
-      // _setOrientation(
-      //   [DeviceOrientation.portraitUp],
-      // );
-      // OrientationPlugin.forceOrientation(null);
-      _globalChannel.invokeMethod("unlockOrientation");
+      SystemChrome.restoreSystemUIOverlays();
+      showSystemOverlay();
+    } else if (Platform.isIOS) {
+      await unlockOrientation();
+      setCurrentOrientation(DeviceOrientation.portraitUp);
+    }
+  }
+
+  static showSystemOverlay() {
+    SystemChrome.setEnabledSystemUIOverlays(const [
+      SystemUiOverlay.top,
+      SystemUiOverlay.bottom,
+    ]);
+  }
+
+  static setSupportOrientation(List<DeviceOrientation> orientations) async {
+    if (orientations.isEmpty) {
+      return;
+    }
+    if (Platform.isAndroid) {
+      await SystemChrome.setPreferredOrientations(orientations);
+      SystemChrome.restoreSystemUIOverlays();
+    } else if (Platform.isIOS) {
+      var orientationIndexList = orientations.map((o) => o.index).toList();
+
+      await _globalChannel.invokeMethod(
+        "setSupportOrientation",
+        <String, dynamic>{
+          "supportOrientation": orientationIndexList,
+        },
+      );
+    }
+  }
+
+  static setCurrentOrientation(DeviceOrientation orientation) async {
+    if (orientation == null) {
+      return;
+    }
+    if (Platform.isAndroid) {
+    } else if (Platform.isIOS) {
+      await _globalChannel.invokeMethod(
+        "setCurrentOrientation",
+        <String, dynamic>{
+          "target": orientation.index,
+        },
+      );
     }
   }
 
@@ -104,12 +129,11 @@ class IjkManager {
         DeviceOrientation.landscapeLeft,
         DeviceOrientation.landscapeRight,
         DeviceOrientation.portraitUp,
+        DeviceOrientation.portraitDown,
       ]);
       await SystemChrome.restoreSystemUIOverlays();
+      showSystemOverlay();
     } else if (Platform.isIOS) {
-      // await _setOrientation([]);
-      // await SystemChrome.restoreSystemUIOverlays();
-      // OrientationPlugin.setPreferredOrientations([]);
       _globalChannel.invokeMethod("unlockOrientation");
     }
   }