瀏覽代碼

修复了iOS部分的setDataSource await 无效的问题
setDataSource的autoplay属性 和play pause方法相互逻辑间的bug

Caijinglong 6 年之前
父節點
當前提交
f2ce0b9901

+ 2 - 2
example/lib/page/developing/develop_prepare_page.dart

@@ -41,8 +41,8 @@ class _ForPreparePageState extends State<ForPreparePage> {
   Future initPlayer() async {
     await controller.setDataSource(
       DataSource.asset("assets/sample1.mp4"),
-      autoPlay: false,
+      autoPlay: true,
     );
-    controller.play();
+    controller.pause();
   }
 }

+ 20 - 14
ios/Classes/CoolFlutterIJK.m

@@ -4,9 +4,7 @@
 
 #import "CoolFlutterIJK.h"
 #import "CoolVideoInfo.h"
-#import "CoolIjkNotifyChannel.h"
-#import <IJKMediaFramework/IJKMediaFramework.h>
-#import <IJKMediaFramework/IJKMediaPlayer.h>
+#import "CoolFlutterResult.h"
 #import <AVFoundation/AVFoundation.h>
 #import <libkern/OSAtomic.h>
 
@@ -22,6 +20,7 @@
     FlutterMethodChannel *channel;
     CoolIjkNotifyChannel *notifyChannel;
     int degree;
+    CoolFlutterResult *prepareResult;
 }
 
 - (instancetype)initWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
@@ -67,8 +66,7 @@
             NSDictionary *params = call.arguments;
             NSString *uri = params[@"uri"];
             NSDictionary *headers = params[@"headers"];
-            [self setDataSourceWithUri:uri headers:headers];
-            result(@(YES));
+            [self setDataSourceWithUri:uri headers:headers result:[CoolFlutterResult resultWithResult:result]];
         }
         @catch (NSException *exception) {
             NSLog(@"Exception occurred: %@, %@", exception, [exception userInfo]);
@@ -80,8 +78,7 @@
             NSString *name = params[@"name"];
             NSString *pkg = params[@"package"];
             IJKFFMoviePlayerController *playerController = [self createControllerWithAssetName:name pkg:pkg];
-            [self setDataSourceWithController:playerController];
-            result(@(YES));
+            [self setDataSourceWithController:playerController result:[CoolFlutterResult resultWithResult:result]];
         }
         @catch (NSException *exception) {
             NSLog(@"Exception occurred: %@, %@", exception, [exception userInfo]);
@@ -91,8 +88,7 @@
         NSDictionary *params = call.arguments;
         NSString *path = params[@"path"];
         IJKFFMoviePlayerController *playerController = [self createControllerWithPath:path];
-        [self setDataSourceWithController:playerController];
-        result(@(YES));
+        [self setDataSourceWithController:playerController result:[CoolFlutterResult resultWithResult:result]];
     } else if ([@"seekTo" isEqualToString:call.method]) {
         NSDictionary *params = call.arguments;
         double target = [params[@"target"] doubleValue];
@@ -150,10 +146,10 @@
     }
 }
 
-- (void)setDataSourceWithController:(IJKFFMoviePlayerController *)ctl {
+- (void)setDataSourceWithController:(IJKFFMoviePlayerController *)ctl result:(CoolFlutterResult *)result {
     if (ctl) {
         controller = ctl;
-        [self prepare];
+        [self prepare:result];
     }
 }
 
@@ -221,7 +217,7 @@
     return options;
 }
 
-- (void)setDataSourceWithUri:(NSString *)uri headers:(NSDictionary *)headers {
+- (void)setDataSourceWithUri:(NSString *)uri headers:(NSDictionary *)headers result:(CoolFlutterResult *)result {
     IJKFFOptions *options = [self createOption];
     if (headers) {
         NSMutableString *headerString = [NSMutableString new];
@@ -234,7 +230,7 @@
     }
     controller = [[IJKFFMoviePlayerController alloc] initWithContentURLString:uri withOptions:options];
 
-    [self prepare];
+    [self prepare:result];
 }
 
 - (void)setDegree:(int)d {
@@ -242,7 +238,8 @@
 }
 
 
-- (void)prepare {
+- (void)prepare:(CoolFlutterResult *)result {
+    prepareResult = result;
     [controller prepareToPlay];
     if (displayLink) {
         displayLink.paused = YES;
@@ -258,6 +255,15 @@
     notifyChannel.infoDelegate = self;
 }
 
+- (void)onLoadStateChange {
+//    IJKMPMovieLoadState loadState = controller.loadState;
+    if (prepareResult) {
+        [prepareResult replyResult:@YES];
+    }
+    prepareResult = nil;
+}
+
+
 - (IJKFFMoviePlayerController *)createControllerWithAssetName:(NSString *)assetName pkg:(NSString *)pkg {
     NSString *asset;
     if (!pkg) {

+ 20 - 0
ios/Classes/CoolFlutterResult.h

@@ -0,0 +1,20 @@
+//
+// Created by Caijinglong on 2019-06-12.
+//
+
+
+#import <Flutter/Flutter.h>
+
+@interface CoolFlutterResult : NSObject
+
+@property(nonatomic, strong) FlutterResult result;
+
+@property(nonatomic, assign) BOOL isReply;
+
+- (instancetype)initWithResult:(FlutterResult)result;
+
++ (instancetype)resultWithResult:(FlutterResult)result;
+
+- (void)replyResult:(id _Nullable)result;
+
+@end

+ 33 - 0
ios/Classes/CoolFlutterResult.m

@@ -0,0 +1,33 @@
+//
+// Created by Caijinglong on 2019-06-12.
+//
+
+#import "CoolFlutterResult.h"
+
+@implementation CoolFlutterResult {
+
+}
+- (instancetype)initWithResult:(FlutterResult)result {
+    self = [super init];
+    if (self) {
+        self.result = result;
+        self.isReply = NO;
+    }
+
+    return self;
+}
+
++ (instancetype)resultWithResult:(FlutterResult)result {
+    return [[self alloc] initWithResult:result];
+}
+
+- (void)replyResult:(id _Nullable)result {
+    if (self.isReply) {
+        return;
+    }
+    self.isReply = YES;
+    self.result(result);
+}
+
+
+@end

+ 2 - 0
ios/Classes/CoolIjkNotifyChannel.h

@@ -12,6 +12,8 @@
 - (CoolVideoInfo *)getInfo;
 
 - (void)setDegree:(int)newDegree;
+
+- (void)onLoadStateChange;
 @end
 
 @interface CoolIjkNotifyChannel : NSObject

+ 2 - 3
ios/Classes/CoolIjkNotifyChannel.m

@@ -122,9 +122,8 @@
 }
 
 - (void)loadStateDidChange:(NSNotification *)notification {
-    NSLog(@"load state change, state = %d", _controller.loadState);
-    // todo 添加回调逻辑通知返回加载结果, 确定是否可播放
-//    [channel invokeMethod:@"loadStateChange" arguments:[self getInfo]];
+    NSLog(@"load state change, state = %lu", (unsigned long)_controller.loadState);
+    [self.infoDelegate onLoadStateChange];
 }
 
 - (void)movieRotationChange:(NSNotification *)notification {