fluttervideocall

How to detect if peer leaves video call in Flutter using Agora


I have two apps are built in Flutter, Doctor app and Patient app, a Doctor establishes a video call with Patient using Agora RTC Engine (peer-to-peer)..

I'm wondering if there is a way (from front-end) to detect when the other peer has left the call (due the poor connection, being disconnected or even got hang-up.

Here is the library i'm using: agora_rtc_engine


Solution

  • I found the answer if someone is interested.

    So first import the library:

    import 'package:agora_rtc_engine/rtc_engine.dart';
    

    And then declare this variable:

    late RtcEngine _engine;
    

    Then add this function and add your own code:

    Future <void> setHandler() async {
        _engine = await RtcEngine.createWithConfig(RtcEngineConfig("$REMOTE_VIDEO_UID"));
    
        await _engine.enableVideo();
    
        _engine.setEventHandler(
          RtcEngineEventHandler(
            joinChannelSuccess: (String channel, int uid, int elapsed) {
              print("Local user has joined the call!");
              // Your code goes here
            },
    
            userJoined: (int uid, int elapsed) {
              print("Local user has joined the call!");
              // Your code goes here
            },
    
            userOffline: (int uid, UserOfflineReason reason) {
              print("remote user has left the call!");
              // Your code goes here
            },
    
          ),
        );
      }
    

    And finally call the handler like this within your preferred state:

      setState(
        () {
          setHandler();
        },
      );
    

    Thanks to this guy for his tutorial.