All Products
Search
Document Center

ApsaraVideo VOD:Custom configuration

Last Updated:Sep 09, 2026

Custom configuration is an extension mechanism in AliPlayerKit. AliPlayerKit includes built-in configurations based on best practices that deliver a high-quality experience in most scenarios without extra setup. When you need player capabilities that are not directly exposed through the AliPlayerKit interface, you can use the custom configuration mechanism to call APIs of the underlying AliPlayer SDK directly.

Key concepts

What is custom configuration?

AliPlayerKit is a high-level wrapper for the underlying AliPlayer SDK and includes built-in, best-practice configurations. However, the underlying SDK offers a richer set of capabilities, such as custom buffering strategies, Referer hotlink protection, and HTTP header injection. Not all of these capabilities are directly exposed through AliPlayerKit.

The custom configuration mechanism provides two callback entry points, allowing you to access the underlying objects at key moments and call their native APIs directly:

Level

API

Timing

Typical scenarios

Global configuration

+setOnGlobalInitBlock:

After internal initialization in +setup completes (once only)

Global behaviors, such as log level and HTTP/2 multiplexing

Player configuration

onPlayerConfigBlock

Before prepare each time configure: is called

Instance-specific behaviors, such as buffering strategy, Referer, and HTTP headers

Three-level configuration system

AliPlayerKit's configuration system has three levels:

┌─────────────────────────────────────────────────────────┐
│              1. Global Configuration (OnGlobalInitBlock)              │
│         Executes once at app launch, affecting all player instances                │
├─────────────────────────────────────────────────────────┤
│              2. Model Configuration (AliPlayerModel)                 │
│         Sets playback parameters via properties each time configure: is called               │
├─────────────────────────────────────────────────────────┤
│           3. Player Configuration (onPlayerConfigBlock)               │
│       Provides a callback to the underlying player before prepare each time configure: is called          │
└─────────────────────────────────────────────────────────┘

Global configuration

Use cases

  • Set global options, such as the global log level

  • Enable HTTP/2 multiplexing

  • Configure a global DNS resolution strategy

  • Implement other global behaviors that must take effect before any player instances are created

How it works

  • Register a callback block by using +[AliPlayerKit setOnGlobalInitBlock:].

  • The callback is triggered once after +setup completes its internal initialization.

  • After execution, the system sets the block to nil, and it will not trigger again.

  • At this point, the underlying SDK is ready, and you can safely call global APIs.

Usage

In application:didFinishLaunchingWithOptions:, register the callback before you call +setup:

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 1. Register the global custom configuration callback.
    //    This must be called before setup.
    [AliPlayerKit setOnGlobalInitBlock:^{
        // Example: Enable HTTP/2 multiplexing.
        [AliPlayerFactory setOption:ALLOW_H2_MULTIPLEX value:1];

        // Example: Set the global log level.
        // [AliPlayerFactory setLogLevel:AF_LOG_LEVEL_INFO];
    }];

    // 2. Initialize the SDK. This triggers the callback registered above
    //    after internal initialization is complete.
    [AliPlayerKit setup];

    return YES;
}

Configuration sequence

application:didFinishLaunchingWithOptions:
    │
    ├── [AliPlayerKit setOnGlobalInitBlock:block]   ← Register callback
    │
    └── [AliPlayerKit setup]
            │
            ├── [GlobalManager setup]    ← Initialize underlying SDK
            │
            ├── sInitialized = YES
            │
            └── block()                  ← Trigger global config callback (once only)

Usage notes

Key point

Description

Call order

setOnGlobalInitBlock: must be called before +setup

Execution count

The global configuration is executed only once, and +setup internally uses dispatch_once for protection.

Clear with nil

You can pass  nil  to cancel a registered callback.

Idempotence

+setup is executed only once across multiple calls, and the callback is not triggered after the first call.

Player configuration

Use cases

  • Customize the buffering strategy (for example, maxBufferDuration and highBufferDuration).

  • Set Referer hotlink protection.

  • Configure HTTP headers.

  • Set instance-level options.

  • Access other player interfaces not directly exposed by AliPlayerKit.

How it works

  • Set a callback block on the AliPlayerModel.onPlayerConfigBlock property.

  • The callback is triggered every time [controller configure:model] is called.

  • The callback executes after setDataSource: and before prepare.

  • The callback parameter is the underlying player instance, which conforms to the MediaPlayer protocol.

Usage

When building AliPlayerModel, set onPlayerConfigBlock:

VideoSource *source = [VideoSource urlSourceWithUrl:@"https://example.com/video.mp4"];
AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:source];

// Set the instance-level custom configuration callback
model.onPlayerConfigBlock = ^(id<MediaPlayer> player) {
    // Example 1: Customize the buffering strategy
    AVPConfig *config = [player getConfig];
    config.maxBufferDuration = 50000;       // Max buffer duration: 50s
    config.highBufferDuration = 3000;       // High watermark: 3s
    config.startBufferDuration = 500;       // Start buffer duration: 500ms
    [player setConfig:config];

    // Example 2: Set Referer hotlink protection
    AVPConfig *refConfig = [player getConfig];
    refConfig.referer = @"https://your-domain.com";
    [player setConfig:refConfig];

    // Example 3: Set an instance-level option
    // [player setOption:key value:value];
};

// Configure and play the video
[controller configure:model];

Configuration sequence

[controller configure:model]
    │
    ├── lifecycleStrategy.acquire()             ← Get player instance
    │
    ├── [mediaPlayer setDataSource:model]       ← Configure video source
    │
    ├── model.onPlayerConfigBlock(mediaPlayer)  ← Trigger player config callback
    │
    ├── [strategyManager start/reset]           ← Start strategy system
    │
    └── [mediaPlayer prepare]                   ← Start preparing for playback

Usage notes

Key point

Description

Trigger Frequency

This is triggered every time configure: is called, making it suitable for dynamic configuration scenarios.

Execution Timing

The best time for custom configuration is after setDataSource: and before prepare.

Callback Parameter

The id<MediaPlayer> instance allows you to call all interfaces of the underlying SDK.

Optional Configuration

onPlayerConfigBlock is optional and does not affect normal playback.

Avoid Asynchrony

Avoid time-consuming or asynchronous operations within the callback.

Built-in configuration items

AliPlayerKit global configuration

By using the methods of the AliPlayerKit class, you can directly set the built-in configuration options:

Parameter

Method

Description

Initialization

+setup

Initialize the SDK. dispatch_once ensures idempotence.

Initialization status

+isInitialized

Checks if the SDK has been initialized.

Global config callback

+setOnGlobalInitBlock:

Registers a custom configuration callback that runs after global initialization.

Debug mode

+setDebugModeEnabled: / +isDebugModeEnabled

Enable/Disable debug mode (Default: NO)

Log panel

+setLogPanelEnabled: / +isLogPanelEnabled

Specifies whether to display the log panel in the player view. The default value is NO.

Log level

+setLogLevel: / +logLevel

SDK log output level (default: LogLevelInfo)

Console log

+enableConsoleLog: / +isConsoleLogEnabled

Specifies whether to output logs to the console (Default: YES)

Player factory

+setPlayerFactory: / +playerFactory

Customize the player creation factory. Pass nil to restore the default.

Preloader

+setPreloader: / +preloader

Customize the preloader, or pass nil to restore the default.

Clear caches

+clearCaches

Clear the player's local media cache (must be called after setup )

Language setting

+setLanguageCode:

Manually specify a language code (zh-Hans / en)

Localization

+setLocaleProvider:

Injects a custom localization provider.

System language

+resetLocaleToSystemLanguage

Resets the locale to follow the system's preferred language.

Current language

+currentLanguageCode

Gets the currently active language code.

Version info

+getPlayerKitVersion / +getSdkVersion

Gets the version number of AliPlayerKit or the underlying SDK.

AliPlayerModel player configuration

Instance-level configuration items set by the AliPlayerModel property:

Property

Type

Default

Description

videoSource

VideoSource *

—

Required. The video data source (readonly, set by using initWithVideoSource: )

sceneType

SceneType

SceneTypeVOD

The playback scene, which affects UI slots and gesture behavior.

coverUrl

NSString *

nil

The URL of the video's cover image.

videoTitle

NSString *

nil

The title displayed in the top bar.

autoPlay

BOOL

YES

Specifies whether to start playback automatically after configuration.

traceId

NSString *

nil

A unique identifier from your application for playback quality monitoring.

startTime

NSInteger

0

The start position for playback in milliseconds. Negative values are corrected to 0.

hardwareDecode

BOOL

YES

Specifies whether to enable hardware decoding.

allowScreenSleep

BOOL

NO

Specifies whether to allow the screen to sleep during playback.

disableScreenshot

BOOL

NO

An iOS-specific property to disable screenshots and screen recording.

onPlayerConfigBlock

void (^)(id<MediaPlayer>)

nil

The instance-level custom configuration callback.

AliPlayerController runtime configuration

Configurations that are dynamically adjusted at runtime by AliPlayerController:

Method / property

Type

Description

-setRate:

float

Sets the playback speed. 1.0 is normal, 0.5 is half-speed, and 2.0 is double-speed.

-setLoop:

BOOL

Enables or disables loop playback.

-setMuted:

BOOL

Enables or disables mute.

-setMirrorMode:

MirrorMode

Mirror mode (None / Horizontal / Vertical)

-setScaleMode:

ScaleMode

Scaling mode (FitXY / FitCenter / CenterCrop)

-setRotateMode:

RotateMode

Rotation angle (0 / 90 / 180 / 270 degrees)

-setControlsVisible:

BOOL

Shows or hides the control bar.

controlsVisibilityLocked

BOOL

Control bar visibility lock (if set to YES, setControlsVisible:NO is ignored)

-setFullscreen:

BOOL

Enters or exits fullscreen mode. The transition is animated by default.

-setFullscreen:animated:

BOOL, BOOL

Enters or exits fullscreen mode and allows you to control the animation.

-selectTrack:

TrackQuality *

Switches the track quality.

-snapshot

—

Takes a snapshot of the current video frame.

-toggle

—

Toggles between play and pause states.

-replay

—

Restarts playback from the beginning.

-stop

—

Stops playback.

autoLifecycleManagement

BOOL

Automatic foreground/background pause and resume (Default: YES)

Configuration priority

Priority rules

If the same item is configured at multiple levels, the settings are applied in the following order of precedence:

Player configuration (onPlayerConfigBlock) > AliPlayerModel properties > Global configuration > SDK default values

Configuration scope

Level

Scope

Lifecycle

Global configuration

All player instances

Application-level (executes once)

Model properties

A single player instance

Single configure: call

Player config callback

A single player instance

Triggered each time configure: is run

Controller methods

A single player instance

Takes effect dynamically at runtime

Configuration example

Code example

// ===== Global configuration (in AppDelegate, once only) =====
- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Global custom configuration
    [AliPlayerKit setOnGlobalInitBlock:^{
        // Enable HTTP/2 multiplexing
        [AliPlayerFactory setOption:ALLOW_H2_MULTIPLEX value:1];
    }];

    // Initialize
    [AliPlayerKit setup];

    // Global built-in configuration (must be called after setup)
    [AliPlayerKit setLogLevel:LogLevelWarn];
    [AliPlayerKit enableConsoleLog:YES];

    return YES;
}

// ===== Player configuration (in the player view controller, for each playback) =====
- (void)startPlayback {
    VideoSource *source = [VideoSource urlSourceWithUrl:videoUrl];
    AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:source];

    // Model property configuration
    model.sceneType = SceneTypeVOD;
    model.videoTitle = @"Awesome Video";
    model.coverUrl = coverImageUrl;
    model.autoPlay = YES;
    model.hardwareDecode = YES;
    model.startTime = resumePosition; // Resume from last position

    // Instance-level custom configuration (for the underlying SDK)
    model.onPlayerConfigBlock = ^(id<MediaPlayer> player) {
        AVPConfig *config = [player getConfig];
        config.maxBufferDuration = 50000;
        config.highBufferDuration = 3000;
        config.startBufferDuration = 500;
        config.referer = @"https://your-domain.com";
        [player setConfig:config];
    };

    [_controller configure:model];
}
Note

Log configurations such as setLogLevel: and enableConsoleLog: must be called after setup because they operate on the LogHub singleton instance.

API quick reference

Global configuration API

Method

Description

+[AliPlayerKit setOnGlobalInitBlock:]

Register a global configuration callback. Pass nil to unregister.

+[AliPlayerKit setup]

Triggers initialization and executes the callback.

Player configuration API

Class

Property

Description

AliPlayerModel

onPlayerConfigBlock

Instance-level configuration callback (void (^)(id<MediaPlayer>))

Related files

File

Description

PlayerKit/Source/AliPlayerKit.h

Global Configuration API (OnGlobalInitBlock typedef)

PlayerKit/Source/AliPlayerModel.h

Player configuration entry point (onPlayerConfigBlock)

PlayerKit/Source/AliPlayerController.h

Defines methods for runtime configuration.

PlayerKit/Source/Player/MediaPlayer.h

Defines the protocol for the underlying player, which is the type of the callback parameter.

For a complete list of APIs, see the API Reference.

FAQ

Configuration execution order

Global configuration is executed once at application startup (in +setup ), and player configuration is executed during each configure: call before prepare. The two do not interfere with each other.

Default behavior

AliPlayerKit includes built-in configurations based on best practices, making it suitable for most use cases without requiring custom configuration.

Asynchronous operations in callbacks

This is not recommended. Callbacks are executed synchronously, which means the player will not continue to execute prepare until the callback is complete. If you perform a time-consuming operation in the callback, it will block the player's startup process.

Changing video source in callbacks

This is not recommended. The video source is already set by using setDataSource: before the callback. Modifying it in the callback may cause unexpected behavior. The player configuration callback should only be used to set playback parameters, such as the buffering strategy and Referer.

Calling setOnGlobalInitBlock: after setup

It will not take effect. The +setup method uses dispatch_once to ensure that it is executed only once. If a Block is set after the +setup method is called, the Block will never be triggered.

Log configuration timing

setLogLevel: and enableConsoleLog: must be called after +setup, because they operate directly on the LogHub singleton. However, setOnGlobalInitBlock: must be set before +setup.