Start/End a call

Updated at: 2025-03-05 09:20

This topic describes how to use AICallKit SDK to create and initialize an engine instance, as well as how to start and end a call.

Usage notes

Flowchart

image

Before calling the call() method, you must start a call instance by calling the GenerateAIAgentCall operation.

Perform the following steps:

  1. Create an engine instance and use it as a member variable.

  2. Configure callbacks.

  3. Initialize the configurations, including whether to disable the microphone, enable the handset, and use the front camera.

  4. Configure call settings:

    1. Set the rendering view if a digital human agent is used.

    2. Set the preview screen and mode if a visual understanding agent is used.

Sample code

Android
iOS
Web
// Define member variables.
ARTCAICallEngine mARTCAICallEngine = null;

// Create and initialize an engine instance.
void setupEngine(Context context) {
    // Initialize the engine instance.
    // context -> Android Context
    // userId -> The ID of the user who joins the Real-Time Communication (RTC) channel.
    mARTCAICallEngine = new ARTCAICallDepositEngineImpl(context, userId); 

    // Configure callbacks.
    mARTCAICallEngine.setEngineCallback(mCallEngineCallbackWrapper); 

    // Configure the startup parameters for the engine.
    ARTCAICallEngine.ARTCAICallConfig artcaiCallConfig = new ARTCAICallEngine.ARTCAICallConfig();
    // Specify a temporary intelligent agent ID, if required.
    artcaiCallConfig.aiAgentId = aiAgentId;
    mARTCAICallEngine.init(artcaiCallConfig);
    
    // Specify the type of the intelligent agent, such as voice, digital human, or visual understanding.
    mARTCAICallEngine.setAiAgentType(aiAgentType);

    // Configure a view if a digital human agent is used.
    if (aiAgentType == AvatarAgent) {
        mARTCAICallEngine.setAvatarAgentView(
            avatarlayer,
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                       ViewGroup.LayoutParams.MATCH_PARENT)
        );
    }
    // Configure a view for local video preview if a visual understanding agent is used.
    else if (aiAgentType == VisionAgent) {
        mARTCAICallEngine.setVisionPreviewView(previewLayer,
            new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                         ViewGroup.LayoutParams.MATCH_PARENT)
        );
    }
}

// Start a call after an intelligent agent is started.
void start() {
    // Start the intelligent agent and make a call.
    // Obtain the following information: channle_id, ai_agent_user_id, agent_instance_id, and rtc_auth_token.

    // Start the call.
    mARTCAICallEngine.call(rtc_auth_token, agent_instance_id, 
                           ai_agent_user_id, channle_id);
}

// End the call and disable the intelligent agent.
void handup() {
    mARTCAICallEngine.handup();
}

// Handle callback events.
ARTCAICallEngine.IARTCAICallEngineCallback mCallEngineCallbackWrapper = new ARTCAICallEngine.IARTCAICallEngineCallback() {
    @Override
    public void onErrorOccurs(ARTCAICallEngine.AICallErrorCode errorCode) {
        // Hang up if an error occurs during the call.
        mARTCAICallEngine.handup();
    }

    @Override
    public void onCallBegin() {
        // Triggered when the call begins.
    }

    @Override
    public void onCallEnd() {
        // Triggered when the call ends.
    }
};
// Integrate the SDK.
import ARTCAICallKit


// Create an engine instance.
let engine = ARTCAICallEngineFactory.createEngine()
let agentType: ARTCAICallAgentType
let userId: String

deinit {
    // Release the engine instance.
    self.engine.destroy()
}

public func setup() {
    // Configure callbacks.
    self.engine.delegate = self

    // Configure a view if a digital human agent is used.
    if self.agentType == .AvatarAgent {
        let viewConfig = ARTCAICallViewConfig(view: self.avatarAgentView)
        self.engine.setAgentViewConfig(viewConfig: viewConfig)
    }
    // Configure a view for local video preview if a visual understanding agent is used.
    else if self.agentType == .VisionAgent {
        // frameRate is set to 5, and adjust it based on the frame rate (generally 2) in the console. The maximum value cannot exceed 15.
        // bitrate: set to 512 if the value of frameRate exceeds 10.
        let visionConfig = ARTCAICallVisionConfig(preview: self.visionCameraView, viewMode: .Auto, frameRate: 5, bitrate: 340)
        self.engine.visionConfig = visionConfig
    }
}

// Start a call after an intelligent agent is started.
public func start() {
    // Start the intelligent agent and make a call.
    // Obtain the following information: channle_id, ai_agent_user_id, agent_instance_id, and rtc_auth_token.

    // Start the call.
    let agentInfo = ARTCAICallAgentInfo(agentType: self.agentType, channelId: channel_id, uid: ai_agent_user_id, instanceId: agent_instance_id)
    self.engine.call(userId: self.userId, token: rtc_auth_token, agentInfo: agentInfo) { [weak self] error in
        if let error = error {
            // Handle the error.
        }
        else {
            // The call is successful.
        }
    }
}

// End the call and disable the intelligent agent.
public func handup() {
    // true indicates that the intelligent agent immediately ends the call.
    self.engine.handup(true)
}

// Handle callback events.
func onErrorOccurs(code: ARTCAICallErrorCode) {
    // Hang up if an error occurs during the call.
    self.engine.handup()
}

func onCallBegin() {
    // Triggered when the call begins.
}

func onCallEnd() {
    // Triggered when the call ends.
}
import ARTCAICallEngine from 'aliyun-auikit-aicall';

let engine;

// Create and initialize an engine instance.
const setup = () => {
  engine = new ARTCAICallEngine();

  engine.on('errorOccurred', (errorCode) => {
    // Hang up or perform other actions if an error occurs during the call.
    engine.handup();
    console.error('AICallErrorOccurred', errorCode);
  });

  engine.on('callBegin', () => {
    // Mark the start of the call with the intelligent agent.
    console.log('AICallBegin');
  });

  engine.on('callEnd', () => {
    // Mark the end of the call with the intelligent agent.
    console.log('AICallEnd');
  });
};

const start = async () => {
  // Start the intelligent agent and make a call.

  try {
    engine.call(
      // The user ID.
      userId,
      // The information about the intelligent agent.
      // agentInfo.agentType indicates the type of the intelligent agent, which can be voice, digital person, or visual understanding.
      // agentInfo.instanceId indicates the ID of the intelligent agent instance, which corresponds to agent_instance_id in the API.
      // agentInfo.channelId indicates the channel ID of the intelligent agent, which corresponds to channel_id in the API.
      // agentInfo.userId indicates the ID of the intelligent agent user, which corresponds to ai_agent_user_id in the API.
      // agentInfo.rtcToken indicates the authentication token, which corresponds to rtc_auth_token in the API.
      // (Optional) agentInfo.reqId indicates the request ID, which corresponds to req_id in the API.
      agentInfo,
      // (Optional) Intelligent agent configurations, which overwrite the configurations of the engine instance.
      // config.muteMicrophone specifies whether to mute the microphone. Default value: false.
      // config.muteCamera specifies whether to disable the camera if a visual understanding agent is used. Default value: false.
      // config.previewElement specifies the local preview element if a visual understanding agent is used.
      // config.cameraConfig specifies the configuration of the local camera if a visual understanding agent is used, including the width, height, frameRate, and bitrate.
      // config.templateConfig: https://help.aliyun.com/zh/ims/developer-reference/api-ice-2020-11-09-generateaiagentcall
      config
    );
    // The call is successful.
  } catch (error) {
    // Handle the error.
  }
};

// End the call and disable the intelligent agent.
const handup = () => {
  engine.handup();
};
  • On this page (1, T)
  • Usage notes
  • Flowchart
  • Sample code
Feedback
phone Contact Us

Chat now with Alibaba Cloud Customer Service to assist you in finding the right products and services to meet your needs.

alicare alicarealicarealicare