您好,登錄后才能下訂單哦!
這篇文章主要介紹“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。