MOLVideoViewController.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // MOLVideoViewController.m
  3. // STLBGVideo
  4. //
  5. // Created by StoneLeon on 16/1/13.
  6. // Copyright © 2016年 StoneLeon. All rights reserved.
  7. //
  8. #import "MOLVideoViewController.h"
  9. #import "MOLVideoFunctions.h"
  10. #define CurrentSystemVersion ([[[UIDevice currentDevice] systemVersion] floatValue])
  11. @interface MOLVideoViewController ()
  12. @property (nonatomic, strong) AVPlayer *player;
  13. @property (nonatomic, strong) AVPlayerLayer *playerLayer;
  14. @property (nonatomic,assign) BOOL isLoop;
  15. @end
  16. @implementation MOLVideoViewController
  17. #pragma mark - Player
  18. - (void)getPlayerNotifications {
  19. // 添加播放结束通知,循环播放
  20. [[NSNotificationCenter defaultCenter] addObserver:self
  21. selector:@selector(playerDidFinishPlaying:)
  22. name:AVPlayerItemDidPlayToEndTimeNotification
  23. object:self.player.currentItem];
  24. // APP进入活跃态的通知
  25. ReceiveNotification(@selector(toCheckThePlayerStatusAndGoPlay), UIApplicationDidBecomeActiveNotification)
  26. }
  27. - (void)toCheckThePlayerStatusAndGoPlay{
  28. //APP进入活跃状态, 检查播放器, 判断是否在播放, 如果暂停则继续播放
  29. if(_player){
  30. if (_player.rate != 0 && _player.error == nil){
  31. MOLogV(@"AVPlayer is playing");
  32. }
  33. else{
  34. MOLogV(@"AVPlayer is not playing");
  35. [_player play];
  36. }
  37. }
  38. }
  39. - (void)preparePlayback {
  40. if (self.player == nil) {
  41. NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[MOLVideoFunctions getVideoUrl] ofType:[MOLVideoFunctions getVideoType]]];
  42. // 初始化 AVPlayer
  43. AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:videoURL];
  44. self.player = [AVPlayer playerWithPlayerItem:playerItem];
  45. // 创建 AVPlayerLayer 并设置其属性
  46. self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
  47. self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  48. self.playerLayer.frame = CGRectMake(0.0, 0.0, SCREENWIDTH, SCREENHEIGHT);
  49. // 将 AVPlayerLayer 添加到视图的底层
  50. [self.view.layer insertSublayer:self.playerLayer atIndex:0];
  51. // 开始播放
  52. [self.player play];
  53. }
  54. }
  55. - (void)dealloc {
  56. // 停止并释放 AVPlayer
  57. [self.player pause];
  58. [self.player.currentItem cancelPendingSeeks];
  59. [self.player.currentItem.asset cancelLoading];
  60. self.player = nil;
  61. }
  62. // 播放结束通知的处理方法
  63. - (void)playerDidFinishPlaying:(NSNotification *)notification {
  64. // 将播放头设置回视频的起始位置,实现循环播放
  65. [self.player seekToTime:kCMTimeZero];
  66. [self.player play];
  67. }
  68. #pragma mark - life cycle
  69. - (void)viewDidLoad {
  70. [super viewDidLoad];
  71. if ([MOLVideoFunctions getUrlInfo] != nil) {
  72. self.isLoop = [MOLVideoFunctions getLoopMode];
  73. [self preparePlayback];
  74. }
  75. }
  76. - (void)viewWillAppear:(BOOL)animated {
  77. [super viewWillAppear:animated];
  78. [self getPlayerNotifications];
  79. [self.player play];
  80. }
  81. - (void)viewWillDisappear:(BOOL)animated {
  82. [super viewWillDisappear:animated];
  83. [[NSNotificationCenter defaultCenter] removeObserver:self];
  84. }
  85. - (void)viewDidDisappear:(BOOL)animated {
  86. [super viewDidDisappear:animated];
  87. [self.player pause];
  88. }
  89. @end