Multiple video source support
A video source is the fundamental data object in AliPlayerKit. It defines various source types and supports three playback methods—VidAuth, VidSts, and URL—to unify the configuration and management of video resources.
Key concepts
What is a video source?
A video source specifies the origin of a video and the authorization method required for playback.
AliPlayerKit uses the VideoSource class to encapsulate three types of video sources:
Video source type | Authorization method | Security level | Use case |
VidAuth | VID + PlayAuth | Medium | Recommended for most production environments. |
VidSts | VID + STS temporary credential | High | High-security scenarios |
URL | Direct URL | Low | Public resources, testing, and demos |
How to choose a video source type
Requirement | Recommended type | Description |
Requires authorization with standard security needs | VidAuth (Recommended) | Simple authorization mechanism, easy to integrate. |
Requires high security and temporary access credentials | VidSts | Uses temporary credentials and supports fine-grained access control. |
Public video resources | URL | No authorization needed, simple to use. |
Design features
Immutable object: All properties of a
VideoSourceobject are read-only and cannot be modified after creation.Factory method creation: The
initandnewmethods are marked asNS_UNAVAILABLE. Use the provided class factory methods to create instances.Parameter validation: Factory methods validate that required parameters are not
nilor empty strings. If validation fails, they returnnil.Validity check: The
isValidmethod lets you perform a secondary check before configuring the player.Unique identifier: The
uniqueIdproperty is automatically derived based on the source type. It returns thevidfor VidAuth and VidSts sources, and theurlfor URL sources. The player uses this ID to reuse instances during lifecycle management.
Video source types
VidAuth mode (Recommended)
This mode authorizes playback by using a video ID (VID) and a PlayAuth. Its simple authorization and easy integration make it the recommended choice for most production environments.
Use cases:
Video resources that require authorization.
Video playback in most production environments.
Scenarios that require a simple and secure authorization method.
Parameters:
Parameter | Type | Required | Description |
|
| Yes | The unique identifier for the video. Cannot be |
|
| Yes | The playback authorization code. Obtain this from your server. Cannot be |
Example:
VideoSource *videoSource =
[VideoSource vidAuthSourceWithVid:@"your_video_id"
playAuth:@"your_play_auth"];
AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:videoSource];
model.sceneType = SceneTypeVOD;
model.autoPlay = YES;VidSts mode
This mode uses a video ID (VID) and an Alibaba Cloud Security Token Service (STS) temporary credential to authorize playback. It offers a higher level of security and access control.
Use cases:
Scenarios that require temporary access credentials.
Video resources with high security requirements.
Scenarios that require fine-grained access control.
Parameters:
Parameter | Type | Required | Description |
|
| Yes | The unique identifier for the video. Cannot be |
|
| Yes | The STS access key ID. Cannot be |
|
| Yes | The STS access key secret. Cannot be |
|
| Yes | The STS security token. Cannot be |
|
| No | The region information, such as |
Example:
VideoSource *videoSource =
[VideoSource vidStsSourceWithVid:@"your_video_id"
accessKeyId:@"your_access_key_id"
accessKeySecret:@"your_access_key_secret"
securityToken:@"your_security_token"
region:@"cn-shanghai"];
AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:videoSource];
model.sceneType = SceneTypeVOD;
model.autoPlay = YES;URL mode
This mode plays a video directly from a URL, making it suitable for publicly accessible video resources.
Use cases:
Public video resources that do not require authorization.
Testing and demo scenarios.
Playback of a live stream.
Parameters:
Parameter | Type | Required | Description |
|
| Yes | The URL of the video. Cannot be |
Example:
VideoSource *videoSource =
[VideoSource urlSourceWithUrl:@"https://example.com/video.mp4"];
AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:videoSource];Comparison of video source types
Feature | VidAuth | VidSts | URL |
Authorization method | PlayAuth | STS temporary credential | No authorization required |
Required parameters | vid, playAuth | vid, accessKeyId, accessKeySecret, securityToken | url |
Optional parameters | — | region | — |
Security level | Medium | High | Low |
Use case | Most production environments | High-security scenarios | Public resources / live streams |
Recommendation level | Recommended | For special requirements | Testing / Demos |
Source of |
|
|
|
Usage
Basic usage: Create a source
Create a video source by using the VideoSource factory class method:
// VidAuth (Recommended)
VideoSource *authSource =
[VideoSource vidAuthSourceWithVid:@"your_video_id"
playAuth:@"your_play_auth"];
// VidSts
VideoSource *stsSource =
[VideoSource vidStsSourceWithVid:@"your_video_id"
accessKeyId:@"your_access_key_id"
accessKeySecret:@"your_access_key_secret"
securityToken:@"your_security_token"
region:@"cn-shanghai"];
// URL
VideoSource *urlSource =
[VideoSource urlSourceWithUrl:@"https://example.com/video.mp4"];If a required parameter is nil or an empty string, the factory method returns nil. The caller should check the return value.
Basic usage: Validate the source
Before you configure the player, call isValid to validate the video source configuration:
VideoSource *source =
[VideoSource vidAuthSourceWithVid:vid playAuth:playAuth];
if (source && [source isValid]) {
AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:source];
[self.controller configure:model];
} else {
NSLog(@"Invalid video source configuration: %@", source);
}Validation rules for isValid:
Type | Validation condition |
VidAuth |
|
VidSts |
|
URL |
|
The VidSts region is an optional parameter and is not included in the isValid validation.
Basic usage: End-to-end playback flow
@interface PlayerViewController ()
@property (nonatomic, strong) AliPlayerView *playerView;
@property (nonatomic, strong) AliPlayerController *playerController;
@end
@implementation PlayerViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1. Create a video source. We recommend fetching the PlayAuth from your server.
VideoSource *videoSource =
[VideoSource vidAuthSourceWithVid:@"your_video_id"
playAuth:@"server_returned_play_auth"];
// 2. Validate the video source.
if (!videoSource || ![videoSource isValid]) {
NSLog(@"Invalid video source configuration: %@", videoSource);
return;
}
// 3. Create the player model.
AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:videoSource];
model.sceneType = SceneTypeVOD;
model.coverUrl = @"https://example.com/cover.jpg";
model.autoPlay = YES;
// 4. Create and configure the controller.
self.playerController = [[AliPlayerController alloc] init];
[self.playerController configure:model];
// 5. Attach the controller to the player view.
[self.playerView attach:self.playerController];
}
- (void)dealloc {
[_playerView detach];
[_playerController destroy];
}
@endAdvanced usage: Switch video sources
To switch between different video sources on the same controller, simply use a new AliPlayerModel to recall configure::
- (void)switchToNewVideo:(NSString *)newVid playAuth:(NSString *)newPlayAuth {
VideoSource *newSource =
[VideoSource vidAuthSourceWithVid:newVid playAuth:newPlayAuth];
AliPlayerModel *newModel = [[AliPlayerModel alloc] initWithVideoSource:newSource];
newModel.videoTitle = @"New Video";
// Re-configure the same controller.
[self.playerController configure:newModel];
}Best practices
Security recommendations
Recommendation | Description |
Do not hard-code sensitive information | Avoid hard-coding |
Fetch credentials from your server | Obtain authorization information from a server-side endpoint and pass it to the client. |
Refresh credentials regularly | VidSts tokens have a limited lifetime and must be refreshed. |
Use HTTPS | Ensure data is transmitted securely. |
// Recommended: Fetch credentials from a server.
- (void)playVideoWithId:(NSString *)videoId {
[self.apiService fetchPlayAuthForVideoId:videoId
completion:^(NSString *playAuth, NSError *error) {
if (error) {
NSLog(@"Failed to get PlayAuth: %@", error);
return;
}
VideoSource *source =
[VideoSource vidAuthSourceWithVid:videoId playAuth:playAuth];
AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:source];
[self.playerController configure:model];
}];
}Common mistakes
Mistake 1: Not checking the return value of a factory method
// ✗ Incorrect: The factory method can return nil, but its return value is not checked.
VideoSource *source = [VideoSource vidAuthSourceWithVid:vid playAuth:playAuth];
AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:source]; // `source` can be nil.
// ✓ Correct: Check the return value.
VideoSource *source = [VideoSource vidAuthSourceWithVid:vid playAuth:playAuth];
if (source && [source isValid]) {
AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:source];
[self.playerController configure:model];
}Mistake 2: Hard-coding sensitive information on the client
// ✗ Incorrect: `playAuth` is hard-coded, which is a security risk.
VideoSource *source =
[VideoSource vidAuthSourceWithVid:@"video_id"
playAuth:@"hardcoded_play_auth_value"];
// ✓ Correct: Fetch from your server.
NSString *playAuth = [self fetchPlayAuthFromServer:videoId];
VideoSource *source =
[VideoSource vidAuthSourceWithVid:videoId playAuth:playAuth];Mistake 3: Not handling expired STS security tokens
// ✗ Incorrect: Using an expired token.
VideoSource *source = [VideoSource vidStsSourceWithVid:vid
accessKeyId:accessKeyId
accessKeySecret:accessKeySecret
securityToken:expiredToken
region:region];
// ✓ Correct: Check the token and refresh it if needed.
if ([self isTokenExpired:securityToken]) {
securityToken = [self refreshSecurityToken];
}
VideoSource *source = [VideoSource vidStsSourceWithVid:vid
accessKeyId:accessKeyId
accessKeySecret:accessKeySecret
securityToken:securityToken
region:region];Mistake 4: Creating an instance with init
// ✗ Compile error: `init` and `new` are marked as NS_UNAVAILABLE.
VideoSource *source = [[VideoSource alloc] init];
// ✓ Correct: Use a class factory method.
VideoSource *source = [VideoSource urlSourceWithUrl:@"https://example.com/video.mp4"];API reference
VideoSourceType enum
Enum value | Value | Description |
| 0 | Video source using a VID and PlayAuth. (Recommended) |
| 1 | Video source using a VID and an STS temporary credential. |
| 2 | Video source using a direct URL. |
VideoSource properties
Property | Type | Description |
|
| Video source type ( |
|
| Unique identifier: VidAuth/VidSts returns |
|
| The URL of the video, which is valid only for the URL type ( |
|
| The unique identifier for the video ( |
|
| The playback authorization code ( |
|
| The STS Access Key ID is valid only for the VidSts type ( |
|
| STS access key secret, valid only for the VidSts type ( |
|
| The STS security token ( |
|
| Region information, valid only for the VidSts type ( |
VideoSource methods
Method | Return type | Description |
|
| Creates a VidAuth source (returns |
|
| Create a VidSts source (returns |
|
| Create a URL source (returns |
|
| Validates the video source's configuration. |
+new and -init are marked as NS_UNAVAILABLE, so you must create instances using a factory class method.
Related files
File | Description |
| Defines the |
| Implements |
| Playback data model (receives a video source through |
For complete API details, see the API reference.
Example
The PlayerKitUsages/UsageVideoSource module provides a complete example of how to use video sources, covering the creation, parameter validation, and playback processes for three types of video sources.