AliPlayerKit includes built-in, best-practice configurations that provide a high-quality experience out-of-the-box. If you need a feature not directly exposed through the AliPlayerKit APIs, you can use the custom configuration mechanism to call the underlying AliPlayer SDK's native APIs directly.
Configuration overview
AliPlayerKit provides two levels of custom configuration to support different granularities:
Level | API | When to call | Typical scenarios |
Global configuration |
| After global initialization is complete (once only) | Global behaviors such as |
Instance configuration |
| For each player instance, before the | Instance-level behaviors such as |
The underlying AliPlayer SDK supports both global and instance configurations. While AliPlayerKit comes with built-in best practices, it also provides the two preceding callbacks for native APIs that are not directly exposed. This lets you get the player instance and call its methods directly.
Global configuration
Use cases
Set global options, such as the global log level.
Enable HTTP/2 multiplexing.
Configure global behaviors that must apply before any player instance is created.
How it works
Register the callback by using
AliPlayerKit.setOnGlobalInit().The callback is triggered after the internal global initialization in
AliPlayerKit.init()is complete. It is executed only once.At this point, the underlying SDK is ready, and you can safely call global APIs.
Usage
In Application.onCreate(), register the callback before calling AliPlayerKit.init():
public class MyApplication extends Application {
@Overridepublic void onCreate() {
super.onCreate();
// 1. Register the global custom configuration callback (call before init).
AliPlayerKit.setOnGlobalInit(() -> {
// Example: Enable HTTP/2 multiplexing.
AliPlayerFactory.setOption(
AliPlayerGlobalSettings.ENABLE_H2_MULTIPLEX, 1
);
// Example: Set the global log level.
// AliPlayerFactory.setOption(...);
});
// 2. Initialize the SDK (this triggers the registered callback internally after initialization is complete).
AliPlayerKit.init(this);
}
}
Usage notes
Point | Description |
Call order |
|
Execution count | The global configuration is executed only once, and subsequent repeated calls to |
Pass null to cancel | You can pass |
Instance configuration
Use cases
Custom buffer policy (
maxBufferDuration,highBufferDuration, etc.).Set the
Refererfor hotlink protection.Configure HTTP headers.
Set instance-level options.
Use other player APIs not directly exposed by AliPlayerKit.
How it works
Use
AliPlayerModel.Builder.onPlayerConfig()to pass in the callback.Triggered each time
controller.configure(model)is called.The callback is executed after
setDataSource()and beforeprepare().The callback parameter is an
IMediaPlayerinstance, which allows you to access all configuration capabilities of the underlying SDK.
Usage
When building the AliPlayerModel, pass the callback by using the onPlayerConfig() method:
AliPlayerModel model = new AliPlayerModel.Builder()
.videoSource(videoSource)
.onPlayerConfig(player -> {
// Example 1: Customize buffering strategy and Referer
PlayerConfig config = player.getPlayConfig();
config.mMaxBufferDuration = 50000; // Max buffer duration: 50s
config.mHighBufferDuration = 3000; // High-water mark buffer: 3s
config.mStartBufferDuration = 500; // Startup buffer duration: 500ms
config.mReferrer = "https://your-domain.com"; // Set Referer for hotlink protection
player.setPlayConfig(config);
// Example 2: Set an instance-level option
player.setOption(AliPlayerGlobalSettings.ALLOW_PRE_RENDER, 1);
})
.build();
// Configure and start playback
controller.configure(model);
Usage notes
Point | Description |
Triggering frequency | Each call to |
Execution timing | The callback is executed after |
Callback parameters | The |
Optional callback |
|
Configuration sequence
Global configuration sequence
Application.onCreate()
│
├── AliPlayerKit.setOnGlobalInit(callback) ← Register callback
│
└── AliPlayerKit.init(context)
│
├── GlobalManager.initialize() ← Underlying SDK initialization
│
└── callback.onGlobalInit() ← Trigger global configuration callback (once only)
Instance configuration sequence
controller.configure(model)
│
├── lifecycleStrategy.acquire() ← Acquire player instance
│
├── player.setDataSource(model) ← Configure video source
│
├── onPlayerConfig.onPlayerConfig(player) ← Trigger instance configuration callback
│
└── player.prepare() / player.start() ← Start playback
Complete lifecycle
┌──────────────────────────────────────────────────────────────────┐
│ Application Startup │
│ │
│ setOnGlobalInit(callback) ──→ init(context) │
│ │ │
│ GlobalManager │
│ │ │
│ callback.onGlobalInit() ← global configuration │
└──────────────────────────────────────────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────────────────┐
│ Player Instance Creation │
│ │
│ AliPlayerModel.Builder() │
│ .videoSource(source) │
│ .onPlayerConfig(callback) ← Register instance configuration callback │
│ .build() │
│ │
│ controller.configure(model) │
│ │ │
│ ├── setDataSource(model) │
│ ├── callback.onPlayerConfig(player) ← instance configuration │
│ └── prepare() / start() │
└──────────────────────────────────────────────────────────────────┘API reference
Core interfaces
OnGlobalInitCallback
A callback interface for custom configuration after global initialization is complete.
@FunctionalInterfacepublic interface OnGlobalInitCallback {
/**
* Configuration callback after global initialization is complete.
*/void onGlobalInit();
}
Method | Description |
| Triggered after global initialization is complete. Executes only once. |
OnPlayerConfigCallback
A callback interface for custom, instance-level player configuration.
@FunctionalInterfacepublic interface OnPlayerConfigCallback {
/**
* Player custom configuration callback, called before prepare().
*
* @param player The player instance. Use this to access the underlying SDK's configuration capabilities.
*/void onPlayerConfig(@NonNull IMediaPlayer player);
}
Method | Description |
| The parameter for the player configuration callback is an |
Registration methods
Class | Method | Description |
|
| Register a global configuration callback. Pass |
|
| Sets the instance-level configuration callback. |
Related files
File | Description |
| Defines the global configuration callback interface. |
| Defines the instance configuration callback interface. |
| Global configuration registration entry point ( |
| Registration entry point for the instance configuration (Builder's |
| Configure the execution logic (call the callback in the |
FAQ
Global and instance configuration order
The global configuration (setOnGlobalInit) is executed once when the application starts. The instance configuration (onPlayerConfig) is executed for each player instance before prepare(). These two configurations do not affect each other.
Omitting custom configurations
AliPlayerKit includes built-in best practices, so you can use it without any custom configuration in most scenarios.
Asynchronous operations in instance callback
This is not recommended. On Android, the callback is executed synchronously, and the execution of prepare() resumes only after 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 instance callback
This is not recommended. The video source is already set by using setDataSource() before the callback. Modifying the video source in the callback may cause unexpected behavior. The instance configuration callback should be used only to set playback parameters, such as the buffering policy and Referer.