美文网首页
视频播放器

视频播放器

作者: wbtuxi | 来源:发表于2017-04-24 09:23 被阅读17次

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

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

    相关文章

      网友评论

          本文标题:视频播放器

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