Documentation
Low_Latency_Live Live Streaming
Documentation
Demo APP
SDK Center
API Center
FAQ
Code Market
Console
Sign Up
Log In
中文站 English
  • Documentation
  • Live Streaming
  • Upgrade the livestream
  • Advanced features
  • Implement a livestream using frameworks
  • Implement livestream features using Angular

Implement audio and video features using Angular

Last updated:2023-11-28 11:49

Introduction

This document describes how to implement an audio and video call using Angular.

Basic concepts:

  • ZEGO Express SDK: The real-time audio and video SDK developed by ZEGO to help you quickly build high-quality, low-latency, and smooth real-time audio and video communications into your apps across different platforms, with support for massive concurrency.
  • Stream publishing: The process of the client app capturing and transmitting audio and video streams to the ZEGOCLOUD server.
  • Stream playing: The process of the client app receiving and playing audio and video streams from the ZEGOCLOUD server.

Prerequisites

Before you begin, make sure you complete the following steps:

  • A project has been created in ZEGOCLOUD Console and applied for a valid AppID and AppSign. For details, please refer to Console - How to view project information .
  • ZEGO Express SDK has been integrated into the project to implement basic real-time audio and video functions. For details, please refer to Integrate the SDK and Implement a basic video call.

Implementation process

The current project uses Node 14.17.3 and Angular/cli 12.1.4.

The following diagram shows the basic process of User A playing a stream published by User B:

/Pics/QuickStart/implementation_process_web_latesten.png

The following diagram shows the API call sequence of the stream publishing and playing process:

/Pics/QuickStart/web_implem_api_call.png

Create a ZegoExpressEngine instance

1. Optional: Create the UI

Add UI elements

Before creating a ZegoExpressEngine instance, we recommend you add the following UI elements to implement basic real-time audio and video features:

  • A view for local preview
  • A view for remote video
  • An End button
layout

2. Create a ZegoExpressEngine instance

To create a singleton instance of the ZegoExpressEngine class, pass in your AppID as the appID parameter and the Server URL as the server parameter. You can obtain them from the ZEGOCLOUD Admin Console.

Initialize the ZegoExpressEngine instance.

// Initialize the [ZegoExpressEngine] instance.
export class AppComponent {
    zg:any = null;

    createZegoExpressEngine() {
        this.zg = new ZegoExpressEngine(appID, server);
    }
}

3. Optional: Listen for and handle the event callbacks

Listen for and handle the event callbacks

After you create a ZegoExpressEngine instance, you can call the on method to listen for and handle various event callbacks as needed.

The following sample code demonstrates how to listen for and handle the roomStateUpdate callback. For more event callbacks, see ZegoRTCEvent and ZegoRTMEvent.

this.zg.on('roomStateUpdate', (roomID, state, errorCode, extendedData) => {
    if (state == 'DISCONNECTED') {
        // Disconnected from the room.
    // ...
    }

    if (state == 'CONNECTING') {
        // Connecting to the room.
    // ...
    }

    if (state == 'CONNECTED') {
        // Connected to the room.
    // ...
    }
})

Optional: Check your browser's WebRTC support

Check if your browser supports WebRTC

Before starting to publish or play a stream, you can call the checkSystemRequirements method to check if your browser supports WebRTC.

const result = await this.zg.checkSystemRequirements();
// The [result] indicates whether it is compatible. It indicates WebRTC is supported when the [webRTC] is [true]. For more results, see the API documents.
console.log(result);
// {
//   webRTC: true,
//   customCapture: true,
//   camera: true,
//   microphone: true,
//   videoCodec: { H264: true, H265: false, VP8: true, VP9: true },
//   screenSharing: true,
//   errInfo: {}
// }

For more about the parameters of the returned results, refer to the parameter description of the ZegoCapabilityDetection method.

Log in to a room

1. Obtain the login token

When logging in to a room, you need to pass in a login token for user authentication. For information about how to obtain the login token, see User privilege control.

2. Log in

To log in to a room, call the loginRoom with the following parameters:

  • A unique room ID as the roomID parameter
  • The login token you obtained in the previous step as the token parameter
  • The user ID and user name as the roomID and userName parameter
  • Optional: Pass the corresponding object to the config parameter based on the actual situation.
  • To receive event callbacks after logging in to a room, you need to subscribe to event callbacks before the room login.
  • You need to implement your own business logic to set the values of roomID, userID, and userName.
  • Each roomID and userID must be globally unique within the scope of the AppID. We recommend you set the userID to a meaningful value. You can associate userID with the account system of your application.
// Log in to a room. It returns `true` if the login is successful.
// The roomUserUpdate callback is disabled by default. To receive this callback, you must set the `userUpdate` property to `true` when logging in to a room. 
const result = await this.zg.loginRoom(roomID, token, {userID, userName}, {userUpdate: true});

3. Listen for and handle the event callbacks related to room users and streams

To listen for and handle various events that may happen after logging in to a room, you can implement the corresponding event callback methods of the event handler as needed. The following are some common event callbacks related to room users and streams:

  • roomStateUpdate: Callback for updates on current user's room connection status. When the current user's room connection status changes (for example, when the current user is disconnected from the room or login authentication fails), the SDK sends out the event notification through this callback.

  • roomUserUpdate : Callback for updates on the status of other users in the room. When other users join or leave the room, the SDK sends out the event notification through this callback.

  • roomStreamUpdate: Callback for updates on the status of the streams in the room. When new streams are published to the room or existing streams in the room stop, the SDK sends out the event notification through this callback.

  • To receive the roomUserUpdate callback, you must set the isUserStatusNotify property of the room configuration parameter ZegoRoomConfig to true when you call the loginRoom method to log in to a room.
  • To play streams published by other users in the room: you can listen for the roomUserUpdate callback, and when there is a stream added, call the startPlayingStream method to start receiving and playing the newly added stream.
// Callback for updates on the current user's room connection status.
this.zg.on('roomStateUpdate', (roomID,state,errorCode,extendedData) => {
    if (state == 'DISCONNECTED') {
        // Disconnected from the room.
    }

    if (state == 'CONNECTING') {
        // Connecting to the room.
    }

    if (state == 'CONNECTED') {
        // Connected to the room.
    }
})

// Callback for updates on the status of ther users in the room.
this.zg.on('roomUserUpdate', (roomID, updateType, userList) => {
    console.warn(
        `roomUserUpdate: room ${roomID}, user ${updateType === 'ADD' ? 'added' : 'left'} `,
        JSON.stringify(userList),
    );
});

// Callback for updates on the status of the streams in the room.
this.zg.on('roomStreamUpdate', async (roomID, updateType, streamList, extendedData) => {
    if (updateType == 'ADD') {
        // New stream added, start playing the stream.
    } else if (updateType == 'DELETE') {
        // Stream deleted, stop playing the stream.
    }
});

Publish streams

1. Create a stream

a. To create a local audio and video stream, call the createZegoStream method. By default, the engine captures video data from the camera and captures audio data from the microphone.

After calling the createZegoStream method, you need to wait for the ZEGO server to return the media stream object (localStream) before any further operation.

Creating a container

for media streaming playback components on HTML.

<div id="local-video" style="width: 320px;height: 240px;"></div>

Render audio and video by assigning the localstream to the video tag object.

// After calling the createZegoStream method, you need to wait for the ZEGO server to return the local stream object before any further operation.
this.localStream = await this.zg.createZegoStream({
    camera: {
        video: {
          input: videoDeviceID
        },
        audio: {
          input: audioDeviceID
        },
    }
});

b. Optional: Set up the audio/video capturing parameters

Set up the audio/video capturing parameters

If you don't want to use the SDK's default audio/video capturing settings, you configure the following parameters of the createZegoStream method with your own specific settings. For more details, see Custom video capture.

  • camera: Configurations for audio/video capture from the microphone and camera.

  • screen: Configurations for audio/video capture for screen sharing.

  • custom: Configurations for audio/video capture from a third-party source.

2. Start publishing a stream

To start publishing a local audio and video stream to remote users, call the startPublishingStream method with the following parameters:

  • A stream ID as the streamID parameter
  • The media stream object obtained in the previous step as localStream parameter
  • You need to implement your own business logic to set the values of streamID and make sure the streamID is globally unique within the scope of the AppID.
  • To publish multiple streams, call the startPublishingStream method multiple times, making sure that each stream has a unique streamID.
// [localStream] is the MediaStream object created by calling creatStream in the previous step.
this.zg.startPublishingStream(streamID, localStream)

3. Listen for and handle the event callbacks related to stream publishing

To listen for and handle various events that may happen after stream publishing starts, you can implement the corresponding event callback methods of the event handler as needed. The following are some common event callbacks related to stream publishing:

publisherStateUpdate: Callback for updates on stream publishing status. After stream publishing starts, if the status changes, (for example, when the stream publishing is interrupted due to network issues and the SDK retries to start publishing the stream again), the SDK sends out the event notification through this callback.

publishQualityUpdate: Callback for reporting stream publishing quality. After stream publishing starts, the SDK sends out the streaming quality data (resolution, frame rate, bit rate, etc.) regularly through this callback.

this.zg.on('publisherStateUpdate', result => {
    // Callback for updates on stream publishing status.
    // ... 
})

this.zg.on('publishQualityUpdate', (streamID, stats) => {
    // Callback for reporting stream publishing quality.
    // ... 
})

Play streams

1. Start playing a stream

To start playing a remote audio and video stream from the ZEGO server, call the startPlayingStream method with the corresponding stream ID passed to the streamID parameter.

You can obtain the stream ID of the streams published by remote users from the callback roomStreamUpdate.

Creating a container

for media streaming playback components on HTML.

<div id="remote-video" style="width: 320px;height: 240px;"></div>

Render the remote streams by assigning the remoteStream to the tag object.

this.remoteStream = await this.zg.startPlayingStream(streamID,options);

const remoteView = this.zg.createRemoteStreamView(this.remoteStream);
remoteView.play("remote-video", {enableAutoplayDialog:true});
  • Each streamID must be globally unique within the AppID.

2. Listen for and handle the event callbacks related to stream playing

To listen for and handle various events that may happen after stream playing starts, you can implement the corresponding event callback methods of the event handler as needed. The following are some common event callbacks related to stream playing:

playerStateUpdate: Callback for updates on stream playing status. After stream playing starts, if the status changes (for example, when the stream playing is interrupted due to network issues and the SDK retries to start playing the stream again), the SDK sends out the event notification through this callback.

playQualityUpdate: Callback for reporting stream playing quality. After stream playing starts, the SDK sends out the streaming quality data (resolution, frame rate, bit rate, etc.) regularly through this callback.

this.zg.on('playerStateUpdate', result => {
    // Callback for updates on stream playing status.
    // ...
})

this.zg.on('playQualityUpdate', (streamID,stats) => {
    // Callback for reporting stream playing quality.
})

Test out real-time audio and video features

We recommend you run your project on a real device. If your app runs successfully, you should hear the sound and see the video captured locally from your device.

To test out the real-time audio and video features, visit the ZEGO Express Web Demo, and enter the same AppID, Server and RoomID to join the same room. If it runs successfully, you should be able to view the video from both the local side and the remote side, and hear the sound from both sides as well.

In audio-only scenarios, no video will be captured and displayed.

Stop publishing and playing streams

1. Stop publishing a stream

To stop publishing a local audio and video stream to remote users, call the stopPublishingStream method with the corresponding stream ID passed to the streamID parameter.

this.zg.stopPublishingStream(streamID)

2. Destroy a local media stream

To destroy a local media stream, call the destroyStream method.

To stop the local video preview, you need to manually destroy the video element after destroying the local media stream.

// localStream is the MediaStream object created when calling the createZegoStream method.
this.zg.destroyStream(localStream)

3. Stop playing a stream

To stop playing a remote audio and video stream, call the stopPlayingStream method with the corresponding stream ID passed to the streamID parameter.

this.zg.stopPlayingStream(streamID)

Log out of a room

To log out of a room, call the logoutRoom method with the corresponding room ID passed to the roomID parameter.

this.zg.logoutRoom(roomID)
Page Directory
  • Free trial
  • 提交工单
    咨询集成、功能及报价等问题
    电话咨询
    400 1006 604
    Get Consulting
    Scan Wechat QR code