跳转至
400-881-9892

文档中心

官方文档,可查阅产品介绍、快速入门、用户指南、开发指南、API参考、SDK参考、帮助等信息。

文档中心 互动课堂

iOS 集成方法

1 前言

在您阅读此文档时,我们假定您已经具备了基础的 iOS 应用开发经验,并能够理解相关基础概念。

1.1 环境准备

  • 请到拓课官方网站下载 TKPlayer 版。

1.2 版本兼容

  • 兼容 iOS 10 及以上版本。

2 工程设置

2.1 Xcode Version 13+

2.2 导入 Framework: - 1.Resources 文件夹 - 2.TKPlayer.framework

2.3 Info.plist 文件的设置 添加权限申请及相应文本

1
2
3
Privacy - Photo Library Additions Usage Description 
Privacy - Photo Library Usage Description
Privacy - Location When In Use Usage Description

2.4 http 请求

1
App Transport Security Settings Allow Arbitrary Loads YES

2.5 方向配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<key>UISupportedInterfaceOrientations</key>
    <array>
         <string>UIInterfaceOrientationPortrait</string>
         <string>UIInterfaceOrientationLandscapeLeft</string>
         <string>UIInterfaceOrientationLandscapeRight</string>
     </array>
     <key>UISupportedInterfaceOrientations~ipad</key>
     <array>
         <string>UIInterfaceOrientationLandscapeRight</string>
         <string>UIInterfaceOrientationLandscapeLeft</string>
     </array>

2.6 后台权限

1
 勾选 Backgrounds Mode -> Audio,AirPlay and Picture in pictue

2.7 三方库

1
2
3
4
5
6
7
8
9
  pod 'Masonry'
  pod 'SDWebImage'
  pod 'Bugly'
  pod 'IQKeyboardManager'
  pod 'MRDLNA'
  pod 'TXLiteAVSDK_Player'
  pod 'AFNetworking'
  pod 'YYModel'
  pod "GCDWebServer", "~> 3.0"

3 集成类型

视频view类型(UIView)

3.1 引入头文件

1
#import "TKPlayer/TKPlayer.h"

3.2 回调方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/// 播放器进度状态回调
/// - Parameters:
///   - currentValue: 当前播放进度 单位秒
///   - duration: 传入的总时长进度
///   - playableDuration: 腾讯解析的总时长
///   - state: 播放器状态
- (void)playerCurrentValue:(float)currentValue duration:(float)duration playableDuration:(float)playableDuration state:(SuperPlayerState)state;

// 回调退出事件
@optional
- (void)playerBack;

/// 回调全屏事件
/// - Parameter isFull: YES 全屏 NO:默认屏幕(iPhone 竖屏 ipad 默认屏幕)
@optional
- (void)playerIsFullScreen:(BOOL)isFull;

// 回调触屏事件
@optional
- (void)playerTapScreen;

// 回调播放完成事件
@optional
- (void)playerFinish;
3.2.1 画中画回调
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
@protocol TKPictureDelegate <NSObject>
//即将开始
- (void)tkPictureInPictureWillStart;
//已经开始
- (void)tkPictureInPictureDidStart;
//即将停止
- (void)tkPictureInPictureWillStop;
//已经停止
- (void)tkPictureInPictureDidStop;
//画中画开始失败
- (void)tkPictureInPictureStartPictureInPictureWithError:(NSError *)error;
//画中画停止后恢复界面
- (void)tkPictureInPictureRestore;
// 画中画播放回调时间
- (void)tkPictureInPicturePlayCurrentTime:(NSInteger)currentTime;

3.3 TKPlayerConfig配置文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
///企业id 用于日志上报  这个一定要填写
@property (nonatomic, copy) NSString *  company_id;
///教室号  专用于正保 一定要填写
@property (nonatomic, copy) NSString *classRommNum;
///视频标题 专用于正保 一定要填写
@property (nonatomic, copy) NSString *recordTitle;
///用户id 用于日志上报 这个一定要填写
@property (nonatomic, copy) NSString *user_id;

///播放列表id; 获取播放列表id
@property (nonatomic,  copy) NSString *  package_id; 
// 播放器id 用户播放器设置
@property (nonatomic, copy) NSString *  player_id;
// 媒资id 视频id
@property (nonatomic,copy) NSString  * assetId ;

// 用户昵称
@property (nonatomic,strong) NSString * nickName;

///播放路径
@property (nonatomic, copy) NSString *playPath;

@property (nonatomic, assign) TKPlayUIType playUIType;

@property (nonatomic,assign) TKPlayerType playerType ;

// 新增配置项
@property (nonatomic,assign) BOOL isNeedUploadLog ; // 是否需要上传日志
@property (nonatomic,assign) BOOL isNeedPromote ; // 是否需要推广数据
@property (nonatomic,assign) BOOL isNeedVideoDot ; // 是否需要视频打点数据
@property (nonatomic,assign) BOOL isNeedProblem ; // 是否需要问答数据
@property (nonatomic,assign) BOOL isNeedSubtitles ; // 是否需要字幕数据
@property (nonatomic,assign) BOOL isNeedVideoBarrage ; // 是否需要弹幕数据
@property (nonatomic,assign) BOOL isNeedAirPlay ; // 是否需要投屏
@property (nonatomic,assign) BOOL isNeedPicInPic ; // 是否需要画中画

@property (nonatomic,assign) BOOL isPrivatePlayerSet ; // 使用私有配置 该配置项优先
@property (nonatomic,assign) NSInteger mkLastPlayTime ; // 上次的播放结束时间 本次播放起始时间 单位 秒(s)
@property (nonatomic,assign) NSInteger mkLessonStudyStatus ; // 小节状态 0未开始1进行中2已完成
@property (nonatomic,assign) BOOL mkMarquee ; // 开启防录屏跑马灯
@property (nonatomic,assign) BOOL mkSpeedPlay ; // 开启倍数播放
@property (nonatomic,assign) BOOL mkSectionNoSpeedPlay  ;//小节未学完禁止倍速播放
@property (nonatomic,assign) BOOL mkSectionNoDrag ; // 小节未学完禁止拖动进度条
@property (nonatomic,assign) BOOL mkIsShowBarrage ; // 门课是否显示弹幕
@property (nonatomic,assign) BOOL isUseMemberLanguage  ; // 是否使用宿主语言包
/**
 目前只支持中文和英文
 英文:@"en-us"
 中文:@"zh-cn";
 繁体中文:@"zh-tw";
 韩文:@"ko";
 日语:@"ja";
 法语:@"fr";
 印度尼西亚语:@"id";
 西班牙语: @"es";
 越南语:@"vi";
 */
@property (nonatomic,strong) NSString  * mkNetLanguageCode ; // 传递需要的语言包code

/**
 TKEnvironmentTypeGlobal = 0,
 TKEnvironmentTypeDemo, // 1
 TKEnvironmentTypeTesting // 2
 */
@property (nonatomic,assign) TKEnvironmentType   environmentType ; // 项目环境

3.4 示例代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
- (void)viewDidLoad {
    [super viewDidLoad];
    self.config = [[TKPlayerConfig alloc] init];
    self.config.company_id = @""; // 使用自己的企业id
    self.config.assetId = @"";// 媒资id
    self.config.user_id = @""; // 用户id
    self.config.nickName = @"昵称"; 
    self.config.playerType = TKPlayerPureVideo;
    self.config.playUIType = TKPlayUIBlack;
//    先判断倍速是否开启显示,在判断倍速是否可以操作切换
    self.config.isPrivatePlayerSet = YES;
    self.config.isNeedUploadLog = NO;
    self.config.mkMarquee = YES;
    self.config.mkSpeedPlay = YES;
    self.config.mkLastPlayTime = 0;
    self.config.mkNetLanguageCode = @"zh-tw";    
    if (IS_IPAD) {
        self.iPadSpace = 10;
        self.iPadContentH = SCREENH - TKDBStatusHeight - self.iPadSpace * 2;
        self.ipadPlayWidth =  self.iPadContentH/2*16/9;
        self.view.backgroundColor =  UIColorRGB(0xE0EAFF);
        [self setiPadUpUI];
    }else{
        [self addOrientationNoti];
        self.iPhoneTop = TK_StatusBar_Height + SCREENW * 9/16 + 44;
        self.view.backgroundColor = [UIColor whiteColor];
        [self setiPhoneUpUI];   
    }

}

// ipad UI
- (void)setiPadUpUI {
    self.playView = [[TKPlayView alloc] initWithFrame:CGRectZero config:self.config viewController:self];
    self.playView.delegate = self;
    [self.view addSubview: self.playView ];
    [self portraitLayout];
}
// iPhone UI
- (void)setiPhoneUpUI {

    CGRect rect = CGRectMake(0, TK_StatusBar_Height, SCREENW, SCREENW*9/16);
    self.playView = [[TKPlayView alloc] initWithFrame:rect config:self.config viewController:self];
    self.playView.delegate = self;
    [self.view addSubview:self.playView];

}


// MARK: - Delegate
- (void)TKDBPlayViewFullScreen:(BOOL)fullScreen
{
    self.isLandscape = fullScreen;
    NSLog(@"屏幕切换 %@",fullScreen?@"全屏":@"默认屏幕");
    if (IS_IPHONE) {
        if (@available(iOS 16.0, *)) {
            [self setNeedsUpdateOfSupportedInterfaceOrientations];
        } else {
           [UIViewController attemptRotationToDeviceOrientation];
        }
    }else{
        if (self.isLandscape) {
            [self landscapeLayout];
        }else{
            [self portraitLayout];
        }
    }
}

// MARK: - 横竖屏切换
- (void)addOrientationNoti
{
  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newOrientationHandler) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
- (void)newOrientationHandler {
    UIInterfaceOrientation orient = [self getCurrentScreenOrientation];
    switch (orient) {
        case UIInterfaceOrientationPortrait:
            [self portraitLayout];
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            break;
        case UIInterfaceOrientationLandscapeLeft:
            break;
        case UIInterfaceOrientationLandscapeRight:
            [self landscapeLayout];
            break;
        default:
            break;
    }

}
// 手机端竖屏布局
- (void)portraitLayout{
    if (IS_IPHONE) {
        self.playView.frame = CGRectMake(0, TK_StatusBar_Height, SCREENW, SCREENW*9/16);
        self.playView.fullScreen = NO;
    }else{
        CGRect playViewRect = CGRectMake(self.iPadSpace, TKDBStatusHeight + self.iPadSpace, self.ipadPlayWidth, self.iPadContentH/2);
        self.playView.frame = playViewRect;
        self.playView.fullScreen = NO;
    }
}
// 手机端横屏布局
- (void)landscapeLayout{
    if (IS_IPHONE) {
        CGFloat statusBarHeight = 34;
        self.playView.frame = CGRectMake(statusBarHeight, 0, SCREENW-statusBarHeight, SCREENH);
        self.playView.fullScreen = YES;
    }else{
//        横屏下无边距的UI
        CGRect playViewRect = CGRectMake(0, 0,SCREENW,SCREENH);
        self.playView.frame = playViewRect;
        self.playView.fullScreen = YES;

    }

}
- (UIInterfaceOrientation)getCurrentScreenOrientation {
    // iOS 13 及以上版本
    if (@available(iOS 13.0, *)) {
        // 获取当前活动的场景窗口
        UIWindowScene *windowScene = (UIWindowScene *)[UIApplication sharedApplication].connectedScenes.allObjects.firstObject;
        if (windowScene) {
            return windowScene.interfaceOrientation; // 返回屏幕方向
        }
    } else {
        // iOS 13 以下版本
        return [UIApplication sharedApplication].statusBarOrientation; // 返回屏幕方向
    }

    return UIInterfaceOrientationUnknown; // 无法确定方向时
}

-(BOOL)shouldAutorotate {
   return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if (IS_IPAD) {
       return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
   }else{
       return self.isLandscape?UIInterfaceOrientationMaskLandscape:UIInterfaceOrientationMaskPortrait;
   }

}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
   UIInterfaceOrientation interfaceOriention = [UIApplication sharedApplication].statusBarOrientation;
   if (IS_IPAD) {
       if (interfaceOriention == UIInterfaceOrientationLandscapeLeft) {
           return UIInterfaceOrientationLandscapeLeft;
       }else {
           return UIInterfaceOrientationLandscapeRight;
       }
   } else {
       if (interfaceOriention == UIInterfaceOrientationLandscapeLeft) {
           return UIInterfaceOrientationLandscapeLeft;
       } else if (interfaceOriention == UIInterfaceOrientationLandscapeRight){
           return UIInterfaceOrientationLandscapeRight;
       } else {
           return UIInterfaceOrientationPortrait;
       }
   }
}
- (void)dealloc{
    [self.playView deallocPlayer]; // 释放播放器
}

控制器类型(UIViewController)

3.1 引入头文件

1
#import <TKPlayer/TKPlayer.h>

3.2 回调函数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@protocol TKPlayerDelegate <NSObject>
// 当前播放进度以及播放状态
- (void)TkPlayerCurrentValue:(float)currentValue state:(SuperPlayerState)state;


/// 获取当前播放状态
/// - Parameters:
///   - currentValue: 当前播放进度
///   - duration: 视频总时长
///   - playableDuration: 可播放时长
///   - state: 播放器的状态
- (void)TkPlayerCurrentValue:(float)currentValue duration:(float)duration playableDuration:(float)playableDuration state:(SuperPlayerState)state;

// 回调退出事件
- (void)TkPlayBack;

// 回调全屏事件
- (void)TkPlayFullScreen;

/// 回调全屏事件
/// - Parameter isFull: YES 全屏 NO:默认屏幕(iPhone 竖屏 ipad 默认屏幕)
- (void)TkPlayIsFullScreen:(BOOL)isFull;

// 回调触屏事件
- (void)TkPlayTapScreen;

// 回调播放完成事件
- (void)TkPlayFinish;

// 回调选集事件
- (void)TkPlayChoice;
3.3 控制器视频类型
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    TKPlayerConfig *config =  [[TKPlayerConfig alloc] init];
    config.playUIType = TKPlayUIBlack;
    config.company_id = @""; // 企业id
    config.package_id = @""; // 播放列表id
    config.player_id = @""; // 播放器id
    config.user_id   =  @""; // 用户id
    config.playerType = TKPlayerVideo; // 指定视频类型
    TKPlayController *tvc = [[TKPlayController alloc] init];
    tvc.delegate = self;
    [tvc presentToPlay:self config:config];
3.4 控制器课件类型
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    TKPlayerConfig *config =  [[TKPlayerConfig alloc] init];
    config.playUIType = TKPlayUIBlack;
    config.company_id = @""; // 企业id
    config.nickName = @""; // 用户昵称
    config.user_id = @""; // 用户id
    config.assetId = @"";// 视频id 云id
    config.playerType = TKPlayerCourse;// 指定课件类型
    TKPlayController *tvc = [[TKPlayController alloc] init];
    tvc.delegate = self;
    [tvc presentToPlay:self config:config];