如何在 flutter 中使用 agora 进行视频通话?

问题描述 投票:0回答:2

我正在使用官方的flutter agora示例进行视频通话,但本地摄像头只打开,而远程用户从未被调用。

套餐: “agora_rtc_engine:^4.2.0” “权限处理程序:^8.3.0”

这是官方的例子 https://pub.dev/packages/agora_rtc_engine/example

我也在关注这个教程 https://www.youtube.com/watch?v=zVqs1EIpVxs

这是我的代码

    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 = "843b5a8f8c3a4e0fbe2c804654f4ce36";
    const token = "006843b5a8f8c3a4e0fbe2c804654f4ce36IAA4Le0bHwiNWScQ3+1c4sM+UXErd0xfOdjuoZRe1Dz8B9vEKrAAAAAAEADVz7HmNwPgYQEAAQAWA+Bh";
    
    void main() => runApp(MaterialApp(home: MyApp()));
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      int? _remoteUid;
      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;
              });
            },
            
          ),
        );
    
        await _engine.joinChannel(token, "test_video_call", 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 != null) {
          return RtcRemoteView.SurfaceView(uid: _remoteUid!);
        } else {
          return Text(
            'Please wait for remote user to join',
            textAlign: TextAlign.center,
          );
        }
      }
    }

flutter video-streaming agora.io videochat
2个回答
1
投票

我还浏览了插件中提供的示例代码

example
,但您需要做的事情很少
ensure

  1. UID
    每个用户应该不同(如果测试则输入 0)
  2. token
    应该与 create 方法中使用的
    AppId
    相同。 (如果启用了令牌)
  3. Token
    将于
    24 hours
    到期,因此请确保令牌正常工作。
  4. Channel name
    应该是
    unique

0
投票

这里的 Github 链接是使用 Firebase 和自定义 UI 的 Agora SDK 的完整示例

特点:-

  1. 自定义用户界面
  2. 拨打电话和接听电话
  3. 接听来电或拒绝来电
  4. 处理通知
  5. 像 WhatsApp 一样的呼入或响铃状态
  6. 屏幕共享
  7. 显示远程用户音频或视频静音
  8. 通话时长

这里是中等文章链接

https://medium.com/@abhaykumarbhumihar/revolutionizing-communication-a-flutter-chat-app-with-agora-and-firebase-9680ca270397

这是 Github 存储库链接

https://github.com/Abhaykumarbhumihar/chatappwithagora

© www.soinside.com 2019 - 2024. All rights reserved.