目录

Outgoing Call

Use Cases

The app initiates a voice or video call.

When an outgoing call is made, the call capsule appears in the upper left corner of the screen since the app is in the foreground and no notification banner is needed. The display effect of an outgoing call is as follows.

Outgoing Voice Call (Calling) Outgoing Voice Call (Ongoing) Outgoing Video Call (Calling) Outgoing Video Call (Ongoing)

For an outgoing call, the user can pull down the notification center panel to mute and unmute the call, or hang up on the live view banner.

Outgoing Call Live View Notification (Calling) Outgoing Call Live View Notification (Ongoing)

Constraints

The outgoing call capability is supported on phones and tablets. Support for wearables was added in API version 6.0.0 (20), and support for PCs/2-in-1 devices was added in API version 6.1.1 (24).

Service Process

API Description

APIs used for outgoing calls are provided by voipCall. For more information, please refer to the API Reference.

API Description
on(type: 'voipCallUiEvent', callback: Callback<VoipCallUiEventInfo>): void Subscribes to voipCallUiEvent.
off(type: 'voipCallUiEvent', callback?: Callback<VoipCallUiEventInfo>): void Unsubscribes from voipCallUiEvent.
reportOutgoingCall(voipCallAttribute: VoipCallAttribute): Promise<ErrorReason> Reports the outgoing call.
reportCallAudioEventChange(callId: string, callAudioEvent: CallAudioEvent): Promise<void> Reports the audio event.
reportCallStateChange(callId: string, callState: VoipCallState): Promise<void> Reports the call status change.
reportCallStateChange(callId: string, callState: VoipCallState, callType: VoipCallType): Promise<void> Reports the call status change and specifies the call type.

Development Procedure

The development procedure for outgoing calls is similar to that for incoming calls.

  1. Import dependencies.

    import { voipCall } from '@kit.CallServiceKit';
    import { image } from '@kit.ImageKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
  2. Subscribe to voipCallUiEvent to detect the user's mute, unmute, and hang-up operations on the live view notification banner. It is recommended to complete the step before reporting the outgoing call.

    The sample code is as follows:

    // Subscribe to voipCallUiEvent.
    voipCall.on('voipCallUiEvent', callback => {
      hilog.info(0x0000, 'CallDemo', 'Succeeded in registering voipCallUiEvent');
    });
  3. After a call connection is established within the app, the outgoing call must be reported to Call Service Kit along with call details. For more information, please refer to VoipCallAttribute.

    The system displays the call capsule in the upper-left corner of the screen.

    Sample code:

    // Construct parameters for reporting the outgoing call.
    let voipCallAttribute: voipCall.VoipCallAttribute = {
      callId: 'callId123',
      voipCallType: voipCall.VoipCallType.VOIP_CALL_VOICE,
      userName: 'Jack',
      userProfile: image.createPixelMapSync(new ArrayBuffer(100), { size: { width: 90, height: 90 } }),
      abilityName: 'VoipCallAbility',
      voipCallState: voipCall.VoipCallState.VOIP_CALL_STATE_DIALING,  // The outgoing call state must be DIALING.
      showBannerForIncomingCall: true
    };
    
    // Report the outgoing call to Call Service Kit.
    voipCall.reportOutgoingCall(voipCallAttribute).then(errorReason => {
      if (errorReason == voipCall.ErrorReason.ERROR_NONE) {
        hilog.info(0x0000, 'CallDemo', 'Succeeded in reporting the outgoing call');
      } else {
        hilog.error(0x0000, 'CallDemo', 'Failed to report the outgoing call: %{public}d', errorReason);
      }
    });

    When reporting an outgoing call, the call status must be VOIP_CALL_STATE_DIALING. Otherwise, Call Service Kit considers the parameter invalid and returns error code 1007200001.

  4. If the called party answers the call, the app needs to report the call status VOIP_CALL_STATE_ACTIVE to Call Service Kit. The system will then update the call capsule and starts displaying the call duration.

    The sample code is as follows:

    // ...The app server receives a message indicating that the call is answered.
    let answeredCallId = '123456'; // The value must be the same as the call ID carried in reportOutgoingCall. It is the unique ID of an in-app call.
    
    // Report the call status to Call Service Kit.
    voipCall.reportCallStateChange(answeredCallId, voipCall.VoipCallState.VOIP_CALL_STATE_ACTIVE);
  5. For the outgoing call, the user can also pull down the notification center panel to mute or unmute the call on the live view notification banner.

    The development procedure is the same as that for an incoming call. For details, please refer to incoming call scenario: muting or unmuting a call.

  6. Additionally, the user can pull down the notification center panel to end the call via a tap on the live view banner.

    The development procedure is the same as that for an incoming call. For details, please refer to incoming call scenario: tapping to end a call.

  7. Unsubscribe from voipCallUiEvent after the call is ended.

    The sample code is as follows:

    // Unsubscribe from voipCallUiEvent.
    voipCall.off('voipCallUiEvent', callback => {
      hilog.info(0x0000, 'CallDemo', `Succeeded in unRegistering voipCallUiEvent, callId: ${callback.callId}`);
    });