You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Agora.io Web端呼叫拒接时如何关闭流

How to Close Media Streams When Declining an Incoming Call in Agora.io Web SDK

Got it, let's walk through how to properly close media streams and clean up resources when declining an incoming call with Agora's Web SDK. Here's what you need to add to your existing declineCall() method to make this work correctly:

declineCall() {
  // First, clean up the local media stream if it exists
  if (this.localStream) {
    // Stop all individual media tracks (camera, microphone) to release hardware access
    this.localStream.getTracks().forEach(track => {
      track.stop();
    });
    // Close the stream instance to fully free up resources
    this.localStream.close();
    // Reset the reference to avoid memory leaks
    this.localStream = null;
  }

  // Notify the caller that the call was declined (custom message via RTC channel)
  if (this.client) {
    try {
      // Send a targeted message to the incoming caller's UID
      this.client.sendMessage(
        { text: "call_declined" },
        this.incomingCallerUid // Replace this with the actual UID of the caller
      );
    } catch (err) {
      console.error("Failed to send decline notification:", err);
    }
  }

  // Keep your existing state reset logic
  this.incomingCall = false;
}

Important Details to Note:

  • Track First, Then Stream: Always stop individual media tracks before closing the stream. This ensures the browser immediately releases access to the camera and microphone, which is critical for any future calls your users might make.
  • Caller Notification: Sending that custom message lets the caller's app update its UI (like showing "Call declined" instead of just a dropped call). Just make sure you're storing the incoming caller's UID somewhere so you can target the message correctly.
  • Resource Reset: Setting this.localStream = null prevents memory leaks and makes sure your app doesn't accidentally try to use a closed stream later.

If You're Using Agora RTM for Call Invitations:

If your call flow uses Agora's RTM (Real-Time Messaging) service for call invitations, you can use the built-in rejection method instead of a custom RTC message—it handles the signaling for you:

// Assuming you have the incoming invitation stored as this.invitation
if (this.invitation) {
  this.invitation.reject("Declined by user")
    .then(() => console.log("Rejection sent successfully"))
    .catch(err => console.error("Failed to reject call:", err));
}

This will automatically notify the caller through RTM, so their app can handle the rejection cleanly without extra custom messaging code.

内容的提问来源于stack exchange,提问作者vak99

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 14:49:11