iOS小视频播放器AVPlayer

在项目中用到了小视频播放,本来要使用类似于今日头条的播放器,无奈BOSS说最简单的播放器就行,所以就自己封装了一个简单的播放器,只需要传个视频的地址就可以,无论是本地的 还是网络的地址都行,下面请看实现吧
.h文件

#import <UIKit/UIKit.h>

@interface JYPostVideoViewController : UIViewController

@property (nonatomic , strong) NSURL *videoUrl;

@end

.m文件

#import "JYPostVideoViewController.h"
#import <AVFoundation/AVFoundation.h>


@interface JYPostVideoViewController ()

@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerItem *playerItem;

@end

@implementation JYPostVideoViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor blackColor];
    
    
    
    
    
    //    [NOAlertManager showHUDWithStatus:@"加载中"];
    //    self.player.status
}

-(void)setVideoUrl:(NSURL *)videoUrl{
    
    _videoUrl = videoUrl;
    CGRect playerFrame = self.view.frame;
    AVURLAsset *asset = [AVURLAsset assetWithURL:videoUrl];
    
    _playerItem = [AVPlayerItem playerItemWithAsset:asset];
    [_playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil];
    
    self.player = [[AVPlayer alloc]initWithPlayerItem:_playerItem];
    
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    
    playerLayer.frame = playerFrame;
    
    //AVLayerVideoGravityResizeAspect
    //AVLayerVideoGravityResizeAspectFill
    //AVLayerVideoGravityResize
    
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    
    [self.view.layer addSublayer:playerLayer];
    
    
    
    [self.player play];
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
    switch (status) {
        case AVPlayerStatusUnknown:
        {
            
        }
            break;
        case AVPlayerStatusReadyToPlay:
        {
            //            [NOAlertManager dismissHUD];
            //            self.backgroundImageView.backgroundColor = [UIColor clearColor];
            //            self.backgroundImageView.image = [UIImage cn_imageWithColor:[UIColor clearColor]];
            //            self.backgroundImageView.image = nil;
        }
            break;
        case AVPlayerStatusFailed:
        {
            
        }
            break;
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)dealloc{
    [self.playerItem removeObserver:self forKeyPath:@"status"];
    [self.player pause];
    self.player = nil;
}

@end

要是你有播放视频的需求,赶紧拿去吧~~~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,505评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 16,009评论 4 61
  • 本篇文章介绍一种设计模式——命令模式。本篇文章内容参考《JAVA与模式》之命令模式。 一、命令模式的概念 命令模式...
    Ruheng阅读 1,636评论 3 19
  • 我选择了秋天, 却没有收获。 我选择了蓝天, 却不能成为风景。 我选择了独立, 却陷入孤单的守望。 我选择了这一刻...
    昊水长天阅读 156评论 0 1
  • 自《陪安东尼度过漫长岁月》开播以来,其主题曲《陪你度过漫长岁月》着实火了一把,几乎每次去K歌都能听见,这不,上次一...
    SSSSSS姑娘啊阅读 188评论 2 0

友情链接更多精彩内容