#import "LiveViewController.h"
#import "NetworkTools.h"
#import
#import
#import "ChatRoomView.h"
#import "CommentsView.h"
#import "LiveGiftShowModel.h"
#import "LiveGiftShowCustom.h"
/// 2.顏色
#define KRGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0green:(g)/255.0blue:(b)/255.0alpha:(a)]
staticNSString*status[] = {
? ? @"PLPlayerStatusUnknow",
? ? @"PLPlayerStatusPreparing",
? ? @"PLPlayerStatusReady",
? ? @"PLPlayerStatusOpen",
? ? @"PLPlayerStatusCaching",
? ? @"PLPlayerStatusPlaying",
? ? @"PLPlayerStatusPaused",
? ? @"PLPlayerStatusStopped",
? ? @"PLPlayerStatusError",
? ? @"PLPlayerStatusCompleted"
};
@interface LiveViewController ()<PLPlayerDelegate,EMChatroomManagerDelegate,EMChatManagerDelegate,LiveGiftShowCustomDelegate>
@property (nonatomic,strong) PLPlayer *player;
@property(nonatomic,weak)UIActivityIndicatorView*activityIndicatorView;
/// 評論專區(qū)
@property (nonatomic,strong) ChatRoomView *chatRoomView;
/// 直播數(shù)據(jù)
@property (nonatomic,strong) NSMutableArray *chatDataArray;
/// 禮物區(qū)域
@property(nonatomic,weak)LiveGiftShowCustom* customGiftShow;
@property (nonatomic ,strong) NSArray <LiveGiftListModel *>* giftArr;
@property (nonatomic ,strong) NSArray * giftDataSource;
@property (nonatomic, strong) LiveUserModel *firstUser;
@property (nonatomic, strong) LiveUserModel *secondUser;
@property (nonatomic, strong) LiveUserModel *thirdUser;
@property (nonatomic, strong) LiveUserModel *fourthUser;
@property (nonatomic, strong) LiveUserModel *fifthUser;
@property (nonatomic, strong) LiveUserModel *fifthUser1;
@property (nonatomic, strong) LiveUserModel *fifthUser2;
@property (nonatomic, strong) LiveUserModel *fifthUser3;
@property (nonatomic,strong) LiveGiftListModel *giftListModel;
@end
@implementationLiveViewController
-(void)viewWillAppear:(BOOL)animated {
? ? [superviewWillAppear:animated];
? ? [self.navigationController setNavigationBarHidden:YES];
}
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? self.view.backgroundColor = [UIColor whiteColor];
? ? [self chatRoomUI];
? ? [self getPlayerUI];
}
- (void)getPlayerUI {
? ? NSURL*streamURL = [NSURLURLWithString:self.rtmp];
? ? //? ? ? ? ? ? ? ? weakSelf.player = [PLPlayer playerWithURL:streamURL option:weakSelf.option];
? ? //
? ? //? ? ? ? ? ? ? ? [weakSelf.player play];
? ? [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
? ? PLPlayerOption *option = [PLPlayerOption defaultOption];
? ? [optionsetOptionValue:@(15) forKey:PLPlayerOptionKeyTimeoutIntervalForMediaPackets];
? ? self.player= [PLPlayerplayerWithURL:streamURLoption:option];
? ? self.player.delegate = self;
? ? //? ? [self.view addSubview:self.player.playerView];
? ? [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(startPlayer) name:UIApplicationWillEnterForegroundNotification object:nil];
? ? [selfsetUI];
? ? [self startPlayer];
? ? [self.playerplay];
}
#pragma mark - 播放代理
/// 結(jié)束主播
- (void)player:(nonnullPLPlayer*)player stoppedWithError:(nullableNSError*)error {
? ? NSLog(@"%@---%@",player,error);
? ? // 當(dāng)發(fā)生錯誤,停止播放時,會回調(diào)這個方法
? ? UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
? ? ? ? [self dismissViewControllerAnimated:YES completion:nil];
? ? }];
? ? //初始化
? ? UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"主播下播了..." message:@"" preferredStyle:UIAlertControllerStyleAlert];
? ? //添加操作
? ? [alertaddAction:alertAction];
? ? [self presentViewController:alert animated:YES completion:nil];
}
/// 設(shè)置播放器frame
- (void)setUI {
? ? if (self.player.status != PLPlayerStatusError) {
? ? ? ? UIView*playerView =self.player.playerView;
? ? ? ? playerView.backgroundColor= [UIColorwhiteColor];
? ? ? ? playerView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
? ? ? ? //? ? ? ? playerView.contentMode = UIViewContentModeScaleAspectFit;
? ? ? ? [self.viewaddSubview:playerView];
? ? ? ? //? ? ? ? if (!playerView.superview) {
? ? ? ? //? ? ? ? ? ? playerView.contentMode = UIViewContentModeScaleAspectFit;
? ? ? ? //? ? ? ? ? ? playerView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
? ? ? ? //? ? ? ? ? ? [self.view addSubview:playerView];
? ? ? ? //? ? ? ? }
? ? ? ? UIButton*cancelBtn = [[UIButtonalloc]init];
? ? ? ? cancelBtn.frame=CGRectMake(self.view.bounds.size.width-70,20,50,50);
? ? ? ? [cancelBtnsetTitle:@"關(guān)閉" forState:UIControlStateNormal];
? ? ? ? [cancelBtnsetTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
? ? ? ? [playerViewaddSubview:cancelBtn];
? ? ? ? [cancelBtnaddTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
? ? ? ? // 評論區(qū)
? ? ? ? self.chatRoomView= [[ChatRoomViewalloc]init];
? ? ? ? self.chatRoomView.frame = CGRectMake(10, self.view.bounds.size.height-250, self.view.bounds.size.width-100, 200);
? ? ? ? [playerViewaddSubview:self.chatRoomView];
? ? ? ? CommentsView*commentsView = [[CommentsViewalloc]init];
? ? ? ? commentsView.frame = CGRectMake(0, self.view.bounds.size.height-50, self.view.bounds.size.width, 50);
? ? ? ? [playerViewaddSubview:commentsView];
? ? ? ? __weaktypeof(self)weakSelf =self;
? ? ? ? // 評論區(qū)
? ? ? ? [commentsViewsetCommentsBlock:^(NSString*_Nonnullcontent) {
? ? ? ? ? ? NSLog(@"發(fā)表評論=%@",content);
? ? ? ? ? ? EMTextMessageBody*body = [[EMTextMessageBodyalloc]initWithText:content];
? ? ? ? ? ? EMMessage*message = [[EMMessagealloc]initWithConversationID:self.chatRoomIdfrom:@"15622731953"to:self.chatRoomIdbody:bodyext:nil];
? ? ? ? ? ? message.chatType=EMChatTypeChatRoom;
? ? ? ? ? ? // 發(fā)送消息
? ? ? ? ? ? EMConversation*conversation = [[EMClientsharedClient].chatManagergetConversation:message.conversationIdtype:EMConversationTypeChatRoomcreateIfNotExist:YES];
? ? ? ? ? ? [conversationinsertMessage:messageerror:nil];
? ? ? ? ? ? // 更新消息
//? ? ? ? ? ? [[EMClient sharedClient].chatManager updateMessage:message];
//? ? ? ? ? ? [[EMClient sharedClient].chatManager updateMessage:message completion:^(EMMessage *aMessage, EMError *aError) {
//? ? ? ? ? ? ? ? NSLog(@"---%@---%@",aMessage,aError);
//
//? ? ? ? ? ? }];
? ? ? ? ? ? [[EMClientsharedClient].chatManagersendMessage:messageprogress:nilcompletion:^(EMMessage*message,EMError*error) {
? ? ? ? ? ? ? ? NSLog(@"---%@---%@",message,error);
? ? ? ? ? ? ? ? if(!error) {
//? ? ? ? ? ? ? ? ? ? NSString *string = [NSString stringWithFormat:@"%@:%@",message.from,content];
? ? ? ? ? ? ? ? ? ? NSAttributedString*string = [selfcontentFromColor:message.fromandText:content];
? ? ? ? ? ? ? ? ? ? [self.chatDataArrayaddObject:string];
? ? ? ? ? ? ? ? ? ? /// 賦值刷新
? ? ? ? ? ? ? ? ? ? self.chatRoomView.dataArray = self.chatDataArray;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }];
? ? ? ? }];
? ? ? ? /// 發(fā)禮物
? ? ? ? [commentsViewsetGiftBlock:^(NSIndexPath*itme) {
? ? ? ? ? ? [weakSelfv15BtnClicked:itme];
? ? ? ? }];
? ? }
}
#pragma mark - 響應(yīng)方法
- (void)cancelClick {
? ? [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)startPlayer {
? ? [self addActivityIndicatorView];
? ? [self.playerplay];
}
/// 設(shè)置播放器view
- (void)addActivityIndicatorView {
? ? if (self.activityIndicatorView) {
? ? ? ? return;
? ? }
? ? UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
? ? activityIndicatorView.center=CGPointMake(CGRectGetMidX(self.view.bounds),CGRectGetMidY(self.view.bounds));
? ? [self.viewaddSubview:activityIndicatorView];
? ? [activityIndicatorViewstopAnimating];
? ? self.activityIndicatorView= activityIndicatorView;
}
#pragma mark - <PLPlayerDelegate>
- (void)player:(nonnullPLPlayer*)player statusDidChange:(PLPlayerStatus)state {
? ? //? ? if (PLPlayerStatusCaching == state) {
? ? //? ? ? ? [self.activityIndicatorView startAnimating];
? ? //? ? } else {
? ? //? ? ? ? [self.activityIndicatorView stopAnimating];
? ? //? ? }
? ? NSLog(@"%@",status[state]);
}
#pragma mark- 聊天室
- (void)chatRoomUI {
? ? // 登錄
//? ? [[EMClient sharedClient] addDelegate:self delegateQueue:nil];
//? ? // 注冊群組回調(diào)
//? ? [[EMClient sharedClient].groupManager addDelegate:self delegateQueue:nil];
//? ? // 注冊聯(lián)系人回調(diào)
//? ? [[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];
? ? // 注冊信息回調(diào)
? ? [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];;
? ? // 注冊聊天室回調(diào)
? ? [[EMClient sharedClient].roomManager addDelegate:self delegateQueue:nil];
? ? // 加入聊天室
? ? [[EMClientsharedClient].roomManagerjoinChatroom:self.chatRoomIdcompletion:^(EMChatroom*aChatroom,EMError*aError) {
? ? ? ? NSLog(@"%@--%@",aChatroom,aError);
? ? }];
}
//sendMessageReadAck
/// 接收到消息列表
- (void)messagesDidReceive:(NSArray*)aMessages {
? ? NSLog(@"收到消息列表%@",aMessages);
? ? for(EMMessage*messageinaMessages) {
? ? ? ? EMTextMessageBody *body = (EMTextMessageBody *)message.body;
? ? ? ? NSAttributedString*string = [selfcontentFromColor:message.fromandText:body.text];
? ? ? ? [self.chatDataArrayaddObject:string];
? ? }
? ? // 賦值
? ? self.chatRoomView.dataArray = self.chatDataArray;
}
#pragma mark - 改變字體顏色
- (NSAttributedString*)contentFromColor:(NSString*)from andText:(NSString*)text {
? ? NSString*contentString = [NSStringstringWithFormat:@"%@ %@",from,text];
? ? NSMutableAttributedString *contentStr = [[NSMutableAttributedString alloc]initWithString:contentString];
? ? //找出特定字符在整個字符串中的位置
? ? NSRangeredRange =NSMakeRange([[contentStrstring]rangeOfString:from].location, [[contentStrstring]rangeOfString:from].length);
? ? //修改特定字符的顏色? 235 229 177
? ? [contentStraddAttributes:@{NSForegroundColorAttributeName:KRGBACOLOR(235,229,177,1.0)}range:redRange];
? ? returncontentStr;
}
#pragma mark - 退出移除操作
- (void)dealloc {
? ? // 移除聊天室回調(diào)
? ? [[EMClient sharedClient].roomManager removeDelegate:self];
//? ? 75415692574721
? ? // 退出聊天室
? ? [[EMClient sharedClient].roomManager leaveChatroom:self.chatRoomId completion:^(EMError *aError) {
? ? ? ? NSLog(@"%@",aError);
? ? }];
}
- (NSMutableArray*)chatDataArray {
? ? if (!_chatDataArray) {
? ? ? ? _chatDataArray= [NSMutableArrayarray];
? ? }
? ? return _chatDataArray;
}
#pragma mark - 點(diǎn)擊屏幕
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event {
? ? /// 發(fā)布隱藏禮物通知
? ? [[NSNotificationCenter defaultCenter]postNotificationName:@"began" object:nil];
}
#pragma mark - 禮物區(qū)域
/*
?以下是測試方法:
?分別是三種添加視圖的方法
?animatedWithGiftModel:? 從1開始動畫展示到 model.toNumber 的效果,會累加;
?addLiveGiftShowModel:? 普通的從1顯示禮物視圖,會+=1;
?addLiveGiftShowModel: showNumber: 普通的禮物顯示視圖,指定顯示特定數(shù)字。
?*/
- (void)v15BtnClicked:(NSIndexPath*)indexPath{
? ? switch(indexPath.item) {
? ? ? ? case0:{
? ? ? ? ? ? LiveGiftShowModel * model = [LiveGiftShowModel giftModel:self.giftListModel userModel:self.firstUser];
//? ? ? ? ? ? model.toNumber = 8;
//? ? ? ? ? ? model.interval = 0.15;
? ? ? ? ? ? [self.customGiftShowanimatedWithGiftModel:model];
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? case1:{
? ? ? ? ? ? LiveGiftShowModel * model = [LiveGiftShowModel giftModel:self.giftArr[1] userModel:self.secondUser];
? ? ? ? ? ? [self.customGiftShowaddLiveGiftShowModel:model];
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? case2:{
? ? ? ? ? ? LiveGiftShowModel * model = [LiveGiftShowModel giftModel:self.giftArr[2] userModel:self.thirdUser];
? ? ? ? ? ? [self.customGiftShowanimatedWithGiftModel:model];
//? ? ? ? ? ? [self.customGiftShow addLiveGiftShowModel:model showNumber:99];
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? case3:{
? ? ? ? ? ? LiveGiftShowModel * model = [LiveGiftShowModel giftModel:self.giftArr[3] userModel:self.fourthUser];
//? ? ? ? ? ? model.toNumber = 3;
? ? ? ? ? ? [self.customGiftShowanimatedWithGiftModel:model];
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? case4:{
? ? ? ? ? ? LiveGiftShowModel * model = [LiveGiftShowModel giftModel:self.giftArr[4] userModel:self.fifthUser];
//? ? ? ? ? ? model.toNumber = 2;
? ? ? ? ? ? [self.customGiftShowanimatedWithGiftModel:model];
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? case5:{
? ? ? ? ? ? LiveGiftShowModel * model = [LiveGiftShowModel giftModel:self.giftArr[5] userModel:self.fifthUser1];
? ? ? ? ? ? [self.customGiftShowanimatedWithGiftModel:model];
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? case6:{
? ? ? ? ? ? LiveGiftShowModel * model = [LiveGiftShowModel giftModel:self.giftArr[6] userModel:self.fifthUser2];
? ? ? ? ? ? //? ? ? ? ? ? model.toNumber = 2;
? ? ? ? ? ? [self.customGiftShowanimatedWithGiftModel:model];
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? case7:{
? ? ? ? ? ? LiveGiftShowModel * model = [LiveGiftShowModel giftModel:self.giftArr[7] userModel:self.fifthUser3];
? ? ? ? ? ? [self.customGiftShowanimatedWithGiftModel:model];
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? default:
? ? ? ? ? ? break;
? ? }
}
/**
?彈幕移除回調(diào)代理
?@param showModel 數(shù)據(jù)模型
?*/
- (void)giftDidRemove:(LiveGiftShowModel*)showModel {
? ? WLog(@"用戶:%@ 送出了 %li 個 %@", showModel.user.name,showModel.currentNumber, showModel.giftModel.name);
}
/*
?禮物視圖支持很多配置屬性,開發(fā)者按需選擇。
?*/
- (LiveGiftShowCustom*)customGiftShow{
? ? if (!_customGiftShow) {
? ? ? ? _customGiftShow = [LiveGiftShowCustom addToView:self.view];
? ? ? ? _customGiftShow.addMode = LiveGiftAddModeAdd;
? ? ? ? [_customGiftShow setMaxGiftCount:3];
? ? ? ? [_customGiftShow setShowMode:LiveGiftShowModeFromTopToBottom];
? ? ? ? [_customGiftShow setAppearModel:LiveGiftAppearModeLeft];
? ? ? ? [_customGiftShow setHiddenModel:LiveGiftHiddenModeNone];
? ? ? ? [_customGiftShow enableInterfaceDebug:YES];
? ? ? ? _customGiftShow.delegate = self;
? ? }
? ? return _customGiftShow;
}
- (LiveUserModel*)firstUser {
? ? if (!_firstUser) {
? ? ? ? _firstUser= [[LiveUserModelalloc]init];
? ? ? ? _firstUser.name = @"么么噠";
? ? ? ? _firstUser.iconUrl = @"http://ww1.sinaimg.cn/large/c6a1cfeagy1ffbg8tb6wqj20gl0qogni.jpg";
? ? }
? ? return _firstUser;
}
- (LiveUserModel*)secondUser {
? ? if (!_secondUser) {
? ? ? ? _secondUser= [[LiveUserModelalloc]init];
? ? ? ? _secondUser.name = @"啪啪啪";
? ? ? ? _secondUser.iconUrl = @"http://ww1.sinaimg.cn/large/c6a1cfeagy1ffbgd5cr5nj209s0akgly.jpg";
? ? }
? ? return _secondUser;
}
- (LiveUserModel*)thirdUser {
? ? if (!_thirdUser) {
? ? ? ? _thirdUser= [[LiveUserModelalloc]init];
? ? ? ? _thirdUser.name = @"項(xiàng)圈";
? ? ? ? _thirdUser.iconUrl = @"http://ww1.sinaimg.cn/large/c6a1cfeagy1ffbgeuwk21j205k05kq2w.jpg";
? ? }
? ? return _thirdUser;
}
- (LiveUserModel*)fourthUser {
? ? if (!_fourthUser) {
? ? ? ? _fourthUser= [[LiveUserModelalloc]init];
? ? ? ? _fourthUser.name = @"比基尼";
? ? ? ? _fourthUser.iconUrl = @"http://ww1.sinaimg.cn/large/c6a1cfeagy1ffbgfpf5bgj205k07v3yk.jpg";
? ? }
? ? return _fourthUser;
}
- (LiveUserModel*)fifthUser {
? ? if (!_fifthUser) {
? ? ? ? _fifthUser= [[LiveUserModelalloc]init];
? ? ? ? _fifthUser.name = @"上上簽";
? ? ? ? _fifthUser.iconUrl = @"http://ww1.sinaimg.cn/large/c6a1cfeagy1ffbgg5427qj205k05k748.jpg";
? ? }
? ? return _fifthUser;
}
- (LiveUserModel*)fifthUser1 {
? ? if (!_fifthUser1) {
? ? ? ? _fifthUser1= [[LiveUserModelalloc]init];
? ? ? ? _fifthUser1.name = @"法拉利";
? ? ? ? _fifthUser1.iconUrl = @"http://ww1.sinaimg.cn/large/c6a1cfeagy1ffbgg5427qj205k05k748.jpg";
? ? }
? ? return _fifthUser1;
}
- (LiveUserModel*)fifthUser2 {
? ? if (!_fifthUser2) {
? ? ? ? _fifthUser2= [[LiveUserModelalloc]init];
? ? ? ? _fifthUser2.name = @"壁咚";
? ? ? ? _fifthUser2.iconUrl = @"http://ww1.sinaimg.cn/large/c6a1cfeagy1ffbgg5427qj205k05k748.jpg";
? ? }
? ? return _fifthUser2;
}
- (LiveUserModel*)fifthUser3 {
? ? if (!_fifthUser3) {
? ? ? ? _fifthUser3= [[LiveUserModelalloc]init];
? ? ? ? _fifthUser3.name = @"浪漫熱氣球";
? ? ? ? _fifthUser3.iconUrl = @"http://ww1.sinaimg.cn/large/c6a1cfeagy1ffbgg5427qj205k05k748.jpg";
? ? }
? ? return _fifthUser3;
}
- (LiveGiftListModel*)giftListModel {
? ? if (!_giftListModel) {
? ? ? ? _giftListModel = [[LiveGiftListModel alloc]init];
? ? ? ? _giftListModel.picUrl = @"http://ww1.sinaimg.cn/large/c6a1cfeagy1ffbgg5427qj205k05k748.jpg";
? ? }
? ? return _giftListModel;
}
@end