美文网首页
iOS小视频播放器AVPlayer

iOS小视频播放器AVPlayer

作者: fighter0501 | 来源:发表于2017-04-25 15:09 被阅读0次

    在项目中用到了小视频播放,本来要使用类似于今日头条的播放器,无奈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
    

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

    相关文章

      网友评论

          本文标题:iOS小视频播放器AVPlayer

          本文链接:https://www.haomeiwen.com/subject/kzuvzttx.html