在 Flutter 上显示空白屏幕的 Agora 本地视图

我正在尝试集成 Agora SDK,我已经能够将其设置为正常运行,但唯一的问题是本地视图是空白的,直到我进行热重启,热重启后一切正常,远程视图正在工作完美,我一直试图调试这个问题无济于事,下面是我的代码。


    import 'dart:async';
    import 'package:flutter/material.dart';
    import 'package:permission_handler/permission_handler.dart';

    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;

    const appId = "e660ebb529e34d838fedc01ed1a1b5e4";
    const token =
        "006e660ebb529e34d838fedc01ed1a1b5e4IABB69OL6buUPHAgM4dq+hpsHKMLCooStMhXyvSdauF2eNzDPrsAAAAAEADHF4BY49MwYQEAAQDi0zBh";

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

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

    class _MyAppState extends State {
      int _remoteUid;
      RtcEngine _engine;

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

      Future 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");
            },
            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, "firstchannel", 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: 100,
                  child: Center(
                    child: RtcLocalView.SurfaceView(),
                  ),
                ),
              ),
            ],
          ),
        );
      }

      Widget _renderLocalPreview() {
        return RtcLocalView.SurfaceView();
      }

      // 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,
          );
        }
      }
    }
stack overflow Agora local view showing blank screen on Flutter
原文答案
author avatar

接受的答案

在为这个问题苦苦挣扎了一段时间后,我意识到问题出在哪里。在尝试显示本地预览之前,我需要包含一个布尔变量来检查本地用户是否已加入频道。显然,Agora SDK 被设计为仅在用户加入频道时显示本地预览。

` `` Widget _renderLocalPreview() { if (_localUserJoined) { return RtcLocalView.SurfaceView(); } else { return Text( '加入频道,请稍候.....', textAlign: TextAlign.center, ); } } `` `


答案:

作者头像

确保 Rtc 远程视图中的 Uid 与用户上线和用户加入直播相同

RtcRemoteView.SurfaceView( uid: , channelId:, )