您好,登录后才能下订单哦!
这篇文章主要介绍“Flutter怎么使用Android原生播放器”,在日常操作中,相信很多人在Flutter怎么使用Android原生播放器问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Flutter怎么使用Android原生播放器”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
创建插件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怎么使用Android原生播放器”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。