MoviePlayerViewController.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // MoviePlayerViewController.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by 王昭威 on 2023/1/24.
  6. //
  7. #import "MoviePlayerViewController.h"
  8. @interface MoviePlayerViewController ()
  9. @property (nonatomic, strong) AVPlayerLayer* videoLayer;
  10. @property (nonatomic, strong) AVPlayerItem* videoItem;
  11. @property (nonatomic, assign) BOOL broken;
  12. @end
  13. @implementation MoviePlayerViewController
  14. - (void)replaceWith:(NSURL *)fileURL{
  15. self.movieFileURL = fileURL;
  16. self.videoItem = [AVPlayerItem playerItemWithURL:self.movieFileURL];
  17. [self.player replaceCurrentItemWithPlayerItem:self.videoItem];
  18. }
  19. - (void)prepareVideoPlayer{
  20. self.videoItem = [AVPlayerItem playerItemWithURL:self.movieFileURL];
  21. self.player = [AVPlayer playerWithPlayerItem:self.videoItem];
  22. self.videoLayer.videoGravity = AVLayerVideoGravityResizeAspect;
  23. self.videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
  24. [self.videoView.layer addSublayer:self.videoLayer];
  25. }
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. // Do any additional setup after loading the view.
  29. self.broken = NO;
  30. [self prepareVideoPlayer];
  31. NSError* err = nil;
  32. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&err];
  33. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
  34. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
  35. }
  36. - (void)willEnterForeground: (NSNotification*)noti{
  37. if(self.broken){
  38. [self.player play];
  39. self.broken = NO;
  40. }
  41. }
  42. - (void)willEnterBackground: (NSNotification*)noti{
  43. if(self.player.timeControlStatus == AVPlayerTimeControlStatusPlaying){
  44. [self.player pause];
  45. self.broken = YES;
  46. }
  47. }
  48. - (void)viewWillDisappear:(BOOL)animated{
  49. [super viewWillDisappear:animated];
  50. if(self.player.timeControlStatus == AVPlayerTimeControlStatusPlaying){
  51. [self.player pause];
  52. self.broken = YES;
  53. }
  54. }
  55. - (void)viewDidAppear:(BOOL)animated{
  56. [super viewDidAppear:animated];
  57. if(self.broken){
  58. self.broken = NO;
  59. [self.player play];
  60. }
  61. }
  62. - (void)viewDidLayoutSubviews{
  63. [super viewDidLayoutSubviews];
  64. self.videoLayer.frame = self.videoView.layer.bounds;
  65. }
  66. - (void)dealloc{
  67. [[NSNotificationCenter defaultCenter] removeObserver:self];
  68. }
  69. /*
  70. #pragma mark - Navigation
  71. // In a storyboard-based application, you will often want to do a little preparation before navigation
  72. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  73. // Get the new view controller using [segue destinationViewController].
  74. // Pass the selected object to the new view controller.
  75. }
  76. */
  77. @end