Flutter使用Android原生播放器详解
作者:白玉梁 时间:2023-04-11 20:23:00
接上篇:播放器-IOS(Swift)篇
安卓端原生播放器的接入思路与ios基本一致,所以本篇就不废话了,直接上代码:
创建插件VideoViewPlugin实现FlutterPlugin:
package io.flutter.plugins.videoplayer;
import android.util.Log;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
public class VideoViewPlugin implements FlutterPlugin, ActivityAware {
private final static String TAG = "VideoViewPlugin";
FlutterPluginBinding fpBinding;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
fpBinding = binding;
Log.e(TAG, "onAttachedToEngine");
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
Log.e(TAG, "onDetachedFromEngine");
}
@Override
public void onAttachedToActivity(@NonNull ActivityPluginBinding apBinding) {
fpBinding.getPlatformViewRegistry().registerViewFactory("plugins.my_video_player/view", new VideoViewFactory(fpBinding, apBinding));
Log.e(TAG, "onAttachedToActivity");
}
@Override
public void onDetachedFromActivityForConfigChanges() {
Log.e(TAG, "onDetachedFromActivityForConfigChanges");
}
@Override
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
Log.e(TAG, "onReattachedToActivityForConfigChanges");
}
@Override
public void onDetachedFromActivity() {
Log.e(TAG, "onDetachedFromActivity");
}
}
由于引用视频播放器时需要用到Activity的context,所以实现了ActivityAware接口,在onAttachedToActivity方法中注册PlatformViewFactory!
创建VideoViewFactory实现PlatformViewFactory:
package io.flutter.plugins.videoplayer;
import android.content.Context;
import io.flutter.Log;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;
public class VideoViewFactory extends PlatformViewFactory {
private final static String TAG = "VideoViewFactory";
private final FlutterPlugin.FlutterPluginBinding fpBinding;
private final ActivityPluginBinding apBinding;
public VideoViewFactory(FlutterPlugin.FlutterPluginBinding fpBinding, ActivityPluginBinding apBinding) {
super(StandardMessageCodec.INSTANCE);
Log.e(TAG, "VideoViewFactory");
this.fpBinding = fpBinding;
this.apBinding = apBinding;
}
@Override
public PlatformView create(Context context, int viewId, Object args) {
Log.e(TAG, "PlatformView-create:" + args.toString());
return new VideoViewPlayer(args.toString(), fpBinding, apBinding);
}
}
创建VideoViewPlayer,实现PlatformView和MethodChannel.MethodCallHandler:
package io.flutter.plugins.videoplayer;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.th.kjjl_flutter.R;
import com.videoplayer.player.VideoView;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;
public class VideoViewPlayer implements PlatformView, MethodChannel.MethodCallHandler {
private final static String TAG = "VideoPlayerView";
Activity context;
private VideoView videoView;
private MethodChannel methodChannel;
VideoViewPlayer(String viewId, FlutterPlugin.FlutterPluginBinding fpBinding, ActivityPluginBinding apBinding) {
this.context = apBinding.getActivity();
videoView = (VideoView) LayoutInflater.from(context).inflate(R.layout.video_player, null);
methodChannel = new MethodChannel(fpBinding.getBinaryMessenger(), "my_video_player_" + viewId);
methodChannel.setMethodCallHandler(this);
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
Log.e(TAG, "call.method>>" + call.method);
switch (call.method) {
case "setUrl":
String url = call.arguments.toString();
videoView.initVideoController(context);
videoView.setUrl(url);
break;
case "start":
videoView.start();
break;
case "pause":
videoView.pause();
break;
case "release":
videoView.pause();
videoView.release();
break;
case "stopFullScreen":
videoView.stopFullScreen();
break;
default:
result.notImplemented();
}
}
@Nullable
@Override
public View getView() {
return videoView;
}
@Override
public void dispose() {
Log.e(TAG, "dispose>>");
videoView.pause();
videoView.release();
methodChannel.setMethodCallHandler(null);
methodChannel = null;
}
}
其中的VideoView即引用的第三方播放器库,你可以根据自己情况,使用常见的安卓端开源播放器如GSY,饺子,DKPlayer等!
video_player.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.videoplayer.player.VideoView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在MainActivity中注册插件:
class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
flutterEngine!!.plugins.add(VideoViewPlugin())
}
}
注意:不要在GeneratedPluginRegistrant中去注册插件包括IOS,这个类由系统自动生成其它第三方插件注册代码!安卓端在MainActivity中注册,IOS端在AppDelegate中注册!
flutter中的引用以及通信方法,上一篇已经写了,本篇就不再重复了!需要注意的是,插件名,插件id,methodChannel等,安卓,IOS和Flutter三端一定要一致!
来源:https://blog.csdn.net/baiyuliang2013/article/details/129228342
标签:Flutter,播放器,Android,原生
0
投稿
猜你喜欢
SpringBoot实现本地文件存储及预览过程
2022-07-31 05:30:58
Mybatis中注解@MapKey的使用详解
2023-11-24 21:43:35
Android自定义控件实现支付宝记账饼图
2022-04-19 13:27:02
C#值类型、引用类型中的Equals和==的区别浅析
2022-09-05 23:04:24
一篇文章带你入门Java Script
2023-09-12 06:09:24
基于C#方法重载的总结详解
2022-07-29 13:19:46
Java 阻塞队列和线程池原理分析
2022-04-19 11:57:45
Java如何实现Word文档分栏效果
2023-09-11 00:28:53
教你快速搭建sona服务及idea使用sona的方法
2023-11-20 05:22:53
C#更改tabControl选项卡颜色的方法
2022-09-07 19:30:14
详解android webView独立进程通讯方式
2022-03-04 13:51:18
关于Mybatis-Plus Wrapper是否应该出现在Servcie类中
2023-11-28 22:04:56
springboot项目如何防止XSS攻击
2021-10-17 10:03:02
Flutter的键值存储数据库使用示例详解
2023-10-15 02:13:15
Mybatis自关联查询一对多查询的实现示例
2021-08-21 04:06:16
Spring中多配置文件及引用其他bean的方式
2023-07-01 17:31:03
JAVA过滤标签实现将html内容转换为文本的方法示例
2022-10-16 21:02:40
IDEA全量替换一次性解决旧项目并将所有文件换行符改为LF问题
2022-09-17 18:44:32
Java对象和Json文本转换工具类的实现
2022-10-22 19:09:23
C#设计模式实现之生成器模式和责任链模式
2023-12-19 21:57:51