logo
Video Call
On this page

Playing Stream by URL

2024-05-06

Feature Introduction

When the publishing end uses third-party publishing tools (such as OBS software, network camera IP Camera, etc.) to publish the stream to a CDN, or uses the ZEGO SDK's forward-to-CDN feature to push audio and video content to a third-party CDN, you can directly pass in the URL address to play the stream.

Note

If you need to switch from the CDN playing stream scenario to the co-hosting scenario, please refer to the Switching from CDN Playing Stream to Co-hosting Scenario document.

Prerequisites

Before implementing the URL playing stream feature, please ensure:

Usage Steps

1 Configure playing stream parameters

To play stream directly through the CDN's URL address, you need to use the ZegoCDNConfig object to fill in the URL parameter. If the corresponding CDN has configured playing stream 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).
ZegoPlayerConfig *playerConfig = [[ZegoPlayerConfig alloc] init];
ZegoCDNConfig *cdnConfig = [[ZegoCDNConfig alloc] init];
// url is the cdn playing stream address
cdnConfig.url = @"rtmp:\\xxxxxxx";
// If authentication is required, set authentication parameters
cdnConfig.auth = @"xxxx";
playerConfig.cdnConfig = cdnConfig;

2 Start playing stream

Start playing stream by calling the startPlayingStream interface.

When playing stream, if an error occurs, please refer to Common Error Codes - 1004xxx Playing Stream Related Error Codes.

Warning
  • Before playing stream by URL, ensure you have logged in to the room.
  • When playing stream by URL, you cannot play stream by directly filling in the stream ID. The actual playing stream content 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, used in subsequent playing stream related callbacks. Therefore, the stream ID still needs to be globally unique within the entire AppID.
// Start playing stream, set the remote playing stream rendering view canvas, use SDK default mode for view mode, aspect ratio scaling to fill the entire View
// After filling in the url parameter, the sdk will pull audio and video streams from the url, but still needs to pass a unique streamID to the SDK, the SDK will use this streamID to identify this stream internally
[[ZegoExpressEngine sharedEngine] startPlayingStream:streamID canvas:[ZegoCanvas canvasWithView:self.remoteUserView] config:playerConfig];

3 Stop playing stream

To stop playing stream, call the stopPlayingStream interface.

// Stop playing stream
// The streamID passed when stopping playing stream is the streamID passed when playing stream
[[ZegoExpressEngine sharedEngine] stopPlayingStream:@"streamID"];

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

Warning

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

// Notification of user playing audio and video stream status
// When the user's playing audio and video stream status changes, this callback will be received. If the network is interrupted and causes playing stream abnormality, the SDK will automatically retry.
- (void)onPlayerStateUpdate:(ZegoPlayerState)state errorCode:(int)errorCode extendedData:(NSDictionary *)extendedData streamID:(NSString *)streamID {
    if (errorCode != 0) {
        NSLog(@"Playing stream status error streamID: %@, errorCode:%d", streamID, errorCode);
    } else {
        switch (state) {
            case ZegoPlayerStatePlaying:
                NSLog(@"Playing stream in progress");
                break;
            case ZegoPlayerStatePlayRequesting:
                NSLog(@"Requesting to play stream");
                break;
            case ZegoPlayerStateNoPlay:
                NSLog(@"Not playing stream");
                break;
        }
    }
}

Previous

CDN Stream Publishing Authentication

Next

Ultra Low Latency Live Streaming

On this page

Back to top