Browse Source

seek getInfo

Caijinglong 6 years ago
parent
commit
3a879287bc

+ 2 - 2
android/build.gradle

@@ -1,4 +1,4 @@
-group 'com.example.ijkplayer'
+group 'top.kikt.ijkplayer'
 version '1.0-SNAPSHOT'
 version '1.0-SNAPSHOT'
 
 
 buildscript {
 buildscript {
@@ -8,7 +8,7 @@ buildscript {
     }
     }
 
 
     dependencies {
     dependencies {
-        classpath 'com.android.tools.build:gradle:3.2.1'
+        classpath 'com.android.tools.build:gradle:3.3.1'
     }
     }
 }
 }
 
 

+ 1 - 1
android/src/main/AndroidManifest.xml

@@ -1,5 +1,5 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-  package="com.example.ijkplayer">
+  package="top.kikt.ijkplayer">
 
 
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

+ 1 - 1
android/src/main/java/com/example/ijkplayer/AssetDataSource.kt → android/src/main/java/top/kikt/ijkplayer/AssetDataSource.kt

@@ -1,4 +1,4 @@
-package com.example.ijkplayer
+package top.kikt.ijkplayer
 
 
 import android.content.res.AssetFileDescriptor
 import android.content.res.AssetFileDescriptor
 import tv.danmaku.ijk.media.player.misc.IMediaDataSource
 import tv.danmaku.ijk.media.player.misc.IMediaDataSource

+ 26 - 6
android/src/main/java/com/example/ijkplayer/Ijk.kt → android/src/main/java/top/kikt/ijkplayer/Ijk.kt

@@ -1,4 +1,4 @@
-package com.example.ijkplayer
+package top.kikt.ijkplayer
 
 
 /// create 2019/3/7 by cai
 /// create 2019/3/7 by cai
 
 
@@ -7,6 +7,7 @@ import android.util.Base64
 import io.flutter.plugin.common.MethodCall
 import io.flutter.plugin.common.MethodCall
 import io.flutter.plugin.common.MethodChannel
 import io.flutter.plugin.common.MethodChannel
 import io.flutter.plugin.common.PluginRegistry
 import io.flutter.plugin.common.PluginRegistry
+import top.kikt.ijkplayer.entity.Info
 import tv.danmaku.ijk.media.player.IjkMediaPlayer
 import tv.danmaku.ijk.media.player.IjkMediaPlayer
 import tv.danmaku.ijk.media.player.TextureMediaPlayer
 import tv.danmaku.ijk.media.player.TextureMediaPlayer
 import java.io.File
 import java.io.File
@@ -69,12 +70,35 @@ class Ijk(private val registry: PluginRegistry.Registrar) : MethodChannel.Method
             "stop" -> {
             "stop" -> {
                 stop()
                 stop()
             }
             }
+            "getInfo" -> {
+                val info = getInfo()
+                result?.success(info.toMap())
+            }
+            "seekTo" -> {
+                val target = call.argument<Double>("target")
+                if (target != null) {
+                    seekTo((target * 1000).toLong())
+                }
+            }
             else -> {
             else -> {
                 result?.notImplemented()
                 result?.notImplemented()
             }
             }
         }
         }
     }
     }
 
 
+    private fun getInfo(): Info {
+        val duration = ijkPlayer.duration
+        val currentPosition = ijkPlayer.currentPosition
+        val width = ijkPlayer.videoWidth
+        val height = ijkPlayer.videoHeight
+        return Info(
+                duration = duration.toDouble() / 1000,
+                currentPosition = currentPosition.toDouble() / 1000,
+                width = width,
+                height = height
+        )
+    }
+
     private fun handleSetUriResult(throwable: Throwable?, result: MethodChannel.Result?) {
     private fun handleSetUriResult(throwable: Throwable?, result: MethodChannel.Result?) {
         if (throwable == null) {
         if (throwable == null) {
             result?.success(null)
             result?.success(null)
@@ -132,7 +156,7 @@ class Ijk(private val registry: PluginRegistry.Registrar) : MethodChannel.Method
 
 
     private fun play() {
     private fun play() {
         try {
         try {
-            mediaPlayer.start()
+            ijkPlayer.start()
         } catch (e: Exception) {
         } catch (e: Exception) {
             e.printStackTrace()
             e.printStackTrace()
         }
         }
@@ -150,9 +174,5 @@ class Ijk(private val registry: PluginRegistry.Registrar) : MethodChannel.Method
         mediaPlayer.seekTo(msec)
         mediaPlayer.seekTo(msec)
     }
     }
 
 
-    fun getDuration(): Long {
-        return mediaPlayer.duration
-    }
-
 }
 }
 
 

+ 1 - 1
android/src/main/java/com/example/ijkplayer/IjkManager.kt → android/src/main/java/top/kikt/ijkplayer/IjkManager.kt

@@ -1,4 +1,4 @@
-package com.example.ijkplayer
+package top.kikt.ijkplayer
 
 
 /// create 2019/3/7 by cai
 /// create 2019/3/7 by cai
 
 

+ 1 - 1
android/src/main/java/com/example/ijkplayer/IjkplayerPlugin.kt → android/src/main/java/top/kikt/ijkplayer/IjkplayerPlugin.kt

@@ -1,4 +1,4 @@
-package com.example.ijkplayer
+package top.kikt.ijkplayer
 
 
 import io.flutter.plugin.common.MethodCall
 import io.flutter.plugin.common.MethodCall
 import io.flutter.plugin.common.MethodChannel
 import io.flutter.plugin.common.MethodChannel

+ 20 - 0
android/src/main/java/top/kikt/ijkplayer/entity/Info.kt

@@ -0,0 +1,20 @@
+package top.kikt.ijkplayer.entity
+
+/// create 2019/3/15 by cai
+data class Info(
+        val duration: Double,
+        val currentPosition: Double,
+        val width: Int,
+        val height: Int
+) {
+
+    fun toMap(): Map<String, Any> {
+        val map = HashMap<String, Any>()
+        map["duration"] = duration
+        map["currentPosition"] = currentPosition
+        map["width"] = width
+        map["height"] = height
+        return map
+    }
+
+}

+ 1 - 1
example/ios/Podfile.lock

@@ -26,7 +26,7 @@ EXTERNAL SOURCES:
 
 
 SPEC CHECKSUMS:
 SPEC CHECKSUMS:
   Flutter: 58dd7d1b27887414a370fcccb9e645c08ffd7a6a
   Flutter: 58dd7d1b27887414a370fcccb9e645c08ffd7a6a
-  flutter_ijkplayer: 68fdddc1bec770c1a40b00f07687a594ffeecd47
+  flutter_ijkplayer: 8e0761718c08711f50a86aaa3ca0ce0655ea3462
   FlutterIJK: 593a48952fb5022fb5c0bd8bd26fd1a11418ce5c
   FlutterIJK: 593a48952fb5022fb5c0bd8bd26fd1a11418ce5c
   photo_manager: d47ddf6cb25cbfa837dc334540eb9a99b208e191
   photo_manager: d47ddf6cb25cbfa837dc334540eb9a99b208e191
 
 

+ 5 - 0
example/lib/main.dart

@@ -143,6 +143,11 @@ class HomePageState extends State<HomePage> {
       onPressed: () async {
       onPressed: () async {
         await controller.setAssetDataSource("assets/sample1.mp4");
         await controller.setAssetDataSource("assets/sample1.mp4");
         controller.play();
         controller.play();
+
+        Future.delayed(Duration(seconds: 2), () async {
+          var info = await controller.getVideoInfo();
+          print("info = $info");
+        });
       },
       },
     );
     );
   }
   }

+ 26 - 0
ios/Classes/FlutterIJK.m

@@ -3,6 +3,7 @@
 //
 //
 
 
 #import "FlutterIJK.h"
 #import "FlutterIJK.h"
+#import "KKVideoInfo.h"
 #import <IJKMediaFramework/IJKMediaFramework.h>
 #import <IJKMediaFramework/IJKMediaFramework.h>
 #import <IJKMediaFramework/IJKMediaPlayer.h>
 #import <IJKMediaFramework/IJKMediaPlayer.h>
 #import <AVFoundation/AVFoundation.h>
 #import <AVFoundation/AVFoundation.h>
@@ -72,11 +73,19 @@
         NSDictionary *params = call.arguments;
         NSDictionary *params = call.arguments;
         NSString *path = params[@"path"];
         NSString *path = params[@"path"];
         IJKFFMoviePlayerController *playerController = [self createControllerWithPath:path];
         IJKFFMoviePlayerController *playerController = [self createControllerWithPath:path];
+    } else if ([@"seekTo" isEqualToString:call.method]) {
+        NSDictionary *params = call.arguments;
+        double target = [params[@"target"] doubleValue];
+        [self seekTo:target];
+    } else if ([@"getInfo" isEqualToString:call.method]) {
+        KKVideoInfo *info = [self getInfo];
+        result([info toMap]);
     } else {
     } else {
         result(FlutterMethodNotImplemented);
         result(FlutterMethodNotImplemented);
     }
     }
 }
 }
 
 
+
 + (instancetype)ijkWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
 + (instancetype)ijkWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
     return [[self alloc] initWithRegistrar:registrar];
     return [[self alloc] initWithRegistrar:registrar];
 }
 }
@@ -159,6 +168,10 @@
     return [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:options];
     return [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:options];
 }
 }
 
 
+- (void)seekTo:(double)target {
+    [controller setCurrentPlaybackTime:target];
+}
+
 - (void)onDisplayLink:(CADisplayLink *)link {
 - (void)onDisplayLink:(CADisplayLink *)link {
     [textures textureFrameAvailable:textureId];
     [textures textureFrameAvailable:textureId];
 }
 }
@@ -177,5 +190,18 @@
     return NULL;
     return NULL;
 }
 }
 
 
+- (KKVideoInfo *)getInfo {
+    KKVideoInfo *info = [KKVideoInfo new];
+
+    CGSize size = [controller naturalSize];
+    NSTimeInterval duration = [controller duration];
+    NSTimeInterval currentPlaybackTime = [controller currentPlaybackTime];
+
+    info.size = size;
+    info.duration = duration;
+    info.currentPosition = currentPlaybackTime;
+
+    return info;
+}
 
 
 @end
 @end

+ 18 - 0
ios/Classes/KKVideoInfo.h

@@ -0,0 +1,18 @@
+//
+// Created by Caijinglong on 2019-03-15.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface KKVideoInfo : NSObject
+
+@property(nonatomic, assign) NSTimeInterval duration;
+
+@property(nonatomic, assign) NSTimeInterval currentPosition;
+
+@property(nonatomic, assign) CGSize size;
+
+- (NSDictionary *)toMap;
+
+@end

+ 21 - 0
ios/Classes/KKVideoInfo.m

@@ -0,0 +1,21 @@
+//
+// Created by Caijinglong on 2019-03-15.
+//
+
+#import "KKVideoInfo.h"
+
+
+@implementation KKVideoInfo {
+
+}
+
+- (NSDictionary *)toMap {
+    NSMutableDictionary *dict = [NSMutableDictionary new];
+    dict[@"width"] = @((int) self.size.width);
+    dict[@"height"] = @((int) self.size.height);
+    dict[@"duration"] = @(self.duration);
+    dict[@"currentPosition"] = @(self.currentPosition);
+    return dict;
+}
+
+@end

+ 24 - 0
lib/src/controller.dart

@@ -49,6 +49,15 @@ class IjkMediaController extends ChangeNotifier {
     await _plugin?.play();
     await _plugin?.play();
     this.notifyListeners();
     this.notifyListeners();
   }
   }
+
+  Future<void> seekTo(double target) async {
+    await _plugin?.seekTo(target);
+  }
+
+  Future<VideoInfo> getVideoInfo() async {
+    Map<String, dynamic> result = await _plugin.getInfo();
+    return VideoInfo.fromMap(result);
+  }
 }
 }
 
 
 const MethodChannel _globalChannel = MethodChannel("top.kikt/ijkplayer");
 const MethodChannel _globalChannel = MethodChannel("top.kikt/ijkplayer");
@@ -108,4 +117,19 @@ class _IjkPlugin {
     // todo
     // todo
     print("id = $textureId file path = $path");
     print("id = $textureId file path = $path");
   }
   }
+
+  Future<Map<String, dynamic>> getInfo() async {
+    var map = await channel.invokeMethod("getInfo");
+    if (map == null) {
+      return null;
+    } else {
+      return map.cast<String, dynamic>();
+    }
+  }
+
+  Future<void> seekTo(double target) async {
+    await channel.invokeMethod("seekTo", <String, dynamic>{
+      "target": target,
+    });
+  }
 }
 }

+ 1 - 0
lib/src/ijkplayer.dart

@@ -3,6 +3,7 @@ import 'dart:io';
 
 
 import 'package:flutter/material.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
 import 'package:flutter/services.dart';
+import 'package:flutter_ijkplayer/src/video_info.dart';
 import './error.dart';
 import './error.dart';
 
 
 part './controller.dart';
 part './controller.dart';

+ 30 - 0
lib/src/video_info.dart

@@ -0,0 +1,30 @@
+import 'dart:convert';
+
+class VideoInfo {
+  int width;
+  int height;
+  double duration;
+  double currentPosition;
+
+  Map<String, dynamic> _map;
+
+  double get radio => width / height;
+
+  double get progress => currentPosition / duration;
+
+  VideoInfo.fromMap(Map<String, dynamic> map) {
+    if (map == null) {
+      return;
+    }
+    this._map = map;
+    this.width = map["width"];
+    this.height = map["height"];
+    this.duration = map["duration"];
+    this.currentPosition = map["currentPosition"];
+  }
+
+  @override
+  String toString() {
+    return json.encode(_map);
+  }
+}

+ 1 - 1
pubspec.lock

@@ -85,7 +85,7 @@ packages:
       name: source_span
       name: source_span
       url: "https://pub.flutter-io.cn"
       url: "https://pub.flutter-io.cn"
     source: hosted
     source: hosted
-    version: "1.5.3"
+    version: "1.5.4"
   stack_trace:
   stack_trace:
     dependency: transitive
     dependency: transitive
     description:
     description:

+ 1 - 1
pubspec.yaml

@@ -25,7 +25,7 @@ flutter:
   # be modified. They are used by the tooling to maintain consistency when
   # be modified. They are used by the tooling to maintain consistency when
   # adding or updating assets for this project.
   # adding or updating assets for this project.
   plugin:
   plugin:
-    androidPackage: com.example.ijkplayer
+    androidPackage: top.kikt.ijkplayer
     pluginClass: IjkplayerPlugin
     pluginClass: IjkplayerPlugin
   # To add assets to your plugin package, add an assets section, like this:
   # To add assets to your plugin package, add an assets section, like this:
   # assets:
   # assets: