Commit 1b41c8fe2d68b986f674009114b701a8117e7b6a

Authored by zhangxiaowen
1 parent fcc25a96

no message

CNLivePlayer/Classes/Cache/CNLivePlayerCache.h
... ... @@ -66,14 +66,14 @@ NS_ASSUME_NONNULL_BEGIN
66 66  
67 67  
68 68 /**
69   - * 删除所有的磁盘换存文件
70   - * 删除所有的磁盘缓存url
  69 + * 删除所有的磁盘缓存url(YYCache)
71 70 */
72 71 + (void)player_RemoveAllUrlCache;
73 72  
74 73  
75 74 /**
76   - * 删除所有的磁盘缓存url和视频
  75 + * 删除所有的磁盘缓存url(YYCache)
  76 + * 删除所有的磁盘缓存视频(KSYHTTPProxyService)
77 77 */
78 78 + (void)player_clearAllCache;
79 79  
... ...
CNLivePlayer/Classes/Cache/CNLivePlayerCache.m
... ... @@ -7,6 +7,7 @@
7 7 //
8 8  
9 9 #import "CNLivePlayerCache.h"
  10 +//缓存url
10 11 #import "YYCache.h"
11 12 //缓存视频
12 13 #import <KSYHTTPCache/KSYHTTPProxyService.h>
... ...
CNLivePlayer/Classes/Player/CNLivePlayer.h
... ... @@ -21,12 +21,19 @@
21 21  
22 22 NS_ASSUME_NONNULL_BEGIN
23 23  
  24 +/*
  25 + * 获取主播状态通知
  26 + */
24 27 UIKIT_EXTERN NSString *const CNLiveHostStatusChangedNotification;
25 28  
26   -// 成功回调
  29 +/*
  30 + * 成功回调
  31 + */
27 32 typedef void(^AuthSuccessBlock)(void);
28 33  
29   -// 失败回调
  34 +/*
  35 + * 失败回调
  36 + */
30 37 typedef void(^AuthFailureBlock)(NSDictionary *errorInfo);
31 38  
32 39 @interface CNLivePlayer : NSObject
... ... @@ -34,7 +41,7 @@ typedef void(^AuthFailureBlock)(NSDictionary *errorInfo);
34 41 /*
35 42 * 播放器类型
36 43 */
37   -@property (nonatomic, assign) CNLivePlayerType type;
  44 +@property (nonatomic, assign, readonly) CNLivePlayerType type;
38 45  
39 46 /*
40 47 * 播放器
... ... @@ -42,6 +49,15 @@ typedef void(^AuthFailureBlock)(NSDictionary *errorInfo);
42 49 @property (nonatomic, strong) KSYMoviePlayerController *player;
43 50  
44 51 #pragma mark - 初始化方法
  52 +/*
  53 + * 初始化播放器并设置播放地址
  54 + *
  55 + * url 视频url
  56 + *
  57 + */
  58 +- (instancetype)initWithContentURL:(NSURL *)url;
  59 +
  60 +
45 61 /* 初始化点播播放器 初始化成功后才能对播放器进行设置(如:自动播放、是否开启硬件解码等)和调用准备播放视频方法(prepareToPlay)
46 62 *
47 63 * videoId 视频Id
... ... @@ -105,7 +121,7 @@ typedef void(^AuthFailureBlock)(NSDictionary *errorInfo);
105 121 * 默认不静音
106 122 * [prepareToPlay]方法前设置即生效,也可以在播放过程中动态切换
107 123 */
108   -@property(nonatomic) BOOL shouldMute;
  124 +@property (nonatomic) BOOL shouldMute;
109 125  
110 126 /**
111 127 @abstract 是否循环播放
... ... @@ -113,7 +129,7 @@ typedef void(^AuthFailureBlock)(NSDictionary *errorInfo);
113 129 * 只在[prepareToPlay]调用前设置生效;
114 130 * 只有点播生效,直播场景请勿设置
115 131 */
116   -@property(nonatomic) BOOL shouldLoop;
  132 +@property (nonatomic) BOOL shouldLoop;
117 133  
118 134 /**
119 135 @abstract 指定逆时针旋转角度,只能是0/90/180/270, 不符合上述值不进行旋转
... ... @@ -127,16 +143,16 @@ typedef void(^AuthFailureBlock)(NSDictionary *errorInfo);
127 143 @discussion NO: 可以与其他后台播放共存,相互之间不会被打断
128 144 @discussion 默认为YES
129 145 */
130   -@property(nonatomic) BOOL bInterruptOtherAudio;
  146 +@property (nonatomic) BOOL bInterruptOtherAudio;
131 147  
132 148 /*
133 149 * 查询视频准备是否完成(只读)
134 150 */
135   -@property(nonatomic, readonly) BOOL isPreparedToPlay;
  151 +@property (nonatomic, readonly) BOOL isPreparedToPlay;
136 152  
137 153 #pragma mark - 方法
138 154 /*
139   - * 获取主播状态(离开/已结束
  155 + * 获取主播状态(离开/已结束)
140 156 */
141 157 - (void)getHostStatus:(void(^)(BOOL result))result;
142 158  
... ...
CNLivePlayer/Classes/Player/CNLivePlayer.m
... ... @@ -52,6 +52,32 @@ NSString * const CNLivePlayerMsgReceiveNotification = @&quot;CNLiveMsgToolsPlayerRece
52 52 NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChangedNotification";
53 53  
54 54 #pragma mark - 初始化方法
  55 +/*
  56 + * 初始化播放器并设置播放地址
  57 + *
  58 + * url 视频url
  59 + *
  60 + */
  61 +- (instancetype)initWithContentURL:(NSURL *)url {
  62 + self = [super init];
  63 + if (self) {
  64 + [UIApplication sharedApplication].idleTimerDisabled = YES;
  65 +
  66 + _contentType = @"";
  67 + _contentId = @"";
  68 + _idName = @"";
  69 +
  70 + [self addMovieNotificationObservers];
  71 + self.player = [[KSYMoviePlayerController alloc] initWithContentURL:url];
  72 + [self.view addSubview:self.player.view];
  73 + _player.view.frame = self.view.bounds;
  74 + _player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  75 + _player.shouldEnableKSYStatModule = NO;
  76 +
  77 + }
  78 + return self;
  79 +}
  80 +
55 81 /* 初始化点播播放器 初始化成功后才能对播放器进行设置(如:自动播放、是否开启硬件解码等)和调用准备播放视频方法(prepareToPlay)
56 82 *
57 83 * videoId 点播视频Id
... ... @@ -66,7 +92,7 @@ NSString * const CNLiveHostStatusChangedNotification = @&quot;CNLiveAnchorStatusChang
66 92  
67 93 }
68 94  
69   - self.type = CNLivePlayerVod;
  95 + _type = CNLivePlayerVod;
70 96  
71 97 _idName = @"vid";
72 98 _contentType = @"vod";
... ... @@ -101,7 +127,7 @@ NSString * const CNLiveHostStatusChangedNotification = @&quot;CNLiveAnchorStatusChang
101 127 activityId = activityId ? [NSString stringWithFormat:@"%@",activityId] : @"";
102 128 }
103 129  
104   - self.type = CNLivePlayerLive;
  130 + _type = CNLivePlayerLive;
105 131  
106 132 _idName = @"cid";
107 133 _contentType = @"live";
... ... @@ -140,7 +166,7 @@ NSString * const CNLiveHostStatusChangedNotification = @&quot;CNLiveAnchorStatusChang
140 166  
141 167 }
142 168  
143   - self.type = CNLivePlayerAudio;
  169 + _type = CNLivePlayerAudio;
144 170  
145 171 _idName = @"vid";
146 172 _contentType = @"vod";
... ... @@ -372,7 +398,7 @@ NSString * const CNLiveHostStatusChangedNotification = @&quot;CNLiveAnchorStatusChang
372 398 @"tag": PLAYER_Tag,
373 399 @"from": @"apple",
374 400 @"sid": PLAYER_UserId,
375   - @"uid": [CNLivePlayerTools getUidForStat:stamp]};
  401 + @"uid": [CNLivePlayerTools getUidForTimestamp:stamp]};
376 402  
377 403 NSString *videoUrl = [CNLivePlayerTools encryptionSignWithUrl:vodUrl parameter:parameter];
378 404  
... ... @@ -426,7 +452,7 @@ NSString * const CNLiveHostStatusChangedNotification = @&quot;CNLiveAnchorStatusChang
426 452 @"tag": PLAYER_Tag,
427 453 @"from": @"apple",
428 454 @"sid": PLAYER_UserId,
429   - @"uid": [CNLivePlayerTools getUidForStat:timestamp]};
  455 + @"uid": [CNLivePlayerTools getUidForTimestamp:timestamp]};
430 456  
431 457 NSString *videoUrl = [CNLivePlayerTools encryptionSignWithUrl:liveUrl parameter:parameter];
432 458  
... ... @@ -711,8 +737,8 @@ NSString * const CNLiveHostStatusChangedNotification = @&quot;CNLiveAnchorStatusChang
711 737 }
712 738 _playTime++;
713 739  
714   - NSString *stamp = [CNLivePlayerTools getTimestamp];
715   - NSString *url = [NSString stringWithFormat:@"%@?sver=%@&uid=%@&tm=%@&from=apple&spid=&appid=%@&version=1001_%@&eventid=1001&uri=%@&sid=&plat=i_%@&objid=%@_%@", StatUrl, StatVersion, [CNLivePlayerTools getUidForStat:stamp], stamp, PLAYER_AppID, PlayerSDKVersion, [NSString stringWithFormat:@"%ld_30", (long)_playTime], StatAppVersion,_contentId, _idName];
  740 + NSString *timestamp = [CNLivePlayerTools getTimestamp];
  741 + NSString *url = [NSString stringWithFormat:@"%@?sver=%@&uid=%@&tm=%@&from=apple&spid=&appid=%@&version=1001_%@&eventid=1001&uri=%@&sid=&plat=i_%@&objid=%@_%@", StatUrl, StatVersion, [CNLivePlayerTools getUidForTimestamp:timestamp], timestamp, PLAYER_AppID, PlayerSDKVersion, [NSString stringWithFormat:@"%ld_30", (long)_playTime], StatAppVersion,_contentId, _idName];
716 742 [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]] delegate:nil];
717 743  
718 744 }
... ...
CNLivePlayer/Classes/Tools/CNLivePlayerTools.h
... ... @@ -22,7 +22,7 @@
22 22 /**
23 23 * 获取UUID_Timestamp
24 24 */
25   -+ (NSString *)getUidForStat:(NSString *)date;
  25 ++ (NSString *)getUidForTimestamp:(NSString *)timestamp;
26 26  
27 27 //错误信息
28 28 + (NSError *)customErrorWithResponseObject:(id)responseObject errorInfo:(NSString *)errorInfo;
... ... @@ -39,6 +39,9 @@
39 39  
40 40  
41 41 #pragma mark - 方法
  42 +/**
  43 + * 视频出错,上传接口
  44 + */
42 45 + (void)errorStat:(NSString *)errorString contentId:(NSString *)contentId idName:(NSString *)idName;
43 46  
44 47 + (void)statSDKWithContentId:(NSString *)contentId idName:(NSString *)idName;
... ...
CNLivePlayer/Classes/Tools/CNLivePlayerTools.m
... ... @@ -14,9 +14,9 @@
14 14 @implementation CNLivePlayerTools
15 15 #pragma mark - 加密
16 16 + (NSString *)encryptionSignWithUrl:(NSString *)url parameter:(NSDictionary *)parameter {
17   - NSString *string1 = [NSString stringWithFormat:@"%@&key=%@", [CNLivePlayerTools signvalue:parameter], PLAYER_AppKey];
18   - NSString *signString1 = [[CNLivePlayerTools sha1:string1] uppercaseString];
19   - return [NSString stringWithFormat:@"%@?%@&sign=%@", url, [CNLivePlayerTools signvalue:parameter], signString1];
  17 + NSString *keyString = [NSString stringWithFormat:@"%@&key=%@", [CNLivePlayerTools signvalue:parameter], PLAYER_AppKey];
  18 + NSString *signString = [[CNLivePlayerTools sha1:keyString] uppercaseString];
  19 + return [NSString stringWithFormat:@"%@?%@&sign=%@", url, [CNLivePlayerTools signvalue:parameter], signString];
20 20  
21 21 }
22 22  
... ... @@ -46,7 +46,7 @@
46 46 NSArray *arr = [keyArr sortedArrayUsingSelector:@selector(compare:)];
47 47  
48 48 NSMutableString *str = [[NSMutableString alloc]init];
49   - for (int i=0; i < arr.count; i++) {
  49 + for (int i = 0; i < arr.count; i++) {
50 50  
51 51 NSString *parameterString = parameter[[arr objectAtIndex:i]];
52 52 if (parameterString.length > 0) {
... ... @@ -62,8 +62,8 @@
62 62 }
63 63  
64 64 #pragma mark - UUID
65   -+ (NSString *)getUidForStat:(NSString *)date {
66   -
  65 ++ (NSString *)getUidForTimestamp:(NSString *)timestamp {
  66 +
67 67 NSString *uid = @"";
68 68  
69 69 NSUserDefaults *defauts = [NSUserDefaults standardUserDefaults];
... ... @@ -73,7 +73,7 @@
73 73 return uid;
74 74 }
75 75 else {
76   - NSString *stamp = [NSString stringWithFormat:@"%.3f", [date floatValue]];
  76 + NSString *stamp = [NSString stringWithFormat:@"%.3f", [timestamp floatValue]];
77 77 stamp = [stamp stringByReplacingOccurrencesOfString:@"." withString:@""];
78 78  
79 79 NSString *idfv = [[[[UIDevice currentDevice] identifierForVendor] UUIDString] stringByReplacingOccurrencesOfString:@"-" withString:@""];
... ... @@ -119,15 +119,18 @@
119 119  
120 120  
121 121 #pragma mark - 方法
  122 +/**
  123 + * 视频出错,上传接口
  124 + */
122 125 + (void)errorStat:(NSString *)errorString contentId:(NSString *)contentId idName:(NSString *)idName{
123   - NSString *stamp = [CNLivePlayerTools getTimestamp];
124   - NSString *url = [NSString stringWithFormat:@"%@?sver=%@&uid=%@&tm=%@&from=apple&spid=&appid=%@&version=1001_%@&eventid=1000&uri=%@&sid=&plat=i_%@&objid=%@_%@", StatUrl, StatVersion, [CNLivePlayerTools getUidForStat:stamp], stamp, PLAYER_AppID, PlayerSDKVersion, errorString, StatAppVersion, contentId, idName];
  126 + NSString *timestamp = [CNLivePlayerTools getTimestamp];
  127 + NSString *url = [NSString stringWithFormat:@"%@?sver=%@&uid=%@&tm=%@&from=apple&spid=&appid=%@&version=1001_%@&eventid=1000&uri=%@&sid=&plat=i_%@&objid=%@_%@", StatUrl, StatVersion, [CNLivePlayerTools getUidForTimestamp:timestamp], timestamp, PLAYER_AppID, PlayerSDKVersion, errorString, StatAppVersion, contentId, idName];
125 128 [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]] delegate:nil];
126 129  
127 130 }
128 131 + (void)statSDKWithContentId:(NSString *)contentId idName:(NSString *)idName {
129   - NSString *stamp = [CNLivePlayerTools getTimestamp];
130   - NSString *url = [NSString stringWithFormat:@"%@?sver=%@&uid=%@&tm=%@&from=apple&spid=&appid=%@&version=1001_%@&eventid=1400&uri=&sid=&plat=i_%@&objid=%@_%@", StatUrl, StatVersion, [CNLivePlayerTools getUidForStat:stamp], stamp, PLAYER_AppID, PlayerSDKVersion, StatAppVersion, contentId , idName];
  132 + NSString *timestamp = [CNLivePlayerTools getTimestamp];
  133 + NSString *url = [NSString stringWithFormat:@"%@?sver=%@&uid=%@&tm=%@&from=apple&spid=&appid=%@&version=1001_%@&eventid=1400&uri=&sid=&plat=i_%@&objid=%@_%@", StatUrl, StatVersion, [CNLivePlayerTools getUidForTimestamp:timestamp], timestamp, PLAYER_AppID, PlayerSDKVersion, StatAppVersion, contentId , idName];
131 134 [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]] delegate:nil];
132 135  
133 136 }
... ...
Example/CNLivePlayer/CNLivePlayerController.m
... ... @@ -31,22 +31,23 @@
31 31 // NSLog(@"%@", errorInfo);
32 32 //
33 33 // }];
  34 +
34 35 //1066_716fabeaafb64236b60ecf74167c5c96
35 36 //82_c8ff347048414b7295cef882f8361073
36   - _player = [[CNLivePlayer alloc]initAudioPlayerWithAudioId:@"82_c8ff347048414b7295cef882f8361073" authSuccess:^{
37   - NSLog(@"---------------初始化播放器成功---------------");
38   - _player.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*9/16.0);
39   -
40   - [_player prepareToPlay];
41   - [self.view addSubview:_player.view];
42   -
43   - NSLog(@"-----%@", _player.contentURL);
44   -
45   -
46   - } authFailure:^(NSDictionary * _Nonnull errorInfo) {
47   - NSLog(@"%@", errorInfo);
48   -
49   - }];
  37 +// _player = [[CNLivePlayer alloc]initAudioPlayerWithAudioId:@"82_c8ff347048414b7295cef882f8361073" authSuccess:^{
  38 +// NSLog(@"---------------初始化播放器成功---------------");
  39 +// _player.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*9/16.0);
  40 +//
  41 +// [_player prepareToPlay];
  42 +// [self.view addSubview:_player.view];
  43 +//
  44 +// NSLog(@"-----%@", _player.contentURL);
  45 +//
  46 +//
  47 +// } authFailure:^(NSDictionary * _Nonnull errorInfo) {
  48 +// NSLog(@"%@", errorInfo);
  49 +//
  50 +// }];
50 51  
51 52 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
52 53 btn.frame =CGRectMake(0, 0, 100, 100);
... ... @@ -61,6 +62,14 @@
61 62 [back addTarget:self action:@selector(backDidClicked) forControlEvents:UIControlEventTouchUpInside];
62 63 [self.view addSubview:back];
63 64  
  65 +
  66 + CNLivePlayer *player = [[CNLivePlayer alloc] initWithContentURL:[NSURL URLWithString:@"http://wideo02.cnlive.com/video/data1/2017/0423/206473/800/c8ff347048414b7295cef882f8361073_206473_800.m3u8"]];
  67 + player.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*9/16.0);
  68 + player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  69 + [player prepareToPlay];
  70 + [self.view addSubview:player.view];
  71 +
  72 +
64 73 // KSYMoviePlayerController *player = [[KSYMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://wideo02.cnlive.com/video/data1/2017/0423/206473/800/c8ff347048414b7295cef882f8361073_206473_800.m3u8"]];
65 74 // player.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*9/16.0);
66 75 // player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
... ...