CollectionViewPlayerCell.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // CollectionViewPlayerCell.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by 王昭威 on 2023/1/24.
  6. //
  7. #import "CollectionViewPlayerCell.h"
  8. #import "MoviePlayerViewController.h"
  9. #import <Masonry/Masonry.h>
  10. #import "MovieListCellModel.h"
  11. #import "OperationNodeMovie.h"
  12. @interface CollectionViewPlayerCell()
  13. @property (nonatomic, readonly, nullable) NSURL* fileURL;
  14. @end
  15. @implementation CollectionViewPlayerCell
  16. - (UIView *)coverView{
  17. return self.playerController.view;
  18. }
  19. - (NSURL *)fileURL{
  20. return ((MovieListCellModel*)self.cellModel).movieModel.fileUrl;
  21. }
  22. - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
  23. return CGRectContainsPoint(self.imageView.frame, point);
  24. }
  25. - (AVPlayer*)play{
  26. if(self.fileURL == nil){
  27. return nil;
  28. }
  29. if(self.playerController.player.timeControlStatus == AVPlayerTimeControlStatusPlaying){
  30. return self.playerController.player;
  31. }
  32. AVPlayer* player = self.playerController.player;
  33. if(player.currentItem != nil &&
  34. player.currentItem.status == AVPlayerItemStatusReadyToPlay &&
  35. player.currentItem.currentTime.value / player.currentItem.currentTime.timescale >= player.currentItem.duration.value / player.currentItem.duration.timescale){
  36. [self.playerController replaceWith:self.fileURL];
  37. }
  38. [self.playerController.player play];
  39. self.playIconView.hidden = YES;
  40. return self.playerController.player;
  41. }
  42. - (void)stop{
  43. self.playIconView.hidden = NO;
  44. [self.playerController.player pause];
  45. }
  46. - (void)setCellModel:(PhotoListCellModel *)cellModel{
  47. [super setCellModel:cellModel];
  48. if([cellModel isKindOfClass:[MovieListCellModel class]]){
  49. self.playerController.movieFileURL = ((MovieListCellModel*)self.cellModel).movieModel.fileUrl;
  50. [self.playerController replaceWith:self.playerController.movieFileURL];
  51. }
  52. }
  53. - (void)awakeFromNib{
  54. [super awakeFromNib];
  55. self.imageView.hidden = YES;
  56. UIStoryboard* sb = [UIStoryboard storyboardWithName:[PCSTools shared].mainStoryboardName bundle:[PCSTools sdkBundle]];
  57. MoviePlayerViewController* playerController = [sb instantiateViewControllerWithIdentifier:@"play-movie-on-cell"];
  58. [playerController loadViewIfNeeded];
  59. _playerController = playerController;
  60. playerController.movieFileURL = ((MovieListCellModel*)self.cellModel).movieModel.fileUrl;
  61. [self.contentView insertSubview:self.playerController.view belowSubview:self.imageView];
  62. [self.playerController viewDidLoad];
  63. self.playerController.view.translatesAutoresizingMaskIntoConstraints = NO;
  64. }
  65. - (void)updateConstraints{
  66. [super updateConstraints];
  67. [self.playerController.view mas_remakeConstraints:^(MASConstraintMaker *make) {
  68. make.width.equalTo(@(CGRectGetWidth(self.contentView.frame)));
  69. make.height.equalTo(@(CGRectGetHeight(self.contentView.frame)));
  70. }];
  71. }
  72. - (void)layoutSubviews{
  73. [super layoutSubviews];
  74. CGFloat x = CGRectGetWidth([UIScreen mainScreen].bounds) * 0.5;
  75. CGFloat y = CGRectGetHeight([UIScreen mainScreen].bounds) * 0.5 - 40;
  76. self.playIconView.center = CGPointMake(x, y);
  77. [self.contentView bringSubviewToFront:self.playIconView];
  78. }
  79. - (void)dealloc{
  80. }
  81. @end