Bladeren bron

微调controller的逻辑

cjl_macbook 6 jaren geleden
bovenliggende
commit
6d84c2d9bc
3 gewijzigde bestanden met toevoegingen van 23 en 17 verwijderingen
  1. 2 10
      example/lib/main.dart
  2. 19 4
      lib/src/controller.dart
  3. 2 3
      lib/src/ijk_event_channel.dart

+ 2 - 10
example/lib/main.dart

@@ -173,21 +173,13 @@ class HomePageState extends State<HomePage> {
         FlatButton(
           child: Text("播放"),
           onPressed: () async {
-            var info = await controller?.getVideoInfo();
-            print(info);
             await controller?.play();
-            info = await controller?.getVideoInfo();
-            print(info);
           },
         ),
         FlatButton(
-          child: Text("停"),
+          child: Text("停"),
           onPressed: () async {
-            var info = await controller?.getVideoInfo();
-            print(info);
-            await controller?.pause();
-            info = await controller?.getVideoInfo();
-            print(info);
+            await controller?.stop();
           },
         ),
       ],

+ 19 - 4
lib/src/controller.dart

@@ -11,7 +11,13 @@ class IjkMediaController extends ChangeNotifier {
 
   IJKEventChannel eventChannel;
 
-  bool isPlaying = false;
+  bool _isPlaying = false;
+
+  bool get isPlaying => _isPlaying;
+
+  set isPlaying(bool value) {
+    this._isPlaying = value;
+  }
 
   Future<void> _initIjk() async {
     try {
@@ -80,10 +86,13 @@ class IjkMediaController extends ChangeNotifier {
   }
 
   Future<void> playOrPause() async {
-    if (isPlaying == true) {
-      await _plugin?.play();
-    } else {
+    var playing = isPlaying == true;
+    if (playing) {
       await _plugin?.pause();
+      playing = false;
+    } else {
+      await _plugin?.play();
+      playing = true;
     }
     this.notifyListeners();
   }
@@ -114,6 +123,12 @@ class IjkMediaController extends ChangeNotifier {
       eventChannel.autoPlay(this);
     }
   }
+
+  Future<void> stop() async {
+    await _plugin?.stop();
+    isPlaying = false;
+    this.notifyListeners();
+  }
 }
 
 /// about channel

+ 2 - 3
lib/src/ijk_event_channel.dart

@@ -29,7 +29,7 @@ class IJKEventChannel {
 
   Future<dynamic> handler(MethodCall call) async {
     switch (call.method) {
-      case "finish": // 对应状态变化
+      case "finish": // 播放完毕
         // var index = call.arguments["type"];
         // var type = FinishType.values[index];
         onPlayFinish(getInfo(call));
@@ -45,6 +45,7 @@ class IJKEventChannel {
           "$channelName ${call.method} not implement",
         );
     }
+    return true;
   }
 
   VideoInfo getInfo(MethodCall call) {
@@ -57,12 +58,10 @@ class IJKEventChannel {
   }
 
   void onPlayStateChange(VideoInfo info) {
-    print("onPlayStateChange $info");
     controller.isPlaying = info.isPlaying;
   }
 
   void onPrepare(VideoInfo info) {
-    print("onPrepare $info");
     controller.isPlaying = info.isPlaying;
     _prepareCompleter?.complete();
     _prepareCompleter = null;