Use the ApsaraVideo Player SDK for iOS to quickly start video playback.
Prerequisites
You have integrated the ApsaraVideo Player SDK for iOS. For more information, see Integrate the SDK.
Open source demo project
For code examples, see the BasicPlayback module in API-Example. This Objective-C demo project helps you quickly integrate the core playback features of the SDK.
Procedure
Step 1: Create a player
Create an AliPlayer instance.
// Create a player instance.
AliPlayer mAliPlayer = [[AliPlayer alloc] init];
// Create a view container to display the video and set the rendering view for the player.
UIView playerView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:playerView];
mAliPlayer.playerView = playerView;
[mAliPlayer setTraceID:traceId];
Playback quality monitoring, single-point tracking, and video playback statistics all depend on instrumentation log reporting. Playback quality monitoring shows overall playback quality data. Single-point tracking helps you locate a specific user or device, analyze playback behavior, and quickly identify issues.
The available features vary based on the setTraceID parameter:
-
If you do not pass the
setTraceIDparameter (default): The instrumentation log reporting feature is enabled. You can use the playback quality monitoring and video playback statistics features. You cannot use the single-point tracking feature. -
If you pass a traceid to the
setTraceIDparameter: The traceid is a custom value that must be a unique identifier for your user or the user's device, such as your business's user ID, an International Mobile Equipment Identity (IMEI), or an identifier for advertisers (IDFA). After you pass the traceid, the instrumentation log reporting feature is enabled. You can then use the playback quality monitoring, single-point tracking, and video playback statistics features. -
If you set the
setTraceIDparameter toDisableAnalytics: The instrumentation log reporting feature is disabled. You cannot use the playback quality monitoring, single-point tracking, or video playback statistics features.
Step 2: Set the playback source
Playback using VidAuth (Recommended)
AVPVidAuthSource *authSource = [[AVPVidAuthSource alloc] init];
authSource.vid = @"Video ID"; // Required. The video ID.
authSource.playAuth = @"<yourPlayAuth>"; // Required. The playback credential. You must call the GetVideoPlayAuth operation of ApsaraVideo VOD to generate the credential.
authSource.region = @"Region"; // For ApsaraVideo Player SDK V5.5.5.0 and later, this parameter is deprecated. You do not need to set the region because the player automatically parses it. For versions earlier than V5.5.5.0, this parameter is required. It specifies the region where ApsaraVideo VOD is activated. Default value: cn-shanghai.
// authSource.authTimeout = 3600; // The validity period of the playback URL, in seconds. This value overwrites the validity period of URL signing that is set in the ApsaraVideo VOD console. If you do not set this parameter, the default value 3600 is used. If you set this parameter, make sure that its value is greater than the actual video duration to prevent the playback URL from expiring before playback is complete.
// Set the playback source.
[mAliPlayer setAuthSource:authSource];
Playback using VidSts
AVPVidStsSource *source = [[AVPVidStsSource alloc] init];
source.vid = @"Video ID"; // Required. The video ID.
source.region = @"Region"; // Required. The region where ApsaraVideo VOD is activated. Default value: cn-shanghai.
source.securityToken = @"<yourSecurityToken>"; // Required. The Security Token Service (STS) token. You must call the AssumeRole operation of STS to generate the token.
source.accessKeySecret = @"<yourAccessKeySecret>"; // Required. The AccessKey secret of the temporary AccessKey pair. You must call the AssumeRole operation of STS to generate the secret.
source.accessKeyId = @"<yourAccessKeyId>"; // Required. The AccessKey ID of the temporary AccessKey pair. You must call the AssumeRole operation of STS to generate the ID.
// source.authTimeout = 3600; // The validity period of the playback URL, in seconds. This value overwrites the validity period of URL signing that is set in the ApsaraVideo VOD console. If you do not set this parameter, the default value 3600 is used. If you set this parameter, make sure that its value is greater than the actual video duration to prevent the playback URL from expiring before playback is complete.
// If you enable HLS encryption parameter pass-through in the ApsaraVideo VOD console and the default parameter name is MtsHlsUriToken, you must set the config and pass it to the vid.
// Set the playback source.
[mAliPlayer setStsSource:source]
Playback using UrlSource
// Create a playback source object and set the playback URL.
// The playback URL can be a third-party video-on-demand (VOD) URL, a playback URL from ApsaraVideo VOD, or a local video URL.
AVPUrlSource *urlSource = [[AVPUrlSource alloc] urlWithString:@"Playback URL"];
[mAliPlayer setUrlSource:urlSource];
For more information about playback sources, see Basic features.
Step 3: Start playback
// Prepare for playback.
[mAliPlayer prepare];
// After prepare is called, you can synchronously call the start operation. Playback starts automatically after the onPrepared callback is complete.
[mAliPlayer start];
Step 4: Stop playback
// Unbind the player view.
mAliPlayer.playerView = nil;
// Stop playback.
[mAliPlayer stop];
// Release resources synchronously. The stop method is automatically called.
[mAliPlayer destroy];
// Clear the reference to prevent memory leaks.
mAliPlayer = nil;
References
For more information about player features such as playback control and event listeners, see Basic features.