Commit 08b73c0342be35296353ec680a1c138161e26f9e
1 parent
966a5db2
no message
Showing
9 changed files
with
91 additions
and
39 deletions
CNLivePlayer/Classes/Header/CNLivePlayerConst.h
| ... | ... | @@ -9,10 +9,5 @@ |
| 9 | 9 | #ifndef CNLivePlayerConst_h |
| 10 | 10 | #define CNLivePlayerConst_h |
| 11 | 11 | |
| 12 | -#define CNLivePlayer_AppId @"60_irn9edco29" | |
| 13 | - | |
| 14 | -#define CNLivePlayer_AppKey @"69fe70bda67e059286ad509d9f5377f815955645b52f94" | |
| 15 | - | |
| 16 | -#define CNLivePlayer_BundleID @"com.cnlive.video" | |
| 17 | 12 | |
| 18 | 13 | #endif /* CNLivePlayerConst_h */ | ... | ... |
CNLivePlayer/Classes/Player/CNLivePlayer.h
| ... | ... | @@ -13,6 +13,9 @@ |
| 13 | 13 | // 枚举定义 |
| 14 | 14 | #import "CNLivePlayerDefines.h" |
| 15 | 15 | |
| 16 | +//常量 | |
| 17 | +#import "CNLivePlayerConst.h" | |
| 18 | + | |
| 16 | 19 | // 播放器管理类 |
| 17 | 20 | #import "CNLivePlayerManager.h" |
| 18 | 21 | |
| ... | ... | @@ -58,7 +61,7 @@ typedef void(^AuthFailureBlock)(NSDictionary *errorInfo); |
| 58 | 61 | * url 视频url |
| 59 | 62 | * |
| 60 | 63 | */ |
| 61 | -- (instancetype)initWithContentURL:(NSURL *)url; | |
| 64 | +- (instancetype)initWithContentURL:(NSString *)url; | |
| 62 | 65 | |
| 63 | 66 | |
| 64 | 67 | /* 初始化点播播放器 初始化成功后才能对播放器进行设置(如:自动播放、是否开启硬件解码等)和调用准备播放视频方法(prepareToPlay) | ... | ... |
CNLivePlayer/Classes/Player/CNLivePlayer.m
| ... | ... | @@ -13,12 +13,11 @@ |
| 13 | 13 | #import "CNLivePlayerTools.h" |
| 14 | 14 | //IM |
| 15 | 15 | #import <CNLiveMsgTools/CNLiveMsgManager.h> |
| 16 | -//常量 | |
| 17 | -#import "CNLivePlayerConst.h" | |
| 18 | 16 | //url |
| 19 | 17 | #import "CNLivePlayerUrl.h" |
| 20 | 18 | //缓存视频 |
| 21 | 19 | #import <KSYHTTPCache/HTTPCacheDefines.h> |
| 20 | +#import "CNLiveHTTPProxyService.h" | |
| 22 | 21 | |
| 23 | 22 | @interface CNLivePlayer () { |
| 24 | 23 | NSString *_idName; |
| ... | ... | @@ -44,10 +43,6 @@ |
| 44 | 43 | } |
| 45 | 44 | @property (nonatomic, strong, readwrite) UIView *view; |
| 46 | 45 | @property (nonatomic, copy) NSString *videoUrl; |
| 47 | -/* | |
| 48 | - * 是否缓存(暂时不能用) | |
| 49 | - */ | |
| 50 | -@property (nonatomic, assign) BOOL openCache; | |
| 51 | 46 | |
| 52 | 47 | @end |
| 53 | 48 | |
| ... | ... | @@ -65,7 +60,7 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang |
| 65 | 60 | * url 视频url |
| 66 | 61 | * |
| 67 | 62 | */ |
| 68 | -- (instancetype)initWithContentURL:(NSURL *)url { | |
| 63 | +- (instancetype)initWithContentURL:(NSString *)url { | |
| 69 | 64 | self = [super init]; |
| 70 | 65 | if (self) { |
| 71 | 66 | [UIApplication sharedApplication].idleTimerDisabled = YES; |
| ... | ... | @@ -76,8 +71,8 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang |
| 76 | 71 | _rate = @"2"; |
| 77 | 72 | |
| 78 | 73 | [self addMovieNotificationObservers]; |
| 79 | - | |
| 80 | - self.player = [[KSYMoviePlayerController alloc] initWithContentURL:url]; | |
| 74 | + NSString *proxyUrl = [[CNLiveHTTPProxyService manager] getProxyUrl:url]; | |
| 75 | + self.player = [[KSYMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:PLAYER_OpenCache?proxyUrl:url]]; | |
| 81 | 76 | [self.view addSubview:self.player.view]; |
| 82 | 77 | _player.view.frame = self.view.bounds; |
| 83 | 78 | _player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; |
| ... | ... | @@ -324,7 +319,8 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang |
| 324 | 319 | __strong typeof(self) strongSelf = weakSelf; |
| 325 | 320 | strongSelf.videoUrl = model.url; |
| 326 | 321 | if(!strongSelf.player){ |
| 327 | - strongSelf.player = [[KSYMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:strongSelf.videoUrl]]; | |
| 322 | + NSString *proxyUrl = [[CNLiveHTTPProxyService manager] getProxyUrl:strongSelf.videoUrl]; | |
| 323 | + strongSelf.player = [[KSYMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:PLAYER_OpenCache?proxyUrl:strongSelf.videoUrl]]; | |
| 328 | 324 | [strongSelf.view addSubview:self.player.view]; |
| 329 | 325 | strongSelf.player.view.frame = self.view.bounds; |
| 330 | 326 | strongSelf.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; |
| ... | ... | @@ -460,7 +456,8 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang |
| 460 | 456 | __strong typeof(self) strongSelf = weakSelf; |
| 461 | 457 | strongSelf.videoUrl = videoURL; |
| 462 | 458 | if(!strongSelf.player){ |
| 463 | - strongSelf.player = [[KSYMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:videoUrl]]; | |
| 459 | + NSString *proxyUrl = [[CNLiveHTTPProxyService manager] getProxyUrl:strongSelf.videoUrl]; | |
| 460 | + strongSelf.player = [[KSYMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:PLAYER_OpenCache?proxyUrl:strongSelf.videoUrl]]; | |
| 464 | 461 | [strongSelf.view addSubview:strongSelf.player.view]; |
| 465 | 462 | strongSelf.player.view.frame = strongSelf.view.bounds; |
| 466 | 463 | strongSelf.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; |
| ... | ... | @@ -537,7 +534,8 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang |
| 537 | 534 | __strong typeof(self) strongSelf = weakSelf; |
| 538 | 535 | strongSelf.videoUrl = url; |
| 539 | 536 | if(!strongSelf.player){ |
| 540 | - strongSelf.player = [[KSYMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:url]]; | |
| 537 | + NSString *proxyUrl = [[CNLiveHTTPProxyService manager] getProxyUrl:strongSelf.videoUrl]; | |
| 538 | + strongSelf.player = [[KSYMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:PLAYER_OpenCache?proxyUrl:strongSelf.videoUrl]]; | |
| 541 | 539 | [strongSelf.view addSubview:strongSelf.player.view]; |
| 542 | 540 | strongSelf.player.view.frame = strongSelf.view.bounds; |
| 543 | 541 | strongSelf.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; |
| ... | ... | @@ -622,7 +620,7 @@ NSString * const CNLiveHostStatusChangedNotification = @"CNLiveAnchorStatusChang |
| 622 | 620 | selector:@selector(mediaIsPreparedToPlayDidChange:) |
| 623 | 621 | name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification |
| 624 | 622 | object:nil]; |
| 625 | - if(self.openCache){ | |
| 623 | + if(PLAYER_OpenCache){ | |
| 626 | 624 | // 缓存状态 |
| 627 | 625 | [[NSNotificationCenter defaultCenter] addObserver:self |
| 628 | 626 | selector:@selector(mediaCacheDidChanged:) | ... | ... |
CNLivePlayer/Classes/Player/CNLivePlayerManager.h
| ... | ... | @@ -14,6 +14,7 @@ |
| 14 | 14 | #define PLAYER_ChannelName [CNLivePlayerManager manager].channelName?[CNLivePlayerManager manager].channelName:@"" |
| 15 | 15 | #define PLAYER_Tag [CNLivePlayerManager manager].tag?[CNLivePlayerManager manager].tag:@"" |
| 16 | 16 | #define PLAYER_TestEnvironment [CNLivePlayerManager manager].isTestEnvironment |
| 17 | +#define PLAYER_OpenCache [CNLivePlayerManager manager].openCache | |
| 17 | 18 | |
| 18 | 19 | NS_ASSUME_NONNULL_BEGIN |
| 19 | 20 | |
| ... | ... | @@ -35,6 +36,11 @@ NS_ASSUME_NONNULL_BEGIN |
| 35 | 36 | */ |
| 36 | 37 | @property (nonatomic, assign, readonly) BOOL isTestEnvironment; |
| 37 | 38 | |
| 39 | +/* | |
| 40 | + * 是否开启边播放边缓存 | |
| 41 | + */ | |
| 42 | +@property (nonatomic, assign) BOOL openCache; | |
| 43 | + | |
| 38 | 44 | /** |
| 39 | 45 | * 用户ID |
| 40 | 46 | */ |
| ... | ... | @@ -73,6 +79,16 @@ NS_ASSUME_NONNULL_BEGIN |
| 73 | 79 | + (void)setAppId:(NSString *)appId appKey:(NSString *)appKey isTestEnvironment:(BOOL)isTestEnvironment; |
| 74 | 80 | |
| 75 | 81 | /** |
| 82 | + @abstract 初始化直播云SDK | |
| 83 | + @param appId 在open.cnlive.com网站申请得到的appId | |
| 84 | + @param appKey 在open.cnlive.com网站申请得到的appKey | |
| 85 | + @param isTestEnvironment YES:测试环境 NO:正式环境 | |
| 86 | + @param openCache YES:开启视频边播放边缓存 NO:关闭视频边播放边缓存 | |
| 87 | + @warning 必传参数 | |
| 88 | + */ | |
| 89 | ++ (void)setAppId:(NSString *)appId appKey:(NSString *)appKey isTestEnvironment:(BOOL)isTestEnvironment openCache:(BOOL)openCache; | |
| 90 | + | |
| 91 | +/** | |
| 76 | 92 | @abstract 获取版本号 |
| 77 | 93 | */ |
| 78 | 94 | + (NSString *)getVersion; | ... | ... |
CNLivePlayer/Classes/Player/CNLivePlayerManager.m
| ... | ... | @@ -8,9 +8,9 @@ |
| 8 | 8 | |
| 9 | 9 | #import "CNLivePlayerManager.h" |
| 10 | 10 | #import <libksygpulive/KSYGPUStreamerKit.h> |
| 11 | -#import "CNLivePlayerConst.h" | |
| 12 | 11 | #import "CNLivePlayerUrl.h" |
| 13 | 12 | #import "CNLivePlayerDBTool.h" |
| 13 | +#import "CNLiveHTTPProxyService.h" | |
| 14 | 14 | |
| 15 | 15 | @interface CNLivePlayerManager() |
| 16 | 16 | /** |
| ... | ... | @@ -40,6 +40,7 @@ static CNLivePlayerManager * _instance = nil; |
| 40 | 40 | _instance = [[self alloc] init]; |
| 41 | 41 | //清除播放器鉴权缓存 |
| 42 | 42 | [CNLivePlayerManager clearAuthentication]; |
| 43 | + //创建数据库 | |
| 43 | 44 | [[CNLivePlayerDBTool shared] createTable]; |
| 44 | 45 | |
| 45 | 46 | }); |
| ... | ... | @@ -53,24 +54,63 @@ static CNLivePlayerManager * _instance = nil; |
| 53 | 54 | } |
| 54 | 55 | |
| 55 | 56 | + (void)setAppId:(NSString *)appId appKey:(NSString *)appKey isTestEnvironment:(BOOL)isTestEnvironment{ |
| 56 | - [[CNLivePlayerManager manager] setAppId:appId appKey:appKey isTestEnvironment:isTestEnvironment]; | |
| 57 | + [CNLivePlayerManager setAppId:appId appKey:appKey isTestEnvironment:isTestEnvironment openCache:NO]; | |
| 57 | 58 | |
| 58 | 59 | } |
| 59 | 60 | |
| 60 | -#pragma mark - 私有方法 | |
| 61 | -- (void)setAppId:(NSString *)appId appKey:(NSString *)appKey { | |
| 62 | - [self setAppId:appId appKey:appKey isTestEnvironment:NO]; | |
| 61 | ++ (void)setAppId:(NSString *)appId appKey:(NSString *)appKey isTestEnvironment:(BOOL)isTestEnvironment openCache:(BOOL)openCache{ | |
| 62 | + [[CNLivePlayerManager manager] setAppId:appId appKey:appKey isTestEnvironment:isTestEnvironment openCache:openCache]; | |
| 63 | + | |
| 63 | 64 | } |
| 64 | 65 | |
| 65 | -- (void)setAppId:(NSString *)appId appKey:(NSString *)appKey isTestEnvironment:(BOOL)isTestEnvironment { | |
| 66 | +#pragma mark - 私有方法 | |
| 67 | +- (void)setAppId:(NSString *)appId appKey:(NSString *)appKey isTestEnvironment:(BOOL)isTestEnvironment openCache:(BOOL)openCache{ | |
| 66 | 68 | [CNLivePlayerManager manager].appId = appId ? [NSString stringWithFormat:@"%@",appId] : @""; |
| 67 | 69 | [CNLivePlayerManager manager].appKey = appKey ? [NSString stringWithFormat:@"%@",appKey] : @""; |
| 68 | 70 | [CNLivePlayerManager manager].isTestEnvironment = isTestEnvironment; |
| 71 | + [CNLivePlayerManager manager].openCache = openCache; | |
| 72 | + if(openCache){ | |
| 73 | + //进入前台 | |
| 74 | + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil]; | |
| 75 | + //进入后台 | |
| 76 | + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil]; | |
| 77 | + //缓存视频的位置 | |
| 78 | + [[CNLiveHTTPProxyService manager] setCacheRoot:[NSTemporaryDirectory() stringByAppendingPathComponent:@"cnlive_player_cache"]]; | |
| 79 | + //边播放边缓存 | |
| 80 | + [[CNLiveHTTPProxyService manager] startServer]; | |
| 81 | + | |
| 82 | + } | |
| 69 | 83 | if (PLAYER_TestEnvironment) { |
| 70 | 84 | NSLog(@"CNLivePlayer的版本是:%@", [CNLivePlayerManager getVersion]); |
| 85 | + if(openCache){ | |
| 86 | + //边播放边缓存 | |
| 87 | + NSLog(@"CNLivePlayer-->开启边播放边缓存"); | |
| 88 | + | |
| 89 | + } | |
| 90 | + | |
| 91 | + } | |
| 92 | + | |
| 93 | +} | |
| 94 | + | |
| 95 | +- (void)applicationDidBecomeActive{ | |
| 96 | + if (![[CNLiveHTTPProxyService manager] isRunning]) { | |
| 97 | + [[CNLiveHTTPProxyService manager] startServer]; | |
| 98 | + | |
| 99 | + } | |
| 100 | + | |
| 101 | +} | |
| 102 | + | |
| 103 | +- (void)applicationDidEnterBackground{ | |
| 104 | + if ([[CNLiveHTTPProxyService manager] isRunning]) { | |
| 105 | + [[CNLiveHTTPProxyService manager] stopServer]; | |
| 106 | + | |
| 71 | 107 | } |
| 72 | 108 | } |
| 73 | 109 | |
| 110 | +- (void)dealloc{ | |
| 111 | + [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 112 | +} | |
| 113 | + | |
| 74 | 114 | + (NSString *)getVersion { |
| 75 | 115 | KSYGPUStreamerKit *kit = [[KSYGPUStreamerKit alloc] init]; |
| 76 | 116 | NSString *str = [[[kit.streamerBase getKSYVersion] componentsSeparatedByString:@"-"] lastObject]; | ... | ... |
CNLivePlayer/Classes/Player/CNLivePlayerTools.m
Example/CNLivePlayer/CNLiveAppDelegate.m
| ... | ... | @@ -9,16 +9,16 @@ |
| 9 | 9 | #import "CNLiveAppDelegate.h" |
| 10 | 10 | #import "CNLivePlayer.h" |
| 11 | 11 | |
| 12 | -#define KAppId @"60_irn9edco29" | |
| 13 | -#define KAppKey @"69fe70bda67e059286ad509d9f5377f815955645b52f94" | |
| 14 | -#define bundleID @"com.cnlive.video" | |
| 12 | +#define CNLivePlayer_AppId @"60_irn9edco29" | |
| 13 | +#define CNLivePlayer_AppKey @"69fe70bda67e059286ad509d9f5377f815955645b52f94" | |
| 14 | +#define CNLivePlayer_BundleID @"com.cnlive.video" | |
| 15 | 15 | |
| 16 | 16 | @implementation CNLiveAppDelegate |
| 17 | 17 | |
| 18 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 19 | 19 | { |
| 20 | 20 | // Override point for customization after application launch. |
| 21 | - [CNLivePlayerManager setAppId:KAppId appKey:KAppKey isTestEnvironment:YES]; | |
| 21 | + [CNLivePlayerManager setAppId:CNLivePlayer_AppId appKey:CNLivePlayer_AppKey isTestEnvironment:YES openCache:NO]; | |
| 22 | 22 | |
| 23 | 23 | return YES; |
| 24 | 24 | } | ... | ... |
Example/CNLivePlayer/CNLivePlayerController.m
| ... | ... | @@ -63,11 +63,11 @@ |
| 63 | 63 | back.backgroundColor = [UIColor greenColor]; |
| 64 | 64 | [back addTarget:self action:@selector(backDidClicked) forControlEvents:UIControlEventTouchUpInside]; |
| 65 | 65 | [self.view addSubview:back]; |
| 66 | - CNLivePlayerDownloader *dowa = [[CNLivePlayerDownloader alloc]initWithUrlString:@"http://wideo02.cnlive.com/video/data1/2017/0423/206473/800/c8ff347048414b7295cef882f8361073_206473_800.m3u8" progressBlock:^(float progress) { | |
| 67 | - NSLog(@"缓存进度中%f",progress); | |
| 68 | - }]; | |
| 69 | - NSLog(@"缓存的url是:%@",dowa.urlString); | |
| 70 | - [dowa startDownload]; | |
| 66 | +// CNLivePlayerDownloader *dowa = [[CNLivePlayerDownloader alloc]initWithUrlString:@"http://wideo02.cnlive.com/video/data1/2017/0423/206473/800/c8ff347048414b7295cef882f8361073_206473_800.m3u8" progressBlock:^(float progress) { | |
| 67 | +// NSLog(@"缓存进度中%f",progress); | |
| 68 | +// }]; | |
| 69 | +// NSLog(@"缓存的url是:%@",dowa.urlString); | |
| 70 | +// [dowa startDownload]; | |
| 71 | 71 | |
| 72 | 72 | // self.player = [[CNLivePlayer alloc] initWithContentURL:[NSURL URLWithString:@"http://wideo02.cnlive.com/video/data1/2017/0423/206473/800/c8ff347048414b7295cef882f8361073_206473_800.m3u8"]]; |
| 73 | 73 | // self.player.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width*9/16.0); |
| ... | ... | @@ -98,14 +98,12 @@ |
| 98 | 98 | // |
| 99 | 99 | // }]; |
| 100 | 100 | NSLog(@"当时已播放的时间是:%f",self.player.currentPlaybackTime); |
| 101 | - [_player setplay] | |
| 102 | - [_player setPlayerWithRate:@"4" authSuccess:^{ | |
| 101 | + [_player setPlayerWithContentId:nil rate:@"4" authSuccess:^{ | |
| 103 | 102 | __strong typeof(self) strongSelf = weakSelf; |
| 104 | 103 | [strongSelf.player prepareToPlay]; |
| 105 | 104 | strongSelf.player.currentPlaybackTime = 10; |
| 106 | - | |
| 107 | 105 | } authFailure:^(NSDictionary * _Nonnull errorInfo) { |
| 108 | - | |
| 106 | + | |
| 109 | 107 | }]; |
| 110 | 108 | |
| 111 | 109 | } | ... | ... |
Example/CNLivePlayer/CNLiveViewController.m
| ... | ... | @@ -9,6 +9,7 @@ |
| 9 | 9 | #import "CNLiveViewController.h" |
| 10 | 10 | #import "CNLivePlayerController.h" |
| 11 | 11 | #import "CNLivePlayerDBTool.h" |
| 12 | +#import "CNLiveHTTPProxyService.h" | |
| 12 | 13 | |
| 13 | 14 | @interface CNLiveViewController () |
| 14 | 15 | |
| ... | ... | @@ -22,6 +23,8 @@ |
| 22 | 23 | for(CNLivePlayerDBModel *model in data){ |
| 23 | 24 | NSLog(@"%@---%@",model.contentId,model.url); |
| 24 | 25 | } |
| 26 | + NSLog(@"--是否缓存--%d----",[[CNLiveHTTPProxyService manager]isCacheCompleteForUrl:[NSURL URLWithString:@"http://wideo02.cnlive.com/video/data1/2017/0423/206473/800/c8ff347048414b7295cef882f8361073_206473_800.m3u8"]]); | |
| 27 | + | |
| 25 | 28 | |
| 26 | 29 | } |
| 27 | 30 | - (void)viewDidLoad | ... | ... |