Flutter3引用原生播放器-Android篇

news/2024/4/20 14:58:07/文章来源:https://blog.csdn.net/baiyuliang2013/article/details/129228342

接上篇:Flutter3引用原生播放器-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;@Overridepublic void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {fpBinding = binding;Log.e(TAG, "onAttachedToEngine");}@Overridepublic void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {Log.e(TAG, "onDetachedFromEngine");}@Overridepublic void onAttachedToActivity(@NonNull ActivityPluginBinding apBinding) {fpBinding.getPlatformViewRegistry().registerViewFactory("plugins.my_video_player/view", new VideoViewFactory(fpBinding, apBinding));Log.e(TAG, "onAttachedToActivity");}@Overridepublic void onDetachedFromActivityForConfigChanges() {Log.e(TAG, "onDetachedFromActivityForConfigChanges");}@Overridepublic void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {Log.e(TAG, "onReattachedToActivityForConfigChanges");}@Overridepublic 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;}@Overridepublic 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);}@Overridepublic 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@Overridepublic View getView() {return videoView;}@Overridepublic 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三端一定要一致!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_74301.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Java学习】初识Java

JavaSEJava初识1. Java简介2.Java环境的安装与配置3. 开发第一个Java程序Java初识 学前疑问&#xff1a;&#xff08;带着疑问去学习&#xff0c;在学习中自行探索答案&#xff09; Java是什么&#xff1f;能做什么&#xff1f;发展前景如何&#xff1f;需要学习哪些内容&…

mysql数据库表的创建与查看

mysql数据库表的创建与查看 一、mysql查看 查看所有数据库 show databases切换数据库 use 数据库名查看该数据库下所有的表名 show tables查看表的结构 desc 表名二、mysq创建 创建数据库 create database 数据库名;创建数据库设置编码 drop database if EXISTS dbname; creat…

终端软件架构说

目录 零&#xff1a;前言 一&#xff0c;基于服务的架构 二&#xff0c;基于多进程多线程的架构 三&#xff0c;以数据为中心的架构 四&#xff0c;类Android的分层架构设计 五&#xff0c;总结 零&#xff1a;前言 谈到架构&#xff0c;可能大家的第一感觉是信息系统的…

redis数据结构的底层实现

文章目录一.引言二.redis的特点三.Redis的数据结构a.字符串b.hashc.listd.sete.zset(有序集合)一.引言 redis是一个开源的使用C语言编写、支持网络、可基于内存亦可持久化的日志型、key-value的NoSQL数据库。 通常使用redis作为缓存中间件来降低数据库的压力&#xff0c;除此…

STC单片机启动看门狗定时器介绍和使用

STC单片机启动看门狗定时器介绍 ✨这里以STC8系列为例。 📑看门狗复位(WDT_CONTR) WDT_FLAG:看门狗溢出标志 看门狗发生溢出时,硬件自动将此位置 1,需要软件清零。EN_WDT:看门狗使能位 0:对单片机无影响 1:启动看门狗定时器。 注意:看门狗定时器可使用软件方式启动,…

CXL互联标准简介及相关资料

毕设是实现CXL的type3扩展内存设备&#xff0c;因为CXL技术非常新&#xff0c;2019年推出&#xff0c;本专栏也是记录CXL的相关知识与一些浅薄的理解 文章目录CXL出现的背景CXL是什么其他互联总线介绍CXL胜出的原因CXL内容简介包含三种协议 CXL.io/cache/memory支持三种设备类型…

Reids实战—黑马点评(三)秒杀篇

Reids实战—黑马点评&#xff08;三&#xff09;秒杀篇 来自黑马的redis课程的笔记 【黑马程序员Redis入门到实战教程&#xff0c;深度透析redis底层原理redis分布式锁企业解决方案黑马点评实战项目】 目录Reids实战—黑马点评&#xff08;三&#xff09;秒杀篇一、全局唯一I…

DevOps实战50讲-(1)彻底理解DevOps

持续坚持原创输出&#xff0c;点击蓝字关注我吧软件质量保障:所寫即所思&#xff5c;一个阿里质量人对测试的所感所悟。浅谈软件开发流程软件开发流程是从需求分析、设计、编码、测试到上线等一系列环节的步骤和活动。通常来说&#xff0c;软件开发流程可以分为以下几个阶段&am…

Python 多进程多线程线程池进程池协程

目录 一、线程与进程很简单的介绍 1.1 线程与进程的区别 二、多进程Process 2.1 多进程与多线程的区别 2.2 多进程为啥要使用队列 2.3 控制进程运行顺序 2.3.1 join &#xff0c; 2.3.1 daemon 守护进程 2.4 进程id 2.5 进程 存活状态is_alive() 2.5 实现自定义多…

计算机图形学:liang算法和Cyrus-Beck算法

其中Cyrus-Beck算法呢&#xff0c;是计算一根直线一个多边形的交线段&#xff1b;liang算法是Cyrus的一个特例&#xff0c;即多边形刚好是矩形&#xff1b;先看看Cyrus算法的思路【从别的博客找的图片】&#xff1a;这很容易理解&#xff0c;点积>0时就可能中内部嘛&#xf…

Pyinstaller 打包EXE(七) 百篇文章学PyQT

本文章是百篇文章学PyQT6的第七篇&#xff0c;本文讲述如何使用Pyinstaller打包UI界面和代码&#xff0c;将程序打包成EXE来更为方便的进行部署&#xff0c;在写博客和学习的过程中会遇到很多问题&#xff0c;例如&#xff1a;PyQT6在网上很多博客都是PyQT5、或者PyQT4大部分都…

Amazon S3 服务15岁生日快乐!

2021年3月14日&#xff0c;作为第一个发布的服务&#xff0c;Amazon S3 服务15周岁啦&#xff01;在中国文化里&#xff0c;15岁是个临界点&#xff0c;是从“舞勺之年”到“舞象之年”的过渡。相信对于 Amazon S3 和其他的云服务15周岁也将是其迎接更加美好未来的全新起点。亚…

java面试题-JVM内存结构

整体结构&#xff1a;1.说说JVM内存整体的结构&#xff1f;线程私有还是共享的&#xff1f;JVM&#xff08;Java Virtual Machine&#xff09;内存可以分为以下几个部分&#xff1a;程序计数器&#xff08;Program Counter Register&#xff09;&#xff1a;是线程私有的&#…

深入浅出解析ChatGPT引领的科技浪潮【AI行研商业价值分析】

Rocky Ding写在前面 【AI行研&商业价值分析】栏目专注于分享AI行业中最新热点/风口的思考与判断。也欢迎大家提出宝贵的意见或优化ideas&#xff0c;一起交流学习&#x1f4aa; 大家好&#xff0c;我是Rocky。 2022年底&#xff0c;ChatGPT横空出世&#xff0c;火爆全网&a…

Linux学习(8.6)文件与目录的默认权限与隐藏权限

目录 文件与目录的默认权限与隐藏权限 文件的默认权限&#xff1a;umask chattr (配置文件隐藏属性) lsattr (显示文件隐藏属性) 文件特殊权限&#xff1a; SUID, SGID, SBIT 观察文件类型&#xff1a;file 以下内容转载自鸟哥的Linux私房菜 文件与目录的默认权限与隐藏权…

【架构师】零基础到精通——架构发展

博客昵称&#xff1a;架构师Cool 最喜欢的座右铭&#xff1a;一以贯之的努力&#xff0c;不得懈怠的人生。 作者简介&#xff1a;一名Coder&#xff0c;软件设计师/鸿蒙高级工程师认证&#xff0c;在备战高级架构师/系统分析师&#xff0c;欢迎关注小弟&#xff01; 博主小留言…

【20230225】【剑指1】分治算法(中等)

1.重建二叉树class Solution { public:TreeNode* traversal(vector<int>& preorder,vector<int>& inorder){if(preorder.size()0) return NULL;int rootValuepreorder.front();TreeNode* rootnew TreeNode(rootValue);//int rootValuepreorder[0];if(preo…

redis秒杀

redis优惠券秒杀 为什么订单表订单ID不采用自增长&#xff1f; id规律性太明显&#xff0c;容易被用户猜测到&#xff08;比如第一天下订单id10&#xff0c;第二天下订单id100&#xff0c;在昨天的1天内只卖出90商品&#xff09;受单表数据量限制&#xff08;订单数据量大&am…

从零开始学习iftop流量监控(找出服务器耗费流量最多的ip和端口)

一、iftop是什么iftop是类似于top的实时流量监控工具。作用&#xff1a;监控网卡的实时流量&#xff08;可以指定网段&#xff09;、反向解析IP、显示端口信息等官网&#xff1a;http://www.ex-parrot.com/~pdw/iftop/二、界面说明>代表发送数据&#xff0c;< 代表接收数…

chatGPT模型原理

文章目录简介BertGPT 初代GPT-2GPT-3chatGPT开源ChatGPT简介 openai 的 GPT 大模型的发展历程。 Bert 2018年&#xff0c;自然语言处理 NLP 领域也步入了 LLM 时代&#xff0c;谷歌出品的 Bert 模型横空出世&#xff0c;碾压了以往的所有模型&#xff0c;直接在各种NLP的建模…