在flutter中使用agora进行屏幕共享

如何解决在flutter中使用agora进行屏幕共享

我需要在我的 flutter 应用程序中使用 argora sdk 添加屏幕共享功能。 屏幕共享仅支持本机。但是不知道如何调用本机代码来颤动。 我试图从本机端启动屏幕共享服务,但是当服务启动时,视频视图被冻结。我试过下面的代码。

以下代码适用于安卓端:

@Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(),CHANNEL)
                .setMethodCallHandler(
                        (call,result) -> {
                            if (call.method.equals("shareScreen")) {
                                handler = new Handler(Looper.getMainLooper());

                                appId = call.argument("appId");
                                token = call.argument("token");
                                channelId = call.argument("ChannelId");
                                Log.d("AppId:" + appId,"Token:"+token);
                                IRtcEngineEventHandler iRtcEngineEventHandler = new IRtcEngineEventHandler() {
                                    @Override
                                    public void onWarning(int warn) {
                                        Log.w(TAG,String.format("onWarning code %d message %s",warn,RtcEngine.getErrorDescription(warn)));
                                    }

                                    /**Reports an error during SDK runtime.
                                     * Error code: https://docs.agora.io/en/Voice/API%20Reference/java/classio_1_1agora_1_1rtc_1_1_i_rtc_engine_event_handler_1_1_error_code.html*/
                                    @Override
                                    public void onError(int err) {
                                        Log.e(TAG,String.format("onError code %d message %s",err,RtcEngine.getErrorDescription(err)));
                                        videoCallJoinMsg = String.format("onError code %d message %s",RtcEngine.getErrorDescription(err));
                                        result.error("VIDEOCALL_ERROR",videoCallJoinMsg,null);
                                    }

                                    /**Occurs when the local user joins a specified channel.
                                     * The channel name assignment is based on channelName specified in the joinChannel method.
                                     * If the uid is not specified when joinChannel is called,the server automatically assigns a uid.
                                     * @param channel Channel name
                                     * @param uid User ID
                                     * @param elapsed Time elapsed (ms) from the user calling joinChannel until this callback is triggered*/
                                    @Override
                                    public void onJoinChannelSuccess(String channel,int uid,int elapsed) {
                                        Log.i(TAG,String.format("onJoinChannelSuccess channel %s uid %d",channel,uid));
                                        videoCallJoinMsg = String.format("onJoinChannelSuccess channel %s uid %d",uid);
                                        myUid = uid;
                                        joined = true;
                                        handler.post(() -> {
                                            videoCallJoinMsg = "Joined Successfully";
                                            joined = true;
                                            result.success(videoCallJoinMsg);
                                        });
                                    }

                                    @Override
                                    public void onLocalVideoStateChanged(int localVideoState,int error) {
                                        super.onLocalVideoStateChanged(localVideoState,error);
                                        if (localVideoState == 1) {
                                            Log.e(TAG,"launch successfully");
                                        }
                                    }

                                    @Override
                                    public void onRemoteVideoStateChanged(int uid,int state,int reason,int elapsed) {
                                        super.onRemoteVideoStateChanged(uid,state,reason,elapsed);
                                        Log.i(TAG,"onRemoteVideoStateChanged:uid->" + uid + ",state->" + state);
                                        if (state == REMOTE_VIDEO_STATE_STARTING) {
                                            /*Check if the context is correct/
                                            Context context = getApplicationContext();
                                            if (context == null) {
                                                return;
                                            }
                                            handler.post(() ->
                                            {
                                                remoteUid = uid;
                                                curRenderMode = RENDER_MODE_HIDDEN;
//                                                setRemotePreview(context);
                                            });
                                        }
                                    }

                                    @Override
                                    public void onRemoteVideoStats(RemoteVideoStats stats) {
                                        super.onRemoteVideoStats(stats);
                                        Log.d(TAG,"onRemoteVideoStats: width:" + stats.width + " x height:" + stats.height);
                                    }

                                    /**Occurs when a remote user (Communication)/host (Live Broadcast) joins the channel.
                                     * @param uid ID of the user whose audio state changes.
                                     * @param elapsed Time delay (ms) from the local user calling joinChannel/setClientRole
                                     *                until this callback is triggered.*/
                                    @Override
                                    public void onUserJoined(int uid,int elapsed) {
                                        super.onUserJoined(uid,"onUserJoined->" + uid);
                                        videoCallJoinMsg = String.format("user %d joined!",uid);
                                    }

                                    @Override
                                    public void onUserOffline(int uid,int reason) {
                                        Log.i(TAG,String.format("user %d offline! reason:%d",uid,reason));
                                        videoCallJoinMsg = String.format("user %d offline! reason:%d",reason);
                                        handler.post(() -> {
                                            /**Clear render view
                                             Note: The video will stay at its last frame,to completely remove it you will need to
                                             remove the SurfaceView from its parent*/
                                            ENGINE.setupRemoteVideo(new VideoCanvas(null,RENDER_MODE_HIDDEN,uid));
//                                            fl_remote.removeAllViews();
                                        });
                                    }
                                };
                                try {
                                    ENGINE = RtcEngine.create(getApplicationContext(),appId,iRtcEngineEventHandler);
                                    Log.d("RtcEngine:","started");

                                }
                                catch (Exception e) {
                                    e.printStackTrace();
                                    Log.d("RtcEngine Error:",e.getMessage());
                                    result.error("UNAVAILABLE",null);

                                    onBackPressed();
                                }
                                bindVideoService();
                            }
                            else {
                                result.notImplemented();
                            }
                        }
                );
    }
    
    private void bindVideoService() {
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(),ExternalVideoInputService.class);
        mServiceConnection = new VideoInputServiceConnection();
        getApplicationContext().bindService(intent,mServiceConnection,Context.BIND_AUTO_CREATE);
    }

    private void unbindVideoService() {
        if (mServiceConnection != null) {
            getApplicationContext().unbindService(mServiceConnection);
            mServiceConnection = null;
        }
    }

    private class VideoInputServiceConnection implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName componentName,IBinder iBinder) {
            mService = (IExternalVideoInputService) iBinder;
            /*Start the screen recording service of the system/
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                MediaProjectionManager mpm = (MediaProjectionManager)
                        getSystemService(Context.MEDIA_PROJECTION_SERVICE);
                Intent intent = mpm.createScreenCaptureIntent();
                startActivityForResult(intent,PROJECTION_REQ_CODE);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mService = null;
        }
    }

颤振侧代码:

class _State extends State<JoinChannelVideo> {
  late final RtcEngine _engine;
  String channelId = ChannelId;
  bool isJoined = false,switchCamera = true,switchRender = true;
  List<int> remoteUid = [];
  TextEditingController? _controller;
  static const platform = const MethodChannel("com.agora/videoCallScreenShareMethodChannel");

  videoCallScreenShare() async{
    try {
      Map data = {
        "appId" : appId,"token" : token,"ChannelId" : channelId
      };
      await _engine.stopPreview();

    final result = await platform.invokeMethod('shareScreen',data);
      print("Result: $result");
    } on PlatformException catch (e) {
    }
  }

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController(text: channelId);
    this._initEngine();
  }

  @override
  void dispose() {
    super.dispose();
    _engine.destroy();
  }

  _initEngine() async {
    _engine = await RtcEngine.createWithConfig(RtcEngineConfig(appId));
    this._addListeners();

    await _engine.enableVideo();
    await _engine.startPreview();
    await _engine.setChannelProfile(ChannelProfile.LiveBroadcasting);
    await _engine.setClientRole(ClientRole.Broadcaster);
  }

  _addListeners() {
    _engine.setEventHandler(RtcEngineEventHandler(
      joinChannelSuccess: (channel,elapsed) {
        print('joinChannelSuccess ${channel} ${uid} ${elapsed}');
        ScaffoldMessenger.of(context).showSnackBar(new SnackBar(content: Text('Channel $channel Joined Successfully! ${uid} \n ${elapsed}',style: TextStyle(color: Colors.white),)));
        setState(() {
          isJoined = true;
        });
      },userJoined: (uid,elapsed) {
        print('userJoined  ${uid} ${elapsed}');
        ScaffoldMessenger.of(context).showSnackBar(new SnackBar(content: Text('User Joined: ${uid} \n ${elapsed}',)));
        setState(() {
          remoteUid.add(uid);
        });
      },userOffline: (uid,reason) {
        print('userOffline  ${uid} ${reason}');
        ScaffoldMessenger.of(context).showSnackBar(new SnackBar(content: Text('User Offline: ${uid} \n ${reason}',)));

        setState(() {
          remoteUid.removeWhere((element) => element == uid);
        });
      },remoteVideoStats: (stats){
        print('RemoteVideo Stats ${stats.toJson()}\n');
        ScaffoldMessenger.of(context).showSnackBar(new SnackBar(content: Text('Remote Video Stats: ${stats.toJson()}',)));
        setState(() {
        });
      },remoteVideoStateChanged: (uid,stats,num){
        print('RemoteVideo Stats $uid,$reason,$stats,$num\n');
        ScaffoldMessenger.of(context).showSnackBar(new SnackBar(content: Text('Remote Video Stats changed!',)));
      },leaveChannel: (stats) {
        print('leaveChannel ${stats.toJson()}');
        setState(() {
          isJoined = false;
          remoteUid.clear();
        });
      },));
  }

  _joinChannel() async {
    if (defaultTargetPlatform == TargetPlatform.android) {
      await [Permission.microphone,Permission.camera].request();
    }
    await _engine.joinChannel(token,channelId,null,uid);
  }

  _leaveChannel() async {
    await _engine.leaveChannel();
  }

  _switchCamera() {
    _engine.switchCamera().then((value) {
      setState(() {
        switchCamera = !switchCamera;
      });
    }).catchError((err) {
      print('switchCamera $err');
    });
  }

  _switchRender() {
    setState(() {
      switchRender = !switchRender;
      remoteUid = List.of(remoteUid.reversed);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('VideoView'),),body: Stack(
        children: [
          Column(
            children: [
              TextField(
                controller: _controller,decoration: InputDecoration(hintText: 'Channel ID'),onChanged: (text) {
                  setState(() {
                    channelId = text;
                  });
                },Row(
                children: [
                  Expanded(
                    flex: 1,child: ElevatedButton(
                      onPressed:
                      isJoined ? this._leaveChannel : this._joinChannel,child: Text('${isJoined ? 'Leave' : 'Join'} channel'),)
                ],_renderVideo(),],Align(
            alignment: Alignment.centerRight,child: Column(
              mainAxisSize: MainAxisSize.min,children: [
                ElevatedButton(
                  onPressed: this._switchCamera,child: Text('Camera ${switchCamera ? 'front' : 'rear'}'),ElevatedButton(
                  onPressed: videoCallScreenShare,child: Text('ScreenShare'),)
        ],);
  }

  _renderVideo() {
    return Expanded(
      child: Stack(
        children: [
          RtcLocalView.SurfaceView(),Align(
            alignment: Alignment.bottomLeft,child: SingleChildScrollView(
              scrollDirection: Axis.horizontal,child: Row(
                children: List.of(remoteUid.map(
                      (e) => GestureDetector(
                    onTap: this._switchRender,child: Container(
                      width: 120,height: 150,margin: EdgeInsets.only(bottom: 50),color: Colors.black,child: RtcRemoteView.SurfaceView(
                        uid: e,)),);
  }
}

有人可以帮忙吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res