How do I get the user's call duration?
Updated
During a call, a user may join and leave an RTC channel multiple times. The user's call duration is the actual time when the user is in the channel. You can use the Agora RTC SDK or Agora Analytics to get a user's call duration.
Implementation
Use the Agora RTC SDK
By design, the SDK triggers the onRtcStats callback after a user successfully joins an RTC channel. To get the duration between each channel-join and channel-leave, use the totalDuration parameter of the last onRtcStats callback triggered before the user leaves the channel. If a user joins and leaves an RTC channel for multiple times, you can calculate the user's call duration by adding up the values of totalDuration.
The above callback is in Java. For other programming languages, use the following parameters to get the user's call duration:
- C++: The
durationparameter of theonRtcStatscallback. - Objective-C: The
durationparameter of thereportRtcStatscallback. - JavaScript: The
Durationparameter of theClient.getSessionStatsmethod.
If the connection is lost under poor network conditions, the calculated duration may be inaccurate. Use Agora Signaling SDK or a different signaling system to implement a heartbeat mechanism, and get the disconnected time of the client. You can calculate the user's call duration by subtracting the disconnected time of the client from the call duration obtained from the Agora RTC SDK.
Use Agora Analytics
On the Call Search page of Agora Analytics, you can find a user's call duration in the In-call Periods column. See Call Search.
