| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- //
- // TXFullVideoView.m
- // TeacherDaya
- //
- // Created by 王智 on 2023/4/14.
- // Copyright © 2023 DayaMusic. All rights reserved.
- //
- #import "TXFullVideoView.h"
- #import "ClassroomService.h"
- #import "TXFullVideoCell.h"
- @interface TXFullVideoView ()<UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
- @property (nonatomic, strong) UICollectionView *videoView;
- @property (nonatomic, strong) NSMutableArray *videoDataSource;
- @property (nonatomic, strong) dispatch_queue_t videoListQueue;
- @property (nonatomic, assign) CGRect viewFrame;
- @property (nonatomic, assign) CGSize cellSize;
- @end
- @implementation TXFullVideoView
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- _viewFrame = CGRectMake(0, 0, KLandscapeWidth, KLandscapeHeight);
- [self addSubview:self.videoView];
- [self addCancleButton];
- }
- return self;
- }
- - (void)addCancleButton {
- UIButton *cancleButton = [[UIButton alloc] initWithFrame:CGRectMake(KLandscapeWidth - 50, 10, 40, 40)];
- [cancleButton setImage:[UIImage imageNamed:@"videoMask_delete"] forState:UIControlStateNormal];
- [cancleButton addTarget:self action:@selector(cancleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:cancleButton];
- [self bringSubviewToFront:cancleButton];
- }
- - (void)cancleButtonAction:(UIButton *)sender {
- if (self.delegate && [self.delegate respondsToSelector:@selector(cancleFullVideoList)]) {
- [self.delegate cancleFullVideoList];
- }
- }
- - (void)showInView:(UIView *)view {
- [view addSubview:self];
- if ([ClassroomService sharedService].currentRoom.isPlayBeat || [ClassroomService sharedService].currentRoom.isPlaySong) {
- // 记录当前播放的学生音频
- self.currentPlayUserId = [ClassroomService sharedService].currentRoom.currentPlayUserId;
- }
- }
- - (void)showVideoList {
- dispatch_async(self.videoListQueue, ^{
- NSArray *tempArray = [ClassroomService sharedService].currentRoom.memberList;
- // NSMutableArray *array = [NSMutableArray arrayWithArray:[ClassroomService sharedService].currentRoom.memberList];
- // [array addObjectsFromArray:tempArray];
- // [array addObjectsFromArray:tempArray];
- // [array addObjectsFromArray:tempArray];
- //// [array addObjectsFromArray:tempArray];
- // tempArray = [NSArray arrayWithArray:array];
- if (tempArray.count <= 0) {
- return;
- }
- NSSortDescriptor * des = [[NSSortDescriptor alloc] initWithKey:@"joinTime" ascending:YES];
- NSMutableArray *sortArray = [[tempArray sortedArrayUsingDescriptors:@[des]] mutableCopy];
- RoomMember *currentMember = [ClassroomService sharedService].currentRoom.currentMember;
- RoomMember *teacher = [ClassroomService sharedService].currentRoom.teacher;
- __block NSInteger teacherIdx = -1;
- __block NSInteger meIdx = -1;
- NSMutableIndexSet *set = [[NSMutableIndexSet alloc]init];
-
- for (int i = 0; i < [sortArray count]; i++ ) {
- RoomMember *member = (RoomMember *)[sortArray objectAtIndex:i];
- if ([member.userId isEqualToString:currentMember.userId]) {
- meIdx = i;
- }else {
- if (member.role == RoleTeacher) {
- teacherIdx = i;
- }
- if (member.role == RoleAudience) {
- [set addIndex:i];//旁观者不能被任何他人看见,如果自己是旁观者自己可以看见自己
- }
- }
- }
-
- NSMutableArray *lastArray = [[NSMutableArray alloc] init];
- if (teacherIdx >= 0) {
- [lastArray addObject:teacher];
- [set addIndex:teacherIdx];
- }
- if (meIdx >= 0) {
- [lastArray addObject:currentMember];
- [set addIndex:meIdx];
- }
- if (set.count > 0) {
- [sortArray removeObjectsAtIndexes:set];
- }
- [lastArray addObjectsFromArray:sortArray];
- dispatch_async(dispatch_get_main_queue(), ^{
- self.videoDataSource = [lastArray mutableCopy];
- [self.videoView reloadData];
- });
- });
- }
- - (void)updateUserVideo:(NSString *)userId {
- dispatch_async(self.videoListQueue, ^{
- for(int i=0;i < self.videoDataSource.count; i++) {
- RoomMember *member = self.videoDataSource[i];
- if([member.userId isEqualToString:userId]) {
- NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
- dispatch_async(dispatch_get_main_queue(), ^{
- TXFullVideoCell * cell = (TXFullVideoCell *)[self.videoView cellForItemAtIndexPath:index];
- if (cell) {
- [self.videoView reloadItemsAtIndexPaths:@[index]];
- }
- });
- }
- }
- });
- }
- - (void)updateUserQuality:(NSString *)userId netWorkingStatus:(TXNetWorkingStatus)quality {
- dispatch_async(self.videoListQueue, ^{
- for(int i = 0;i < self.videoDataSource.count; i++) {
- RoomMember *member = self.videoDataSource[i];
- if([member.userId isEqualToString:userId]) {
- NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0];
- dispatch_async(dispatch_get_main_queue(), ^{
- TXFullVideoCell * cell = (TXFullVideoCell *)[self.videoView cellForItemAtIndexPath:index];
- if (cell) {
- [cell updateQuality:quality];
- }
- });
- }
- }
- });
- }
- - (void)updateMicStatus:(NSString *)userId volume:(NSInteger)volume {
- dispatch_async(self.videoListQueue, ^{
- for(int i = 0;i < self.videoDataSource.count; i++) {
- RoomMember *member = self.videoDataSource[i];
- if([member.userId isEqualToString:userId]) {
- RoomMember *currentMember = [[ClassroomService sharedService].currentRoom getMember:userId];
- NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0];
- dispatch_async(dispatch_get_main_queue(), ^{
- TXFullVideoCell * cell = (TXFullVideoCell *)[self.videoView cellForItemAtIndexPath:index];
- if (cell) {
- [cell updateUserVolume:volume isCloseMic:!currentMember.microphoneEnable];
- }
- });
- }
- }
- });
- }
- #pragma mark ----- collection view data source
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
- return 1;
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return self.videoDataSource.count;
- }
- - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
-
- TXFullVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TXFullVideoCell" forIndexPath:indexPath];
- cell.cellSize = self.cellSize;
- [cell setModel:[self.videoDataSource objectAtIndex:indexPath.row] displayUserId:self.displayUserId];
-
- return cell;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- // 显示大屏
- RoomMember *mem = [self.videoDataSource objectAtIndex:indexPath.row];
- if (self.delegate && [self.delegate respondsToSelector:@selector(fullVideoListView:didTap:)]) {
- self.displayUserId = mem.userId;
- [self.delegate fullVideoListView:self didTap:mem];
- }
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- switch (self.videoDataSource.count) {
- case 1:
- {
- self.cellSize = CGSizeMake((self.viewFrame.size.width - 30) / 2.0f, self.viewFrame.size.height - 20);
- return self.cellSize;
- }
- break;
- case 2:
- {
- self.cellSize = CGSizeMake((_viewFrame.size.width - 30) / 2.0f, _viewFrame.size.height - 20);
- return self.cellSize;
- }
- break;
- case 3:
- case 4:
- {
- self.cellSize = CGSizeMake((_viewFrame.size.width - 30) / 2.0f, (_viewFrame.size.height - 30) / 2.0f);
- return self.cellSize;
- }
- break;
- case 5:
- case 6:
- {
- self.cellSize = CGSizeMake((_viewFrame.size.width - 40) / 3.0f, (_viewFrame.size.height - 30) / 2.0f);
- return self.cellSize;
- }
- break;
- default:
- self.cellSize = CGSizeMake((_viewFrame.size.width - 40) / 3.0f, (_viewFrame.size.height - 40) / 3.0f);
- return self.cellSize;
- }
- }
- #pragma mark ---- lazying
- - (UICollectionView *)videoView {
- if (!_videoView) {
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
- layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
- _videoView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KLandscapeWidth, KLandscapeHeight) collectionViewLayout:layout];
- _videoView.backgroundColor = HexRGB(0xffffff);
- _videoView.delegate = self;
- _videoView.dataSource = self;
- _videoView.bounces = NO;
- _videoView.showsVerticalScrollIndicator = NO;
- _videoView.showsHorizontalScrollIndicator = NO;
- [_videoView registerNib:[UINib nibWithNibName:@"TXFullVideoCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"TXFullVideoCell"];
- }
- return _videoView;
- }
- - (NSMutableArray *)videoDataSource {
- if (!_videoDataSource) {
- _videoDataSource = [NSMutableArray array];
- }
- return _videoDataSource;
- }
- - (dispatch_queue_t)videoListQueue {
- if (!_videoListQueue) {
- _videoListQueue = dispatch_queue_create("cn.daya.fullVideo.videolist", DISPATCH_QUEUE_SERIAL);
- }
- return _videoListQueue;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|