接上篇:播放器-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播放器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!