简体   繁体   English

Flutter中的Agora-在视频聊天中禁用一个人的视频并使另一个人的视频全屏

[英]Agora in Flutter- disablle one persons's video and make another person's video full screen in the video chat

I am using Agora in a Flutter application.我在Flutter应用程序中使用Agora It's a very basic app where 2 users can just go online to have a video chat with one another.这是一个非常基本的应用程序,其中 2 个用户只需 go 在线进行视频聊天。 The source code is pretty small in size and easy to understand as well.源代码很小,也很容易理解。 But my issue is: I want to show only one person (Person1) in the video and not the other person (Person2).但我的问题是:我只想在视频中显示一个人(Person1)而不是另一个人(Person2)。

I have used the agora_rtc_engine plugin.我使用了agora_rtc_engine插件。

main.dart : main.dart

import 'dart:async';

import 'package:agora_rtc_engine/rtc_engine.dart';
import 'package:agora_rtc_engine/rtc_local_view.dart' as RtcLocalView;
import 'package:agora_rtc_engine/rtc_remote_view.dart' as RtcRemoteView;
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';


const appId = "...";//Obtain it from Agora site
const token = ""; // Temporary token generated from Agora site

void main() => runApp(MaterialApp(home: MyApp()));

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // int? _remoteUid=1;

  int _remoteUid=1; // for another user remoteUid=2;
  bool _localUserJoined = false;
  late RtcEngine _engine;

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

  Future<void> initAgora() async {
    // retrieve permissions
    await [Permission.microphone, Permission.camera].request();

    //create the engine
    _engine = await RtcEngine.create(appId);
    await _engine.enableVideo();
    _engine.setEventHandler(
      RtcEngineEventHandler(
        joinChannelSuccess: (String channel, int uid, int elapsed) {
          print("local user $uid joined");
          setState(() {
            _localUserJoined = true;
          });
        },
        userJoined: (int uid, int elapsed) {
          print("remote user $uid joined");
          setState(() {
            _remoteUid = uid;
          });
        },
        userOffline: (int uid, UserOfflineReason reason) {
          print("remote user $uid left channel");
          setState(() {
            // _remoteUid = null;
            _remoteUid = 0;
          });
        },
      ),
    );

    // await _engine.joinChannel(token, "test", null, 0);
    await _engine.joinChannel(token, "InstaClass", null, 0);

  }

  // Create UI with local view and remote view
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Agora Video Call'),
      ),
      body: Stack(
        children: [
          Center(
            child: _remoteVideo(),
          ),
          Align(
            alignment: Alignment.topLeft,
            child: Container(
              width: 100,
              height: 150,
              child: Center(
                child: _localUserJoined
                    ? RtcLocalView.SurfaceView()
                    : CircularProgressIndicator(),
              ),
            ),
          ),
        ],
      ),
    );
  }

  // Display remote user's video
  Widget _remoteVideo() {
    
    if (_remoteUid != 0) {
      return RtcRemoteView.SurfaceView(
        uid: _remoteUid,
        channelId: "InstaClass",
      );
    }else {
      return Text(
        'Please wait for remote user to join',
        textAlign: TextAlign.center,
      );
    }
  }
}

Same code is used for both Person1 and Person2 with a few changes. Person1 和 Person2 使用相同的代码,但有一些更改。 The above code snippet is for Person1 (notice int remoteUid=1; ).上面的代码片段适用于 Person1(注意int remoteUid=1; )。 For Person2, I have used int remoteUid=2;对于 Person2,我使用了int remoteUid=2; . . For disabling the video of Person2, in Person2's app, I have added the following line为了禁用 Person2 的视频,在 Person2 的应用程序中,我添加了以下行

_engine.enableLocalVideo(false)

after the line:行后:

await _engine.enableVideo();

Now Person2 sees Person1's video (ie, remote video) in full screen but does not see his own (Person2) video (ie, local video).现在Person2 可以全屏看到Person1 的视频(即远程视频),但看不到他自己(Person2)的视频(即本地视频)。 A black rectangle is shown in the place of the local video.黑色矩形显示在本地视频的位置。

Q1) How can I hide this black rectangle? Q1) 如何隐藏这个黑色矩形? I just want to show only the remote video from Person2's side.我只想显示来自 Person2 的远程视频。

And from person1's side, no video of Person2 is shown and Person1's video is shown in a small rectangle.从 person1 的角度来看,没有显示 Person2 的视频,而 Person1 的视频显示在一个小矩形中。

Q2) How can I show Person1's video in full screen instead of a small rectangle from his own (Person1) side? Q2) 我怎样才能全屏显示 Person1 的视频,而不是从他自己 (Person1) 一侧的小矩形?

You need to remove Align widget from you code to hide the small screen from your call UI.您需要从代码中删除Align小部件以从您的呼叫 UI 中隐藏小屏幕。 This will hide the box and even if you don't call _engine.enableLocalVideo(false) functions local video will not be visible and after doing so your both questions should be solved.这将隐藏该box ,即使您不调用_engine.enableLocalVideo(false)函数,本地视频也将不visible ,这样做之后您的两个问题都应该得到解决。 Try once试一次

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Agora Video Call'),
      ),
      body: Stack(
        children: [
          Center(
            child: _remoteVideo(),
          ),
        ],
      ),
    );
  }

For Full screen video of self you can assign local video instead of remote video to the Center widget对于自己的全屏视频,您可以将local视频而不是remote视频分配给中心小部件

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Agora Video Call'),
      ),
      body: Stack(
        children: [
          Center(
            child: _localUserJoined
                ? RtcLocalView.SurfaceView()
                : CircularProgressIndicator(),
          ),
        ],
      ),
    );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM