文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Flutter使用Android原生播放器详解

2023-02-28 11:39

关注

接上篇:播放器-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三端一定要一致!

到此这篇关于Flutter使用Android原生播放器详解的文章就介绍到这了,更多相关Flutter播放器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     801人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     348人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     311人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     432人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     220人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯