视频播放器

简单的记录一下iOS原生API视频播放

引入头文件

#import <AVFoundation/AVFoundation.h>

三大要素
/** playerLayer */
@property(nonatomic,strong)AVPlayerLayer *playerLayer;
/** player */
@property(nonatomic,strong)AVPlayer *player;
/** playerItem */
@property(nonatomic,strong)AVPlayerItem *playerItem;
然后就是几句代码
    self.player = [[AVPlayer alloc]init];
    
//    声音
    self.player.volume = 1.0;
        
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    
    self.playerLayer.frame = self.view.frame;
    
    [self.view.layer addSublayer:self.playerLayer];
    
    self.playerItem = [AVPlayerItem playerItemWithURL:self.url];

    [self.player replaceCurrentItemWithPlayerItem:self.playerItem];
    
    [self Repeats];
    
    [self.player play];
- (void)Repeats
{
    // 视频播放完的系统通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
}
通过通知实现反复播放
- (void)playbackFinished:(NSNotification *)notification
{
    [self.player seekToTime:CMTimeMake(0, 1)];
    
    [self.player play];
}

基本功能就这么多-_-!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容