KZVideoPlayer.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // KZVideoPlayer.m
  3. // KZWeChatSmallVideo_OC
  4. //
  5. // Created by HouKangzhu on 16/7/21.
  6. // Copyright © 2016年 侯康柱. All rights reserved.
  7. //
  8. #import "KZVideoPlayer.h"
  9. #import <AVFoundation/AVFoundation.h>
  10. @implementation KZVideoPlayer {
  11. AVPlayer *_player;
  12. UIView *_ctrlView;
  13. CALayer *_playStatus;
  14. BOOL _isPlaying;
  15. }
  16. - (instancetype)initWithFrame:(CGRect)frame videoUrl:(NSURL *)videoUrl{
  17. if (self = [super initWithFrame:frame]) {
  18. _autoReplay = YES;
  19. _videoUrl = videoUrl;
  20. [self setupSubViews];
  21. }
  22. return self;
  23. }
  24. - (void)play {
  25. if (_isPlaying) {
  26. return;
  27. }
  28. [self tapAction];
  29. }
  30. - (void)stop {
  31. if (_isPlaying) {
  32. [self tapAction];
  33. }
  34. }
  35. - (void)setupSubViews {
  36. AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:_videoUrl];
  37. _player = [AVPlayer playerWithPlayerItem:playerItem];
  38. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playEnd) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
  39. AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
  40. playerLayer.frame = self.bounds;
  41. playerLayer.videoGravity = AVLayerVideoGravityResize;
  42. [self.layer addSublayer:playerLayer];
  43. _ctrlView = [[UIView alloc] initWithFrame:self.bounds];
  44. _ctrlView.backgroundColor = [UIColor clearColor];
  45. [self addSubview:_ctrlView];
  46. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
  47. [_ctrlView addGestureRecognizer:tap];
  48. [self setupStatusView];
  49. [self tapAction];
  50. }
  51. - (void)setupStatusView {
  52. CGPoint selfCent = CGPointMake(self.bounds.size.width/2+10, self.bounds.size.height/2);
  53. CGFloat width = 40;
  54. CGMutablePathRef path = CGPathCreateMutable();
  55. CGPathMoveToPoint(path, nil, selfCent.x - width/2, selfCent.y - width/2);
  56. CGPathAddLineToPoint(path, nil, selfCent.x - width/2, selfCent.y + width/2);
  57. CGPathAddLineToPoint(path, nil, selfCent.x + width/2 - 4, selfCent.y);
  58. CGPathAddLineToPoint(path, nil, selfCent.x - width/2, selfCent.y - width/2);
  59. CGColorRef color = [UIColor colorWithRed: 1.0 green: 1.0 blue: 1.0 alpha: 0.5].CGColor;
  60. CAShapeLayer *trackLayer = [CAShapeLayer layer];
  61. trackLayer.frame = self.bounds;
  62. trackLayer.strokeColor = [UIColor clearColor].CGColor;
  63. trackLayer.fillColor = color;
  64. trackLayer.opacity = 1.0;
  65. trackLayer.lineCap = kCALineCapRound;
  66. trackLayer.lineWidth = 1.0;
  67. trackLayer.path = path;
  68. [_ctrlView.layer addSublayer:trackLayer];
  69. _playStatus = trackLayer;
  70. CGPathRelease(path);
  71. }
  72. - (void)tapAction {
  73. if (_isPlaying) {
  74. [_player pause];
  75. }
  76. else {
  77. [_player play];
  78. }
  79. _isPlaying = !_isPlaying;
  80. _playStatus.hidden = !_playStatus.hidden;
  81. }
  82. - (void)playEnd {
  83. if (!_autoReplay) {
  84. return;
  85. }
  86. [_player seekToTime:kCMTimeZero completionHandler:^(BOOL finished) {
  87. [_player play];
  88. }];
  89. }
  90. - (void)removeFromSuperview {
  91. [_player.currentItem cancelPendingSeeks];
  92. [_player.currentItem.asset cancelLoading];
  93. [[NSNotificationCenter defaultCenter] removeObserver:self ];
  94. [super removeFromSuperview];
  95. }
  96. - (void)dealloc {
  97. // NSLog(@"player dalloc");
  98. }
  99. @end