This guide describes how to quickly integrate the AliPlayerKit SDK for iOS. It covers the complete process, including global initialization, component layer integration, and scene layer integration.
Before you begin, configure your environment and add the required dependencies by following the Integration Preparation guide.
This document's clear structure makes it ideal for AI parsing and execution. You can use it as a Skill to streamline your development workflow. We recommend using AI assistance to integrate AliPlayerKit.
AliPlayerKit features a minimal API that lets you quickly integrate video playback features with minimal code.
Choose an integration solution
AliPlayerKit features a layered architecture. In addition to component layer integration, it also offers scene layer integration, allowing you to choose the option that best fits your business needs:
Integration solution | Module | Description | Use cases |
Component layer integration |
| Provides configurable player UI components using the core PlayerKit module. | For use cases that require a custom player UI or fine-grained control over playback behavior. |
Scene layer integration |
| Provides complete solutions for common playback scenarios, built on the component layer. | Quickly implement standard playback scenarios, such as long-form video, short-form video, and live streaming. |
Since the scene layer depends on the component layer, you must integrate the component layer first.
Global initialization
In the application:didFinishLaunchingWithOptions: method of AppDelegate, call [AliPlayerKit setup] to perform global initialization. This method only needs to be called once during the entire lifecycle of the application.
#import <AliPlayerKit/AliPlayerKit.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Initialize global settings for AliPlayerKit
[AliPlayerKit setup];
return YES;
}
@endimport AliPlayerKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Initialize global settings for AliPlayerKit
AliPlayerKit.setup()
return true
}
}AliPlayerKit also provides other global configuration interfaces, such as the logging system and preloading policies. For more information, see the API Reference.
Option 1: Component layer integration
The component layer (PlayerKit module) provides out-of-the-box, configurable player UI components. Follow these steps to implement playback in each view that requires video.
Step 1: Add the player view
In your ViewController, create an AliPlayerView and add it to your view hierarchy:
AliPlayerView *playerView = [[AliPlayerView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:playerView];let playerView = AliPlayerView(frame: view.bounds)
view.addSubview(playerView)You can place the AliPlayerView anywhere in your view hierarchy and set its frame as needed. If you enable full-screen switching, the component automatically handles view hierarchy adjustments.
Step 2: Bind the controller and play
You must call configure: before attach:, or the playback configuration cannot be retrieved during view binding.
In your ViewController, create the controller, configure the data, and attach the view:
Create
AliPlayerController.Configure
AliPlayerModel(you can use the recommended VidAuth method or the URL source for quick testing).Call
configure:andattach:.In
dealloc, calldestroyto release resources.
#import <AliPlayerKit/AliPlayerKit.h>
@interface VideoPlayerViewController ()
@property (nonatomic, strong) AliPlayerView *playerView;
@property (nonatomic, strong) AliPlayerController *controller;
@end
@implementation VideoPlayerViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1. Create the player view
self.playerView = [[AliPlayerView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.playerView];
// 2. Create the player controller
self.controller = [[AliPlayerController alloc] init];
// 3. Configure the playback data (VidAuth is recommended)
VideoSource *source = [VideoSource vidAuthSourceWithVid:@"your_video_id"
playAuth:@"your_playback_credential"];
AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:source];
model.videoTitle = @"Sample Video";
// 4. Configure the source and attach the view (playback starts automatically after attachment)
[self.controller configure:model];
[self.playerView attach:self.controller];
}
- (void)dealloc {
// Destroy the player to release resources
[self.controller destroy];
}
@end
import AliPlayerKit
class VideoPlayerViewController: UIViewController {
private let playerView = AliPlayerView(frame: .zero)
private let controller = AliPlayerController()
override func viewDidLoad() {
super.viewDidLoad()
// 1. Create the player view
playerView.frame = view.bounds
view.addSubview(playerView)
// 2. Configure the playback data (VidAuth is recommended)
let source = VideoSource.vidAuthSource(withVid: "your_video_id", playAuth: "your_playback_credential")
let model = AliPlayerModel(videoSource: source)
model.videoTitle = "Sample Video"
// 3. Configure the source and attach the view (playback starts automatically after attachment)
controller.configure(model)
playerView.attach(controller)
}
deinit {
// Destroy the player to release resources
controller.destroy()
}
}
If you are temporarily unable to obtain the video ID and playback credential, you can use a URL source for a quick test. The scene module provides built-in SceneConstants sample data, which you can also use directly.
VideoSource *source = [VideoSource urlSourceWithUrl:@"https://example.com/video.mp4"];AliPlayerModel.autoPlaydefaults toYES, which automatically starts playback after configuration is complete. You do not need to manually callplay.AliPlayerController.autoLifecycleManagementisYESby default, which automatically handles pausing and resuming playback when the application switches between the foreground and background. However, for page-level navigation (such as push/pop), inviewDidAppearandviewWillDisappear, you need to manually callonResumeandonPause, respectively.configure:Internally obtains a player instance by using a lifecycle strategy, sets the data source, and prepares for playback.In the
deallocmethod, you must calldestroyto destroy the controller. Otherwise, a memory leak will occur.
Step 3 (Optional): Handle the back button
If you need to customize the player's back button behavior, you can use the onBackPressed callback:
__weak typeof(self) weakSelf = self;
self.playerView.onBackPressed = ^{
[weakSelf.navigationController popViewControllerAnimated:YES];
};playerView.onBackPressed = { [weak self] in
self?.navigationController?.popViewController(animated: true)
}This callback is only triggered when the player is not in full-screen mode. In full-screen mode, the back button exits full-screen instead. If you do not set this callback, the player attempts a default navigation action (pop or dismiss) via the responder chain.
Step 4 (Optional): Custom configuration
To inject custom SDK configurations, such as the buffering strategy and Referer, before the player is ready, use onPlayerConfigBlock:
model.onPlayerConfigBlock = ^(id<MediaPlayer> player) {
// Executed before `prepare`. You can access the underlying player instance for custom configuration.
// For example, set the buffering strategy, Referer, or use `setOption`.
};model.onPlayerConfigBlock = { player in
// Executed before `prepare`. You can access the underlying player instance for custom configuration.
}Global configuration is set once by using [AliPlayerKit setOnGlobalInitBlock:] before setup. Instance configuration is triggered by model.onPlayerConfigBlock each time configure: is called. For more information, see Custom Configurations.
Option 2: Scene layer integration
The scene layer provides complete playback solutions for specific business scenarios, such as long-form video, short-form video, and live streaming. Built on the component layer, these solutions are ready to use out-of-the-box.
Step 1: Select a scene module
Scene modules, located in the PlayerKitScenes directory, are imported via CocoaPods subspecs. The available modules include:
Scene module | Pod subspec | Description |
|
| The required common module for all scenes. It is a base dependency for other scene modules. |
|
| Long-form video scene: Provides a complete solution for long-form video playback. |
|
| Short-form video scene: Provides a complete solution for swipe-to-play short-form video feeds. |
|
| Live streaming scene: Provides a complete solution for live streaming playback. |
|
| Playlist scene: Provides a complete solution for video playlist playback. |
Step 2: Launch the scene page
After integrating a scene module, you can launch the corresponding scene page in one of two ways:
Method 1: Direct instantiation (recommended)
Each scene module provides an independent ViewController, which can be directly instantiated and navigated to via UINavigationController :
#import "SceneLongVideoViewController.h"
// Navigate to the long-form video scene
SceneLongVideoViewController *vc = [[SceneLongVideoViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];// Navigate to the long-form video scene
let vc = SceneLongVideoViewController()
navigationController?.pushViewController(vc, animated: true)The corresponding ViewController for each scene is as follows:
Scene | ViewController |
Long-form video |
|
Short-form video |
|
Live streaming |
|
Playlist |
|
Method 2: Schema routing
The scene module supports starting the corresponding playback scene by using the Schema protocol. The demo project provides the AppSchemaRouter routing utility class, which can automatically discover and navigate to the corresponding scene by using a Schema URL:
#import "AppSchemaRouter.h"
// Navigate to the long-form video scene via schema
[AppSchemaRouter navigateToSchema:@"playerkit://scenes/longvideo" fromViewController:self];// Navigate to the long-form video scene via schema
AppSchemaRouter.navigate(toSchema: "playerkit://scenes/longvideo", from: self)The corresponding schema address for each scene is as follows:
Scene | Schema |
Long-form video |
|
Short-form video |
|
Live streaming |
|
Playlist |
|
ppSchemaRouter is a routing tool provided in the demo project (App module). It enables automatic module discovery based on the AppModuleProtocol protocol. Scene modules use *Module classes (such as SceneLongVideoModule) to register their schemas, titles, and priorities. If you want to use schema routing in your application, you can refer to this implementation or integrate your own routing framework.
Scene implementation
The scene module source code provides best-practice examples for component layer integration. The following sections describe the core implementation patterns for each scene.
Long-form video
SceneLongVideoViewController demonstrates the standard integration process for the component layer:
// Initialize the AliPlayerKit playback components
- (void)setupPlayerKit {
// 1. Create the player component view
self.playerView = [[AliPlayerView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.playerView];
// Optional: Set the back button callback
__weak typeof(self) weakSelf = self;
self.playerView.onBackPressed = ^{
[weakSelf.navigationController popViewControllerAnimated:YES];
};
// 2. Create the player component controller
self.playerController = [[AliPlayerController alloc] init];
// 3. Configure the player component data
VideoSource *source = [VideoSource vidAuthSourceWithVid:@"your_video_id" playAuth:@"your_playback_credential"];
AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:source];
model.videoTitle = @"Long Video";
// 4. Configure the source and attach the controller to the view (playback starts automatically after attachment)
[self.playerController configure:model];
[self.playerView attach:self.playerController];
}Live streaming
SceneLiveViewController demonstrates the usage of the URL source and live streaming scenario type:
VideoSource *source = [VideoSource urlSourceWithUrl:liveUrl];
AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:source];
model.videoTitle = @"Live Stream";
model.sceneType = SceneTypeLive; // Set the scene type to live
[self.playerController configure:model];
[self.playerView attach:self.playerController];SceneType affects the slot visibility configuration and UI behavior. For example, in a live streaming scenario, VOD elements such as the progress bar are automatically hidden. For more information, see Scene Type.
Playlist
ScenePlaylistViewController demonstrates an advanced usage of SingletonLifecycleStrategy and PlayerNotificationDelegate to implement continuous playback:
// Use the singleton lifecycle strategy to reuse the player instance
self.lifecycleStrategy = [SingletonLifecycleStrategy sharedInstance];
self.currentController = [[AliPlayerController alloc] initWithLifecycleStrategy:self.lifecycleStrategy];
// Register a notification for playback completion to trigger continuous playback
[self.currentController addNotificationDelegate:self];
// PlayerNotificationDelegate callback
- (void)playerDidCompletion {
[self playNextVideo]; // The current video finished playing; play the next one automatically
}For list playback scenarios, you can use initWithLifecycleStrategy: to specify a lifecycle strategy and reuse player instances to improve performance. For more information, see Player lifecycle strategy.
Examples and extensions
By following these steps, you have integrated AliPlayerKit and enabled video playback.
For more examples, refer to the following directories:
Module | Description |
| Provides usage examples for common APIs, including the logging system, slot system, notification system, policy system, lifecycle strategy, preloading, video sources, and localization. |
| Provides solutions for typical playback scenarios, such as long-form video, short-form video, live streaming, and playlists. |
The Schema route format for each usage example is playerkit://usages/<module_name>. You can access these examples directly from the homepage of the demo project.