Flutter UI插件写法

dart(UI+控制器)


import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class EditLiveViewController {
  MethodChannel channel;
  BasicMessageChannel _basicMessageChannel;
  Function _onPlayStart;
  Function _onPlayStop;
  Function _onPlayChange;
  EditLiveViewController(){
    channel = const MethodChannel('edit_live_view_plugin');
    channel.setMethodCallHandler((call){
      if("onPlayStart" == call.method){
          if(_onPlayStart != null){
            _onPlayStart();
          }
      }else{

      }
      return null;
    });
    _basicMessageChannel = const BasicMessageChannel("basic_edit_live_view_plugin",  JSONMessageCodec());
    _basicMessageChannel.setMessageHandler((message){
      Map map = message as Map;
      String method = map['method'];
      if("onPlayChange" == method){
        int v = map['value'];
        if(_onPlayChange != null){
          _onPlayChange(v);
        }
      }else if("onPlayStop" == method){
        if(_onPlayStop != null){
          _onPlayStop();
        }
      }
      return null;
    });
  }

  ///监听进度
  setOnPlayStop(Function listener){
    _onPlayStop = listener;
  }
  ///监听进度
  setOnPlayStart(Function listener){
    _onPlayStart = listener;
  }
  ///监听进度
  setOnPlayChange(Function listener){
    _onPlayChange = listener;
  }

  ///播放时间线
  Future playTimeline() {
    return channel.invokeMethod('playTimeline');
  }
  ///暂停时间线
  Future pauseTimeline() {
    return channel.invokeMethod('pauseTimeline');
  }

  ///获取时间总长度
  Future getTimelineDuration() {
    return channel.invokeMethod('getTimelineDuration');
  }
}

class EditLiveView extends StatefulWidget{
  const EditLiveView({
    Key key,
    this.paths,
    this.musicData,
    this.controller,
    this.onCreate
  }):super(key:key);
  final List paths;
  final EditLiveViewController controller;
  final Function onCreate;
  final Map musicData;// 音乐对象 "path":"" "startTime":0, "endTime":0,


  @override
  State createState() {
    return _EditLiveViewState();
  }

}
class _EditLiveViewState extends State{

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if(defaultTargetPlatform == TargetPlatform.iOS){
      return UiKitView(
        key: UniqueKey(),
        viewType: "edit_live_view",//platform_text_view
        onPlatformViewCreated:_onPlatformViewCreated,
        creationParams: {
          "paths":widget.paths,
          "musicData":widget.musicData,
        },
        creationParamsCodec: const StandardMessageCodec(),//new StandardMessageCodec()
      );
    }else{
      return AndroidView(
          key: UniqueKey(),
          viewType: "edit_live_view",
          onPlatformViewCreated:_onPlatformViewCreated,
          creationParams: {
            "paths":widget.paths,
            "musicData":widget.musicData,

          },
          creationParamsCodec: const StandardMessageCodec()
      );
    }
  }

  void _onPlatformViewCreated(int id){
    if(widget.onCreate != null){
      widget.onCreate();
    }
  }

}

Android:

MyPlugs.java

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodChannel;

public class MyPlugs implements FlutterPlugin {

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {

        LiveView.registerWithRegistrar(binding);
        VideoFirstView.registerWithRegistrar(binding);
        EditLiveView.registerWithRegistrar(binding);
        TimelineEditorView.registerWithRegistrar(binding);
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {

    }
}

MainActivity.java

package com.meishe.sdkdemo;

import static com.meishe.sdkdemo.xg.MessageReceiver.*;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

//import com.flutter.flutter_app_upgrade.FlutterAppUpgradePlugin;

import com.dashan.meishi.util.NvAssetManager;
import com.meishe.sdkdemo.loader.FlutterXUpdatePlugin;
import com.meishe.sdkdemo.media.MediaUtils;
import com.meishe.sdkdemo.media.MusicInfo;
import com.meishe.sdkdemo.wxapi.WXEntryActivity;
import com.meishe.sdkdemo.xg.MessageReceiver;
import com.dashan.meishi.Utils;
import com.dashan.meishi.base.base.constants.AndroidOS;
import com.dashan.meishi.util.PermissionsChecker;
import com.dashan.meishi.view.MyPlugs;
import com.google.android.gms.wearable.MessageEvent;
import com.meicam.effect.sdk.NvsEffectSdkContext;
import com.meicam.sdk.NvsStreamingContext;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushConfig;
import com.tencent.android.tpush.XGPushManager;
import com.tencent.android.tpush.XGPushShowedResult;
import com.meishe.sdkdemo.media.MediaData;
import com.meishe.sdkdemo.media.MusicInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.android.FlutterEngineProvider;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends FlutterActivity{
    private FlutterEngine flutterEngine;
    private Activity self  = this;

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        flutterEngine = getFlutterEngine();
        flutterEngine.getPlugins().add(new MyPlugs());
    }
 
}

EditLiveView.java

import android.content.Context;
import android.graphics.Point;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.dashan.meishi.Utils;
import com.dashan.meishi.util.NvsStreamUtil;
import com.meicam.sdk.NvsAudioClip;
import com.meicam.sdk.NvsAudioResolution;
import com.meicam.sdk.NvsAudioTrack;
import com.meicam.sdk.NvsColor;
import com.meicam.sdk.NvsLiveWindowExt;
import com.meicam.sdk.NvsRational;
import com.meicam.sdk.NvsStreamingContext;
import com.meicam.sdk.NvsTimeline;
import com.meicam.sdk.NvsVideoClip;
import com.meicam.sdk.NvsVideoFx;
import com.meicam.sdk.NvsVideoResolution;
import com.meicam.sdk.NvsVideoTrack;
import com.meicam.sdk.NvsVolume;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.JSONMessageCodec;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;

public class EditLiveView extends PlatformViewFactory implements PlatformView {


    private static NvsLiveWindowExt mLiveWindow;
    private static BinaryMessenger messenger;
    public static MethodChannel methodChannel;
    private static BasicMessageChannel basicMessageChannel;

    private static EditLiveView instance;
    private Handler mainHandler = new Handler(Looper.getMainLooper());

    private EditLiveView() {
        super(StandardMessageCodec.INSTANCE);
    }
    public static void registerWithRegistrar(@NonNull FlutterPlugin.FlutterPluginBinding binding){
        methodChannel = new MethodChannel(binding.getBinaryMessenger(),"edit_live_view_plugin");
        methodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
                if("playTimeline".equals(call.method)){//播放时间轴  开始播放/恢复播放
                    if(instance != null){
                        instance.playTimeline();
                    }
                }else if("pauseTimeline".equals(call.method)){//暂停时间轴
                    if(instance != null){
                        instance.pauseTimeline();
                    }
                }else if("getTimelineDuration".equals(call.method)){
                    if(instance != null){
                       result.success(instance.getTimelineDuration());
                    }
                }


            }
        });

        basicMessageChannel = new BasicMessageChannel(binding.getBinaryMessenger(),"basic_edit_live_view_plugin", JSONMessageCodec.INSTANCE);
        basicMessageChannel.setMessageHandler(new BasicMessageChannel.MessageHandler() {
            @Override
            public void onMessage(@Nullable Object message, @NonNull BasicMessageChannel.Reply reply) {

            }
        });

        if(instance == null){
            instance = new EditLiveView();
        }
        messenger = binding.getBinaryMessenger();
        binding.getPlatformViewRegistry().registerViewFactory("edit_live_view",instance);
    }

    //播放时间线
    private void playTimeline(){
        if(mStreamingContext != null && mTimeline != null){
            Log.e("test","" + mTimeline.getDuration());
            Log.e("test","" + mStreamingContext.getTimelineCurrentPosition(mTimeline));
            long duration = mStreamingContext.getTimelineCurrentPosition(mTimeline) - mTimeline.getDuration();
            duration = Math.abs(duration);
            if(duration < 30000){
                mStreamingContext.playbackTimeline(mTimeline,0, mTimeline.getDuration(), NvsStreamingContext.VIDEO_PREVIEW_SIZEMODE_LIVEWINDOW_SIZE, true, 0);
                if(methodChannel != null){
                    methodChannel.invokeMethod("onPlayStart",null);
                }
            }else{
                mStreamingContext.playbackTimeline(mTimeline, mStreamingContext.getTimelineCurrentPosition(mTimeline), mTimeline.getDuration(), NvsStreamingContext.VIDEO_PREVIEW_SIZEMODE_LIVEWINDOW_SIZE, true, 0);
            }

        }
    }
    //停止时间线
    private void pauseTimeline(){
        if(mStreamingContext != null && mTimeline != null){
            mStreamingContext.pausePlayback();
        }
    }
    //
    private long getTimelineDuration(){
        if(mTimeline != null){
            return mTimeline.getDuration();
        }else{
            return 0;
        }
    }


    @Override
    public View getView() {
        return mLiveWindow;
    }
    @Override
    public void dispose() {

    }



    @Override
    public PlatformView create(@Nullable Context context, int viewId, @Nullable Object args) {
        mLiveWindow = new NvsLiveWindowExt(context);
        Map map = (Map) args;
        List paths = (List)  map.get("paths");// 视频列表
        Map musicData =(Map) map.get("musicData");  // 音乐数据

        System.out.println("edit接收的音乐:"+musicData);
        //todo 初始化函数
        initView(paths,musicData);
        //todo 测试函数
//        test();
        return instance;
    }

    //获取当前引擎状态
    private int getCurrentEngineState() {
        return mStreamingContext.getStreamingEngineState();
    }

}

你可能感兴趣的:(Flutter UI插件写法)