IVTKSYDianBoViewController.m 37.9 KB
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
//
//  IVTKSYDianBoViewController.m
//  IVTMeLive
//
//  Created by XRG on 16/5/19.
//  Copyright © 2016年 Joky. All rights reserved.
//

#import "IVTKSYDianBoViewController.h"
#import <libksygpulive/libksygpulive.h>
#import <libksygpulive/libksygpuimage.h>
#import <AVFoundation/AVFoundation.h>
#import "Masonry.h"
#import <ifaddrs.h>
#import <arpa/inet.h>
#import <CommonCrypto/CommonDigest.h>
#import "UIFont+IVTMeFont.h"
#import "UIColor+IVTMeColor.h"
#import "GrounderSuperView.h"
#import "HPGrowingTextView.h"
#import "IVTKSYLiveSHowObj.h"
#import "IVTUserInfoCenterViewController.h"
#import "IVTMeUserView.h"
#import "JSONKit.h"
#import "informReportViewController.h"

#import "UIButton+WebCache.h"
#import "IVTMyLiveViewController.h"
#import "IVTMeMineUserView.h"
#import "IVTMyChatListViewController.h"

#define krequestLiveRoomInfoURL [NSString stringWithFormat:@"%@%@",BaseURL,@"live/requestLiveRoomInfo.json"]
#define userInfoUrl [NSString stringWithFormat:@"%@%@",BaseURL,@"user/requestUserInfo.json"]

@interface IVTKSYDianBoViewController ()<UITableViewDelegate,UITableViewDataSource,XMPPStreamDelegate,XMPPManagerDelegate,UITextFieldDelegate,UIGestureRecognizerDelegate,IVTMeUserDelegate>{
    // 金山云拉流核心类
//    IVTKSYLiveSHowObj * ksyLiveShowObj;
    /**
     *  上部UI
     */
    // 屏幕上部
    // 主播头像背景
    UIImageView *_headBackGroundImg;
    // 主播头像
    UIButton *_headImg;
    //  直播label
    UILabel *_liveLabel;
    //  人数label
    UILabel *_viewCount;
    //  关注按钮
    UIImageView *_attentionImg;
    
    
    
    // 蜜糖背景
    UIButton *_mitangBackGroundImg;
    //  蜜糖更多按钮
    UIImageView *_mitangMoreImg;
    // 蜜糖数
    UILabel *_mitangCountLabel;
    // 观众滚动视图
    UIView *_viewersView;
    
    
    /**
     *  中部UI
     */
    
    UIWebView *_gifWebView;

    
    /**
     *  下部UI
     */
    // 聊天按钮
    UIButton *_chatBtn;
    //  消息按钮
    UIButton *_msgBtn;
    
    // 分享按钮
//    UIButton *_shareBtn;
    // 礼物按钮
    UIButton *_presentBtn;
    
    //  红包按钮
    UIButton  *_redPacket;
    //  关闭直播按钮
    UIButton *_closeBtn;
    //  分享页面
//    CustomShareView *_customShareView;
    
    // 私信
    IVTMyChatListViewController *myChatListVC;

    UIView  *_zanView;
    CGFloat _heartSize;
    NSTimer *_burstTimer;
    NSTimeInterval lastCheckTime;
    double lastSize;
    int tapCount;
    
    NSMutableDictionary *dict1;
}

@property (strong, nonatomic) NSURL *liveShowURL;
@property (strong,nonatomic) NSURL *reloadUrl;
@property (strong, nonatomic) KSYMoviePlayerController *player;


//.. 聊天室
@property (nonatomic, strong) UITableView *chatRoomTableView;

@property (nonatomic, strong) NSMutableArray *messages;
@property (nonatomic, strong)  NSLayoutConstraint *bottomConstraint;
@property (nonatomic, strong) NSMutableDictionary *realJIDs;

@property (nonatomic, strong) GrounderSuperView *barrageView;
/*
 键盘UI
 */
@property (nonatomic, strong) HPGrowingTextView *textView;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIView *backgroundImg;
@property (nonatomic, strong) UIView *backGround;
@property (nonatomic, strong) UIButton *danmuSwitch;
@property (nonatomic, strong) UIButton *sendBtn;
@property (nonatomic, strong) IVTMeUserView *userView;

@property (nonatomic, strong) NSString *type;
@property (nonatomic, strong) NSString *recordAddr;
@property (nonatomic, strong) NSString *videoAddr;

@property (nonatomic, strong) NSString* serverIp;
@property (nonatomic, strong) NSTimer* timer;


// 进度条
@property (nonatomic, strong) UIProgressView *pro;
@property (nonatomic, strong) UISlider *mySlider;
@end

@implementation IVTKSYDianBoViewController
{
       UIView *videoView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    tapCount = 1000;
    [self setupObservers];
    [self requestLoad];
    [self KSY_StartLiveShowFunc];
    
    [self initUI];
    
    //  完善UITableViewCell
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
}

#pragma mark - 金山云开始观看直播
- (void)KSY_StartLiveShowFunc
{
//    ksyLiveShowObj =  [[IVTKSYLiveSHowObj alloc] init];
//    ksyLiveShowObj.videoView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
//    ksyLiveShowObj.videoView.userInteractionEnabled = YES;
//    [self.view addSubview:ksyLiveShowObj.videoView];
//    [ksyLiveShowObj onPlayVideo:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[self.roomInfo objectForKey:@"recordAddr"]]]];
//
    
    self.view.backgroundColor = [UIColor blackColor];

    _player = [[KSYMoviePlayerController alloc] initWithContentURL:self.liveShowURL];
    _player.controlStyle = MPMovieControlStyleNone;
    [_player.view setFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [self.view addSubview: _player.view];
    _player.view.autoresizesSubviews = TRUE;
    _player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    _player.shouldAutoplay = YES;
    _player.bufferTimeMax = 5;
    _player.scalingMode = MPMovieScalingModeAspectFit;
    _player.shouldEnableKSYStatModule = TRUE;
    [_player prepareToPlay];

}

#pragma mark - XMPP初始化
- (void)initXmppStreamFunc
{
    [[XMPPManager sharedInstance] joinOrCreateRoomWithChatRoomAddr:self.roomJID isOwner:NO];
    [XMPPManager sharedInstance].delegate = self;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
//    [ksyLiveShowObj setupObservers];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
//    [ksyLiveShowObj releaseObservers];
}

//
//- (void)fetchMessage {
//    dispatch_async(dispatch_get_main_queue(), ^{
//        NSManagedObjectContext *context = self.tool.roomCoreDataStorage.mainThreadManagedObjectContext;
//        NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"XMPPRoomMessageCoreDataStorageObject"];
//        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"roomJIDStr = %@", self.roomJID.bare];
//        request.predicate = predicate;
//        NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"localTimestamp" ascending:YES];
//        request.sortDescriptors = @[descriptor];
//        self.messages = [context executeFetchRequest:request error:nil];
//        [self.chatRoomTableView reloadData];
//        
//        if (self.messages.count > 0) {
//            [self.chatRoomTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:self.messages.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
//
//        }
//    });
//}


#pragma mark - initUI
- (void) initUI
{
    
    //  直播的底层View
    videoView = [[UIView alloc] init];
    UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,kScreenWidth,kScreenHeight)];
    [bgImgView setImage:[UIImage imageNamed:@"背景图片"]];
    [videoView addSubview:bgImgView];
    [videoView sendSubviewToBack:bgImgView];
    videoView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    videoView.userInteractionEnabled = YES;
    [self.view addSubview:videoView];
    
    
    
    __unsafe_unretained typeof (self) wself = self;
#pragma mark 上部UI
    // 添加头部背景
    _headBackGroundImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"headBackGroundImg"]];
    [self.view addSubview:_headBackGroundImg];
    _headBackGroundImg.userInteractionEnabled = YES;
    [_headBackGroundImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wself.view).offset(8);
        make.top.equalTo(wself.view).offset(30);
        make.size.mas_equalTo(CGSizeMake(100, 40));
    }];
    _headBackGroundImg.backgroundColor = [UIColor clearColor];
    _headBackGroundImg.layer.masksToBounds = YES;
    
    
    // 添加主播头像
    _headImg = [UIButton buttonWithType:UIButtonTypeCustom];
    _headImg.layer.cornerRadius = 37 / 2;
    _headImg.layer.masksToBounds = YES;
    [_headImg setBackgroundImage:[UIImage imageNamed:@"headImg"] forState:UIControlStateNormal];
    [_headBackGroundImg addSubview:_headImg];
    [_headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_headBackGroundImg).offset(3);
        make.top.equalTo(_headBackGroundImg).offset(1.5);
        make.size.mas_equalTo(CGSizeMake(37, 37));
    }];
    _headImg.backgroundColor = [UIColor clearColor];
    _headImg.layer.masksToBounds = YES;
//    [_headImg addTarget:self action:@selector(imageClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    //  添加直播Label
    _liveLabel  = [[UILabel alloc] init];
    [_headBackGroundImg addSubview:_liveLabel];
    [_liveLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_headBackGroundImg).offset(45);
        make.top.equalTo(_headBackGroundImg).offset(8);
        make.size.mas_equalTo(CGSizeMake(40, 10));
    }];
    _liveLabel.textAlignment = 1;
    _liveLabel.text = @"直播Live";
    _liveLabel.textColor = [UIColor colorWithRed:255 / 255 green:255 / 255 blue:255 / 255 alpha:1.0f];
    _liveLabel.font = [UIFont fontWithName:@"Helvetica" size:10];
    
    // 添加观看人数
    _viewCount = [[UILabel alloc] init];
    [_headBackGroundImg addSubview:_viewCount];
    [_viewCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_headBackGroundImg).offset(48);
        make.top.equalTo(_headBackGroundImg).offset(25);
        make.size.mas_equalTo(CGSizeMake(40, 10));
    }];
    _viewCount.text = @"348";
    _viewCount.textAlignment = 1;
    _viewCount.font = [UIFont fontWithName:@"Helvetica" size:10];
    _viewCount.textColor = [UIColor colorWithRed:255 / 255 green:255 / 255 blue:255 / 255 alpha:1.0f];
    
//    //  添加关注
//    _attentionImg  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"attention"]];
//    [_headBackGroundImg addSubview:_attentionImg];
//    [_attentionImg mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(_headBackGroundImg).offset(-10);
//        make.top.equalTo(_headBackGroundImg).offset(10);
//        make.size.mas_equalTo(CGSizeMake(40, 20));
//    }];
//    _attentionImg.backgroundColor = [UIColor clearColor];
//    _attentionImg.layer.masksToBounds = YES;
    
    // 添加蜜糖背景(主播蜜糖数)
    _mitangBackGroundImg = [UIButton buttonWithType:UIButtonTypeCustom];
    [_mitangBackGroundImg setBackgroundImage:[UIImage imageNamed:@"metangBackGroundImg"] forState:UIControlStateNormal];//..............
    [self.view addSubview:_mitangBackGroundImg];
    [_mitangBackGroundImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wself.view).offset(0);
        make.top.equalTo(_headBackGroundImg).offset(60);
        make.size.mas_equalTo(CGSizeMake(142, 23));
    }];
//    [_mitangBackGroundImg addTarget:self action:@selector(mitang:) forControlEvents:UIControlEventTouchUpInside];
    
    //  添加蜜糖数label
    UILabel *miTangNameLabel  = [[UILabel alloc] init];
    miTangNameLabel.textAlignment = 1;
    miTangNameLabel.textColor = [UIColor redColor];
    miTangNameLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
    miTangNameLabel.text = @"蜜糖";
    [_mitangBackGroundImg addSubview:miTangNameLabel];
    [miTangNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_mitangBackGroundImg.mas_left).offset(10);
        make.top.equalTo(_mitangBackGroundImg.mas_top);
        make.bottom.equalTo(_mitangBackGroundImg.mas_bottom);
        make.width.mas_equalTo(30);
    }];
    
    //  添加蜜糖数label
    _mitangCountLabel  = [[UILabel alloc] init];
    _mitangCountLabel.textAlignment = 1;
    _mitangCountLabel.textColor = [UIColor colorWithRed:255 / 255 green:255 / 255 blue:255 / 255 alpha:1.0f];
    _mitangCountLabel.font = [UIFont systemFontOfSize:15];
    [_mitangBackGroundImg addSubview:_mitangCountLabel];
    [_mitangCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(miTangNameLabel.mas_right).offset(5);
        make.top.equalTo(_mitangBackGroundImg).offset(4);
        make.size.mas_equalTo(CGSizeMake(0, 15));
    }];
    
    _mitangMoreImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mitangMore"]];
    [_mitangBackGroundImg addSubview:_mitangMoreImg];
    [_mitangMoreImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_mitangCountLabel.mas_right).offset(5);
        make.top.equalTo(_mitangBackGroundImg).offset(4);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    _mitangMoreImg.backgroundColor = [UIColor clearColor];
    _mitangMoreImg.layer.masksToBounds = YES;
    
    
#pragma mark - 中部UI

    
    /*
     弹幕
     */
    
    self.barrageView = [[GrounderSuperView alloc] initWithFrame:CGRectMake(0, _mitangBackGroundImg.frame.size.height + 100, kScreenWidth, 330)];
//    [self.barrageView bringSubviewToFront:];
    [self.view addSubview:_barrageView];
    self.barrageView.userInteractionEnabled = YES;//用户不可交互
    [self.view setBackgroundColor:[UIColor clearColor]];
    
    
    
#pragma mark - 下部UI
    //  聊天按钮
    _chatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_chatBtn setBackgroundImage:[UIImage imageNamed:@"暂停键"] forState:UIControlStateNormal];
    [self.view addSubview:_chatBtn];
    [_chatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wself.view).offset(12);
        make.bottom.equalTo(wself.view).offset(-12);
        make.size.mas_equalTo(CGSizeMake(31, 31));
    }];
    _chatBtn.backgroundColor = [UIColor clearColor];
    _chatBtn.layer.masksToBounds = YES;
    [_chatBtn addTarget:self action:@selector(playOrNotPlay:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //  退出房间按钮
    _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_closeBtn setBackgroundImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
    [self.view addSubview:_closeBtn];
    [_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(wself.view).offset(-12);
        make.bottom.equalTo(wself.view).offset(-12);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    _closeBtn.backgroundColor = [UIColor clearColor];
    _closeBtn.layer.masksToBounds = YES;
    [_closeBtn addTarget:self action:@selector(onQuit:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //  分享房间按钮
//    _shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [_shareBtn setBackgroundImage:[UIImage imageNamed:@"分享"] forState:UIControlStateNormal];
//    [self.view addSubview:_shareBtn];
//    [_shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(_closeBtn).offset(-42);
//        make.bottom.equalTo(wself.view).offset(-12);
//        make.size.mas_equalTo(CGSizeMake(30, 30));
//    }];
//    _shareBtn.backgroundColor = [UIColor clearColor];
//    _shareBtn.layer.masksToBounds = YES;
//    [_shareBtn addTarget:self action:@selector(shareBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    //  消息按钮
    _msgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [_msgBtn setBackgroundImage:[UIImage imageNamed:@"消息"] forState:UIControlStateNormal];
    [self.view addSubview:_msgBtn];
    [_msgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_closeBtn).offset(-42);
        make.bottom.equalTo(wself.view).offset(-12);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    _msgBtn.backgroundColor = [UIColor clearColor];
    _msgBtn.layer.masksToBounds = YES;
    [_msgBtn addTarget:self action:@selector(msgBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //进度条
    _mySlider = [[UISlider alloc] init];
    
    [_mySlider setMaximumTrackTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"未观看进度条"]]];

    [_mySlider setMinimumTrackTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"已观看进度条"]]];
    
    [_mySlider setThumbImage:[UIImage imageNamed:@"进度椭圆"] forState:UIControlStateNormal];

    
    [_mySlider addTarget:self action:@selector(sliderValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_mySlider];
    [_mySlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(wself.view);
        make.bottom.equalTo(_chatBtn.mas_top).with.offset(-12);
        make.size.mas_equalTo(CGSizeMake(350, 10));
        
    }];
    
    _pro = [[UIProgressView alloc] init];
    [_pro setTrackTintColor:[UIColor grayColor]];
    [_pro setProgressTintColor:[UIColor lightGrayColor]];
    [_mySlider addSubview:_pro];

    [_pro mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(wself.view);
        make.bottom.equalTo(_chatBtn.mas_top).with.offset(-15);
        make.size.mas_equalTo(CGSizeMake(350, 1));
    }];
 }



-(void)createTabView{
    
    self.chatRoomTableView = [[UITableView alloc] initWithFrame:CGRectMake(8, kScreenHeight - 260, 250, 180) style:UITableViewStylePlain];
    
    self.chatRoomTableView.backgroundColor = [UIColor clearColor];
    [self.chatRoomTableView setShowsVerticalScrollIndicator:NO];
    
    self.chatRoomTableView.delegate = self;
    self.chatRoomTableView.dataSource = self;
    
    self.chatRoomTableView.separatorColor = [UIColor clearColor];
    self.chatRoomTableView.estimatedRowHeight = 100;
    self.chatRoomTableView.rowHeight = UITableViewAutomaticDimension;
    [self.view addSubview:self.chatRoomTableView];

}


#pragma mark - UI事件


////头像点击
//-(void)imageClick:(UIButton*)sender{
//    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:dict1[@"userId"],@"userid",[IVTAccountTool getUserId],@"username",[IVTAccountTool getPassword],@"captcha", nil];
//    [IVTHttpTool POST:userInfoUrl parameters:dict success:^(id responseObject) {
//         NSDictionary *userInfo = [responseObject objectForKey:@"data"];
//        if ([[userInfo objectForKey:@"id"] integerValue] == [[IVTAccountTool getUserId] integerValue]) {//本人
//            IVTMeMineUserView *userMine = [[IVTMeMineUserView alloc]initWithFrame:CGRectMake((kScreenWidth - 280)/2, (kScreenHeight - 320)/2, 280, 320)];
//            userMine.entity = userInfo;
//            [self.view addSubview:userMine];
//        }
//        else{
//            IVTMeUserView *user = [[IVTMeUserView alloc]initWithFrame:CGRectMake((kScreenWidth - 280)/2, (kScreenHeight - 320)/2, 280, 320)];
//            user.entity = userInfo;
//            if ([[userInfo objectForKey:@"id"] integerValue] == [_userIdOther integerValue]) {//主播
//                user.isPullStream = YES;
//                user.isZhuBo = YES;
//            }
//            else{
//                user.isPullStream = YES;
//                user.isZhuBo = NO;
//            }
//            user.delegate = self;
//            [self.view addSubview:user];
//        }
//
//        
//
//    }failure:^(NSError *error) {
//        
//    }];
//}

//蜜糖点击
//-(void)mitang:(id)sender{
//    IVTMeSupportVC *supportVC = [[IVTMeSupportVC alloc]init];
//    [self.navigationController pushViewController:supportVC animated:YES];
//    
//    
//}
#pragma mark - 退出
- (void)onQuit:(id)sender
{
    [[XMPPManager sharedInstance] leaveRoomBySelf];
//    [self quitRoomReq];
//    if (ksyLiveShowObj.player.isPlaying) {
        [self.player stop];
        [self.navigationController popViewControllerAnimated:YES];

//    }else{
//        [self.navigationController popViewControllerAnimated:YES];

//    }
}


//  释放第一响应者
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.textView resignFirstResponder];
    [myChatListVC.view removeFromSuperview];
}
//#pragma mark - 消息
//消息点击
-(void)msgBtnClick:(UIButton *)sender
{
    myChatListVC = [[IVTMyChatListViewController alloc]init];
    myChatListVC.isSmall = YES;
    [self addChildViewController:myChatListVC];
    [self.view addSubview:myChatListVC.view];
}
#pragma mark - 分享
//分享按钮点击
- (void)shareBtn:(id)sender
{
    [SVProgressHUD setMinimumDismissTimeInterval:2];
    [SVProgressHUD showSuccessWithStatus:@"暂未开通"];
    
//    _customShareView = [[CustomShareView alloc]init];
//    [self.view addSubview:_customShareView];
//    [_customShareView showInView:self.view];
}

#pragma mark - 停止视频
- (IBAction)onStopVideo:(id)sender
{
    if (_player) {
        
        [_player stop];
        [_player.view removeFromSuperview];
        _player = nil;
        [self StopTimer];
    }
}

#pragma mark - 停止时间
- (void)StopTimer
{
    if (nil == _timer) {
        return;
    }
    [_timer invalidate];
    _timer = nil;

}
#pragma mark - 点击用户头像代理
////用户主页
//-(void)showUserHead{
//    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[IVTAccountTool getUserId],@"username",[IVTAccountTool getPassword],@"captcha",dict1[@"userId"],@"userid", nil];
//    [IVTHttpTool POST:userInfoUrl parameters:dict success:^(id responseObject) {
//        IVTMeUserView *user = [[IVTMeUserView alloc]initWithFrame:CGRectMake((kScreenWidth-349)/2, 209, 349, 296)];
//        _userView = user;
//        user.delegate = self;
//        
//        user.userId = dict1[@"userId"];
//        
//        NSDictionary *userInfo = [responseObject objectForKey:@"data"];
//        
//        NSString *avatar  =[NSString stringWithFormat:@"%@",[userInfo objectForKey:@"avatar"]];
//        
//        [user.userIcon sd_setImageWithURL:[NSURL URLWithString:avatar] placeholderImage:[UIImage imageNamed:@"占位头像"]];
//        
//        NSString *fanCount = [NSString stringWithFormat:@"%@",[userInfo objectForKey:@"fanscount"]];
//        
//        NSString *followCount = [NSString stringWithFormat:@"%@",[userInfo objectForKey:@"followercount"]];
//        
//        user.attentionNum.text =[NSString stringWithFormat:@"我的关注:%@",followCount];
//        
//        user.fansNum.text = [NSString stringWithFormat:@"粉丝总数:%@",fanCount];
//        
//        NSString *giftNum = [NSString stringWithFormat:@"送出礼物:%@",[userInfo objectForKey:@"sentgiftcount"]];
//        user.giftNum.text =giftNum;
//        
//        NSString *mitangNum = [NSString stringWithFormat:@"蜜糖:%@",[userInfo objectForKey:@"mecointotal"]];
//        user.miTang.text =mitangNum;
//        
//        NSString *name = [userInfo objectForKey:@"name"];
//        user.name.text  = name;
//        
//        NSString *sex = [userInfo objectForKey:@"sex"];
//        if ([sex isEqualToString:@"male"]) {
//            user.sex.image = [UIImage imageNamed:@"红包-性别男-拷贝"];
//            
//        }else if([sex isEqualToString:@"female"]){
//            user.sex.image = [UIImage imageNamed:@"我的-性别"];
//        }else {
//            user.sex.image = nil;
//            
//        }
//        
//        NSString *level = [userInfo objectForKey:@"level"];
//        user.lelveNum.text = level;
//        
//        [self.view addSubview:user];
//        
//    }failure:^(NSError *error) {
//        
//    }];
//
//}

//主页
-(void)showUserInfo{
    
    IVTUserInfoCenterViewController *userCenter = [[IVTUserInfoCenterViewController alloc]init];
    userCenter.userId = [NSString stringWithFormat:@"%@",[IVTAccountTool getUserId]];
    [self.navigationController pushViewController:userCenter animated:YES];
    
}

//举报
-(void)showInform{
    informReportViewController *inform = [[informReportViewController alloc]init];
    [self.navigationController pushViewController:inform animated:YES];
    
}




////私信
//-(void)showmessage{
//    
//    [_userView removeFromSuperview];
//    IVTMeSingleChatSmallVC *singleChat = [[IVTMeSingleChatSmallVC alloc]init];
//    
//    [self addChildViewController:singleChat];
//    [self.view addSubview:singleChat.view];
//    
//}

//回复
-(void)showReply{
    
    [_userView removeFromSuperview];
    self.backGround.hidden = NO;
    [self.textField becomeFirstResponder];
    [self.textView becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - slider事件

- (void)update:(NSTimer *)t
{
    if ( 0 == lastCheckTime) {
        lastCheckTime = [self getCurrentTime];
        return;
    }
    if (nil == _player) {
        return;
    }
    double flowSize = [_player readSize];
    
    lastCheckTime = [self getCurrentTime];
    lastSize = flowSize;
    
    [ _mySlider setValue:_player.currentPlaybackTime / _player.duration];
    
    [_pro setProgress:_player.playableDuration / _player.duration animated:NO];
}


- (void) sliderValue:(id)sender{
    UISlider* control = (UISlider*)sender;
    if(control == _mySlider){

        if (_player) {
            _player.currentPlaybackTime = control.value * _player.duration;
            
            NSLog(@"%@",[NSString stringWithFormat:@"%.2f",control.value]);
        }
    }
}









- (void)playOrNotPlay:(id)sender
{
    if (tapCount == 1000) {
        [_chatBtn setBackgroundImage:[UIImage imageNamed:@"播放键"] forState:UIControlStateNormal];
        if (_player) {
            [_player pause];
        }
        tapCount = 2000;
    }else if(tapCount == 2000){
        [_chatBtn setBackgroundImage:[UIImage imageNamed:@"播放中"] forState:UIControlStateNormal];
        tapCount = 1000;
        if (_player) {
            [_player play];
        }
    }
}




//  网络请求
- (void)requestLoad
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setObject:[self.roomInfo objectForKey:@"id"] forKey:@"liveId"];
    [dict setObject:[IVTAccountTool getUserId] forKey:@"username"];
    [dict setObject:[IVTAccountTool getPassword] forKey:@"captcha"];
    [dict setObject:[IVTAccountTool getUserId] forKey:@"userid"];
    
    [[IVTNetworkAPIClient sharedClient] getReplayAdd:dict success:^(NSDictionary *successData) {
        dict1 = [successData objectForKey:@"data"];
        NSLog(@"dict1:%@", dict1);
        _type = [dict1 objectForKey:@"type"];
        _recordAddr = [dict1 objectForKey:@"recordAddr"];
        _videoAddr = [dict1 objectForKey:@"videoAddr"];
        
        NSLog(@"_type:%@\n,_recordAddr:%@\n,_videoAddr%@\n",_type,_recordAddr,_videoAddr);
        
        _player = [[KSYMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:[dict1 objectForKey:@"recordAddr"]]];
        
        _player.controlStyle = MPMovieControlStyleNone;
        [_player.view setFrame: videoView.bounds];  // player's frame must match parent's
        [videoView addSubview: _player.view];
        videoView.autoresizesSubviews = TRUE;
        _player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        _player.shouldAutoplay = TRUE;
        _player.bufferTimeMax = 5;
        _player.scalingMode = MPMovieScalingModeAspectFit;
        _player.shouldEnableKSYStatModule = TRUE;
        [_player prepareToPlay];
        [_headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dict1 objectForKey:@"avatar"]]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"占位头像"]];
        
        NSInteger zhuBoMiTangNumber = [[dict1 objectForKey:@"mecointotal"] integerValue];
        _mitangCountLabel.text = [IVTAccountTool numberFormat:zhuBoMiTangNumber];
        CGFloat widthForMiTangLabel = [IVTThirdFuncTool widthOfLabelWithString:[NSString stringWithFormat:@"%ld",(long)zhuBoMiTangNumber] sizeOfFont:15 height:15];
        [_mitangBackGroundImg mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(71+widthForMiTangLabel, 23));
        }];
        [_mitangCountLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(widthForMiTangLabel + 1, 15));
        }];
        [_mitangMoreImg mas_updateConstraints:^(MASConstraintMaker *make) {
            
        }];
        
//        _viewCount.text = [NSString stringWithFormat:@"%@",dict1[@"audienceNowNum"]];
        _viewCount.text = @"1";
        
        
        if (!_player) {
            [_player play];
            [self StartTimer];
            
        }
        lastSize = 0.0;

    } error:^(NSDictionary *successData) {

    } failure:^(NSError *failureError) {
        LRLog(@"failureError:%@",failureError);
    }];
}

- (void)StartTimer
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(update:) userInfo:nil repeats:YES];
}




- (NSTimeInterval) getCurrentTime{
    return [[NSDate date] timeIntervalSince1970];
}

- (void)setupObservers
{
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(handlePlayerNotify:)
                                                name:(MPMediaPlaybackIsPreparedToPlayDidChangeNotification)
                                              object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(handlePlayerNotify:)
                                                name:(MPMoviePlayerPlaybackStateDidChangeNotification)
                                              object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(handlePlayerNotify:)
                                                name:(MPMoviePlayerPlaybackDidFinishNotification)
                                              object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(handlePlayerNotify:)
                                                name:(MPMoviePlayerLoadStateDidChangeNotification)
                                              object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(handlePlayerNotify:)
                                                name:(MPMovieNaturalSizeAvailableNotification)
                                              object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(handlePlayerNotify:)
                                                name:(MPMoviePlayerFirstVideoFrameRenderedNotification)
                                              object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(handlePlayerNotify:)
                                                name:(MPMoviePlayerFirstAudioFrameRenderedNotification)
                                              object:nil];
}
#pragma mark - 移除通知
- (void)releaseObservers
{
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification
                                                 object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:MPMoviePlayerPlaybackStateDidChangeNotification
                                                 object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:MPMoviePlayerPlaybackDidFinishNotification
                                                 object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:MPMoviePlayerLoadStateDidChangeNotification
                                                 object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:MPMovieNaturalSizeAvailableNotification
                                                 object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:MPMoviePlayerFirstVideoFrameRenderedNotification
                                                 object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:MPMoviePlayerFirstAudioFrameRenderedNotification
                                                 object:nil];
}
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
    if (receivedEvent.type == UIEventTypeRemoteControl) {
        
        switch (receivedEvent.subtype) {
                
            case UIEventSubtypeRemoteControlPlay:
                [_player play];
                NSLog(@"play");
                break;
                
            case UIEventSubtypeRemoteControlPause:
                [_player pause];
                NSLog(@"pause");
                break;
                
            case UIEventSubtypeRemoteControlPreviousTrack:
                
                break;
                
            case UIEventSubtypeRemoteControlNextTrack:
                
                break;
                
            default:
                break;
        }
    }
}

-(void)handlePlayerNotify:(NSNotification*)notify
{
    if (!_player) {
        return;
    }
    if (MPMediaPlaybackIsPreparedToPlayDidChangeNotification ==  notify.name) {
        _serverIp = [_player serverAddress];
        
    }
    if (MPMoviePlayerPlaybackStateDidChangeNotification ==  notify.name) {
        
    }
    if (MPMoviePlayerLoadStateDidChangeNotification ==  notify.name) {
        
        if (MPMovieLoadStateStalled & _player.loadState) {
            
        }
        
        if (_player.bufferEmptyCount &&
            (MPMovieLoadStatePlayable & _player.loadState ||
             MPMovieLoadStatePlaythroughOK & _player.loadState)){
//                
//                NSString *message = [[NSString alloc]initWithFormat:@"loading occurs, %d - %0.3fs",
//                                     (int)_player.bufferEmptyCount,
//                                     _player.bufferEmptyDuration];
//                [self toast:message];
            }
    }
}


#pragma mark - Table view data source

//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//
//    return self.messages.count;
//}
//
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    XMPPRoomMessageCoreDataStorageObject *message = self.messages[indexPath.row];
//
//    
//    static NSString *CellIdentifier = @"cell";
//    
//    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//    }
//    
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.textLabel.textColor = [UIColor whiteColor];
//    cell.textLabel.font = [UIFont YXFontOfSize:14];
//
//    cell.textLabel.numberOfLines = 0;
//    cell.textLabel.text = message.message.body;
//    cell.imageView.image = [UIImage imageNamed: message.isFromMe?@"等级黄色":@"等级紫色"];
//
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    return cell;
//}


////  键盘发送消息
//- (void)send:(NSIndexPath *)indexPath
//{
//    [self.view endEditing:YES];
//    
//    NSMutableDictionary *Dict = [[NSMutableDictionary alloc] init];
//    
//    [Dict setObject:@"0x1222" forKey:@"msgType"];
//    [Dict setObject:@"100" forKey:@"msgNums"];
//    [Dict setObject:@"test" forKey:@"msgBody"];
//    [Dict setObject:@"送了小花" forKey:@"msgAdd"];
//
//
//    
//    XMPPMessage *messageSend = [[XMPPMessage alloc] initWithName:@"message"];
//    
//    [messageSend addAttributeWithName:@"to" stringValue:self.roomJID.bare];
//    [messageSend addAttributeWithName:@"type" stringValue:@"groupchat"];
//    //  将转变的JSON传送给message,包含四个关键字
////    [messageSend addBody:[self dictionaryToJson:Dict]];
//    [messageSend addBody:[Dict objectForKey:@"mesBody"]];
////        [message addBody:];
//    [self.tool.stream sendElement:messageSend];
//    [NSThread sleepForTimeInterval:0.1];
//    [self fetchMessage];
//    self.textView.text = nil;
//    self.backGround.hidden = YES;
//}

//  字典转JSON

- (NSString*)dictionaryToJson:(NSDictionary *)dic

{
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}


//- (void)tableView:(UITableView *)tableView commitEditingStyle:
//(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSUInteger row = [indexPath row];
// if(editingStyle == UITableViewCellEditingStyleInsert ){
//        NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];
//        [self.messages insertObject:@"inset new Cell" atIndex:row];
//        [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationMiddle];
//    }
//}



- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor clearColor];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 20;
}

//- (void)fetchReadJID {
//    NSManagedObjectContext *context = self.tool.roomCoreDataStorage.mainThreadManagedObjectContext;
//    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"XMPPRoomOccupantCoreDataStorageObject"];
//    NSArray *occupants = [context executeFetchRequest:request error:nil];
//    for (XMPPRoomOccupantCoreDataStorageObject *occupant in occupants) {
//        self.realJIDs[occupant.jidStr] = occupant.realJID;
//    }
//}


- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
    [NSThread sleepForTimeInterval:0.1];
//    [self fetchMessage];
}



@end