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

How to make video call in flutter with agora?

我用的是官方的flutter agora视频通话的例子,但是本地摄像头只打开,远程用户一直没有被调用。

包: “agora_rtc_engine:^4.2.0” "permission_handler: ^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,
          );
        }
      }
    }

我还查看了插件 example 中可用的示例代码,但您需要做的事情很少 ensure

  1. UID 每个用户应该不同(如果测试则放 0)
  2. token 应该来自创建方法中使用的相同 AppId。 (如果启用令牌)
  3. Token 将在 24 hours 后到期,因此请确保该令牌有效。
  4. Channel name 应该是 unique