视频播放器

2017-04-24  本文已影响17人  wbtuxi

简单的记录一下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];
}

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

上一篇下一篇

猜你喜欢

热点阅读