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

微调android部分代码结构

Caijinglong 6 лет назад
Родитель
Сommit
2d45a349e0

+ 35 - 4
android/src/main/java/com/example/ijkplayer/Ijk.kt

@@ -9,7 +9,7 @@ import io.flutter.plugin.common.PluginRegistry
 import tv.danmaku.ijk.media.player.IjkMediaPlayer
 import tv.danmaku.ijk.media.player.TextureMediaPlayer
 
-class Ijk(registry: PluginRegistry.Registrar) : MethodChannel.MethodCallHandler {
+class Ijk(private val registry: PluginRegistry.Registrar) : MethodChannel.MethodCallHandler {
 
     private val textureEntry = registry.textures().createSurfaceTexture()
     val id: Long
@@ -28,9 +28,6 @@ class Ijk(registry: PluginRegistry.Registrar) : MethodChannel.MethodCallHandler
 
     override fun onMethodCall(call: MethodCall?, result: MethodChannel.Result?) {
         when (call?.method) {
-            "dispose" -> {
-                IjkplayerPlugin.manager.dispose(id)
-            }
             "setDataSource" -> {
                 val uri = call.argument<String>("uri")!!
                 setUri(uri) { throwable ->
@@ -42,6 +39,18 @@ class Ijk(registry: PluginRegistry.Registrar) : MethodChannel.MethodCallHandler
                     }
                 }
             }
+            "setAssetDataSource" -> {
+
+                val uri = call.argument<String>("uri")!!
+                setUri(uri) { throwable ->
+                    if (throwable == null) {
+                        result?.success(throwable)
+                    } else {
+                        throwable.printStackTrace()
+                        result?.error("2", "加载失败", throwable)
+                    }
+                }
+            }
             "play" -> {
                 play()
                 result?.success(1)
@@ -71,6 +80,28 @@ class Ijk(registry: PluginRegistry.Registrar) : MethodChannel.MethodCallHandler
         }
     }
 
+    private fun setAssetUri(name: String, `package`: String?, callback: (Throwable?) -> Unit) {
+        try {
+            ijkPlayer.setOnPreparedListener {
+                callback(null)
+            }
+            val asset =
+                    if (`package` == null) {
+                        registry.lookupKeyForAsset(name)
+                    } else {
+                        registry.lookupKeyForAsset(name, `package`)
+                    }
+
+            // 设置资产系的datasource
+
+
+            ijkPlayer.prepareAsync()
+        } catch (e: Exception) {
+            e.printStackTrace()
+            callback(e)
+        }
+    }
+
     fun dispose() {
         mediaPlayer.stop()
         mediaPlayer.release()

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

@@ -15,7 +15,7 @@ class IjkManager(private val registrar: PluginRegistry.Registrar) {
         return ijk
     }
 
-    fun findIJK(id: Long): Ijk? {
+    private fun findIJK(id: Long): Ijk? {
         return ijkList.find { it.id == id }
     }
 

+ 4 - 1
android/src/main/java/com/example/ijkplayer/IjkplayerPlugin.kt

@@ -26,7 +26,10 @@ class IjkplayerPlugin(private val registrar: Registrar) : MethodCallHandler {
                     result.error("1", "创建失败", e)
                 }
             }
-
+            "dispose" -> {
+                val id = call.argument<Int>("id")!!.toLong()
+                manager.dispose(id)
+            }
             else -> result.notImplemented()
         }
     }

+ 42 - 5
example/lib/main.dart

@@ -1,6 +1,7 @@
 import 'package:flutter/material.dart';
 import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';
 import 'package:photo/photo.dart';
+import 'package:photo_manager/photo_manager.dart';
 
 void main() => runApp(MyApp());
 
@@ -67,8 +68,8 @@ class HomePageState extends State<HomePage> {
         child: Icon(Icons.play_arrow),
         onPressed: () async {
           await controller.setDataSource(
-            // 'https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4',
-            'rtmp://172.16.100.245/live1',
+            'https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4',
+            // 'rtmp://172.16.100.245/live1',
             // 'https://www.sample-videos.com/video123/flv/720/big_buck_bunny_720p_10mb.flv',
             // 'http://184.72.239.149/vod/smil:BigBuckBunny.smil/playlist.m3u8',
             // "file:///sdcard/Download/Sample1.mp4",
@@ -81,12 +82,48 @@ class HomePageState extends State<HomePage> {
   }
 
   void _pickVideo() async {
-    var list = await PhotoPicker.pickAsset(
+    List<AssetEntity> imgList = await PhotoPicker.pickAsset(
+      // BuildContext required
       context: context,
+
+      /// The following are optional parameters.
+      themeColor: Colors.green,
+      // the title color and bottom color
+      padding: 1.0,
+      // item padding
+      dividerColor: Colors.grey,
+      // divider color
+      disableColor: Colors.grey.shade300,
+      // the check box disable color
+      itemRadio: 0.88,
+      // the content item radio
+      maxSelected: 8,
+      // max picker image count
+      // provider: I18nProvider.english,
+      provider: I18nProvider.chinese,
+      // i18n provider ,default is chinese. , you can custom I18nProvider or use ENProvider()
+      rowCount: 3,
+      // item row count
+      textColor: Colors.white,
+      // text color
+      thumbSize: 160,
+      // preview thumb size , default is 64
+      sortDelegate: SortDelegate.common,
+      // default is common ,or you make custom delegate to sort your gallery
+      checkBoxBuilderDelegate: DefaultCheckBoxBuilderDelegate(
+        activeColor: Colors.white,
+        unselectedColor: Colors.white,
+      ),
+      // default is DefaultCheckBoxBuilderDelegate ,or you make custom delegate to create checkbox
+
+      badgeDelegate: const DurationBadgeDelegate(),
+      // badgeDelegate to show badge widget
+
       pickType: PickType.onlyVideo,
     );
-    if (list != null && list.isNotEmpty) {
-      var asset = list[0];
+
+    if (imgList != null && imgList.isNotEmpty) {
+      var asset = imgList[0];
       var fileUri = (await asset.file).uri;
       playUri(fileUri.toString());
     }

+ 1 - 1
example/pubspec.lock

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

+ 26 - 9
lib/src/controller.dart

@@ -9,7 +9,7 @@ class IjkMediaController extends ChangeNotifier {
 
   bool get isInit => textureId == null;
 
-  Future _initIjk() async {
+  Future<void> _initIjk() async {
     try {
       var id = await createIjk();
       this.textureId = id;
@@ -27,7 +27,7 @@ class IjkMediaController extends ChangeNotifier {
     super.dispose();
   }
 
-  Future setDataSource(String url) async {
+  Future<void> setDataSource(String url) async {
     if (this.textureId != null) {
       await _plugin?.dispose();
     }
@@ -36,7 +36,16 @@ class IjkMediaController extends ChangeNotifier {
     this.notifyListeners();
   }
 
-  Future play() async {
+  Future<void> setAssetDataSource(String name, {String package}) async {
+    if (this.textureId != null) {
+      await _plugin?.dispose();
+    }
+    await _initIjk();
+    await _plugin?.setAssetDataSource(name, package);
+    this.notifyListeners();
+  }
+
+  Future<void> play() async {
     await _plugin?.play();
     this.notifyListeners();
   }
@@ -56,24 +65,32 @@ class _IjkPlugin {
 
   _IjkPlugin(this.textureId);
 
-  Future dispose() async {
-    channel.invokeMethod("dispose");
+  Future<void> dispose() async {
+    _globalChannel.invokeMethod("dispose", {"id": textureId});
   }
 
-  Future play() async {
+  Future<void> play() async {
     await channel.invokeMethod("play");
   }
 
-  Future pause() async {
+  Future<void> pause() async {
     channel.invokeMethod("pause");
   }
 
-  Future stop() async {
+  Future<void> stop() async {
     channel.invokeMethod("stop");
   }
 
-  Future setDataSource({String uri}) async {
+  Future<void> setDataSource({String uri}) async {
     print("id = $textureId uri = $uri");
     channel.invokeMethod("setDataSource", {"uri": uri});
   }
+
+  Future<void> setAssetDataSource(String name, String package) async {
+    print("id = $textureId asset name = $name package = $package");
+    channel.invokeMethod("setAssetDataSource", <String, dynamic>{
+      "name": name,
+      "package": package,
+    });
+  }
 }