logo
On this page

Playing Stream by URL

2024-05-06

Overview

When the publishing end uses third-party publishing tools (such as OBS software, IP Camera, etc.) to push the stream to CDN, or uses ZEGO SDK to relay the audio and video screen to third-party CDN through the CDN relaying function, you can directly pass in the URL address to play the stream.

Prerequisites

Before implementing the URL stream playing functionality, ensure:

Usage Steps

1 Configure Stream Playing Parameters

To play streams directly through the CDN's URL address, you need to use the ZegoCDNConfig object to fill in the URL parameters. If the corresponding CDN has configured stream playing authentication, you also need to fill in the authentication parameters through the authParam field.

Note
  • The authentication parameter refers to the string after the "?" in the URL (excluding "?"). For example, if the CDN playing stream URL is "rtmp://xxxx.yyy.zzz?a=qqq&b=www", the authentication parameter is "a=qqq&b=www".
  • The authentication parameter in the playing stream URL is mainly used for anti-leeching. For specific rules, please consult your CDN provider or ZEGO Technical Support. If there is no authentication parameter, you can ignore the "authParam" field.
  • Supported playing stream protocols include: RTMP, FLV, HLS (if you need to use HLS protocol for playing stream, please contact ZEGO Technical Support to enable it).
// Set CDN parameters
let config = new ZegoCDNConfig();
// URL is the CDN stream playing address
config.url = "rtmp://xxxxxxxx";
// If authentication is required, set the authentication parameters. If authentication is not required, you can leave it unset (the authentication parameter cannot contain the "?" character)
config.authParam = "xxx";

let playerConfig = { cdnConfig: config }

2 Start Playing Stream

Call the startPlayingStream interface to start playing stream. If an error occurs during stream playing, please refer to Common Error Codes - 1004xxx Stream Playing Related Error Codes.

Warning
  • Before playing stream by URL, ensure that you have logged in to the room.
  • When playing stream by URL, you cannot directly play stream by filling in the stream ID. The actual played stream image is based on the URL.
  • Although the stream ID cannot be used for playing stream at this time, the SDK still uses the stream ID as a unique identifier internally for subsequent stream playing related callbacks. Therefore, the stream ID still needs to be globally unique within the entire AppID.
// Start playing stream
// After filling in the url parameter, the SDK will pull the audio and video stream from the url, but you still need to pass a unique streamID to the SDK, and the SDK will use this streamID to identify this stream internally
ZegoExpressEngine.instance().startPlayingStream("STREAM_ID", playerConfig);

3 Stop Playing Stream

To stop playing stream, call the stopPlayingStream interface.

// The streamID passed when stopping playing stream is the streamID passed when playing stream
ZegoExpressEngine.instance().stopPlayingStream("STREAM_ID");

You can listen to the result of playing stream from CDN through playerStateUpdate.

Warning

If you are not using ZEGO SDK for publishing, but using third-party publishing tools to publish directly, and using ZEGO SDK for playing, in this scenario, the publishing end has not joined the room using ZEGO SDK, and the playing end will not receive the roomStreamUpdate callback by default. You can use the Add Room Stream and Delete Room Stream functions to allow the playing end to receive the roomStreamUpdate callback.

ZegoExpressEngine.instance().on("playerStateUpdate", (streamID, state, errorCode, extendedData) => {
    console.info(`playerStateUpdate:streamID:${streamID}, state:${state}, errorCode:${errorCode}, extendedData:${JSON.stringify(extendedData)}`);

    // After successfully calling the playing stream interface, when the player state changes, such as network interruption causing publishing exceptions, etc., the SDK will notify through this callback while retrying to play stream
    // 1. When receiving this callback notification and state is Playing, it indicates successful stream playing
    // 2. When receiving this callback notification and state is PlayRequesting, it indicates that it may be playing stream or the SDK is retrying to play stream due to network interruption, etc.
    // 3. When receiving this callback notification and state is NoPlay, it indicates stream playing has stopped
    // 4. When playing stream by URL, the stream_id in the callback parameters is the stream ID when calling the playing stream API, used to uniquely identify the current stream playing event.
});

Previous

CDN Stream Publishing Authentication

Next

RTMP Push Stream to ZEGO Server

On this page

Back to top