UIView+addVideoToView.m 1.1 KB

123456789101112131415161718192021222324252627282930
  1. //
  2. // UIView+addVideoToView.m
  3. // 扩展Demo
  4. //
  5. // Created by Hello on 2018/4/19.
  6. // Copyright © 2018年 Hello. All rights reserved.
  7. //
  8. #import "UIView+addVideoToView.h"
  9. #import "VideoPlayerView.h"
  10. @implementation UIView (addVideoToView)
  11. -(void)addVideoWithPlayerLayerFrame:(CGRect)frame withPlayerItemUrlString:(NSString *)url complete:(void (^)(AVPlayer *))block{
  12. VideoPlayerView *playerView = [VideoPlayerView sharedVideoViewWithFrame:frame];
  13. [playerView.playerLayer removeFromSuperlayer];
  14. [playerView removeFromSuperview];
  15. NSURL *mediaURL = [NSURL URLWithString:url];
  16. playerView.playerItem = [AVPlayerItem playerItemWithURL:mediaURL];
  17. playerView.videoPlayer = [[AVPlayer alloc] initWithPlayerItem:playerView.playerItem];
  18. playerView.playerLayer = [AVPlayerLayer playerLayerWithPlayer:playerView.videoPlayer];
  19. playerView.playerLayer.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
  20. [playerView.layer addSublayer:playerView.playerLayer];
  21. [playerView.videoPlayer pause];
  22. [self addSubview:playerView];
  23. block(playerView.videoPlayer);
  24. }
  25. @end