美文网首页
AVPlayer和AVPlayerViewController

AVPlayer和AVPlayerViewController

作者: William_ | 来源:发表于2018-06-07 15:34 被阅读18次

    AVPlayer

        NSURL *url = [NSURL URLWithString:@"http://ips.ifeng.com/video.ifeng.com/video04/2011/03/24/480x360_offline20110324.mp4"];
        
        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
        
        self.player = [AVPlayer playerWithPlayerItem:item];
        
        _playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
        
        _playerLayer.bounds = CGRectMake(0, 0, self.view.bounds.size.width, 300);
        
        _playerLayer.position = CGPointMake(CGRectGetMidX(self.view.bounds), 64 + CGRectGetMidY(_playerLayer.bounds) + 30);
        
        _playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        
        NSLog(@"%lld",item.duration.value);
        
        [self.player play];
        
        [self.view.layer addSublayer:_playerLayer];
    

    AVPlayerViewController

        NSURL *url = [NSURL URLWithString:@"http://ips.ifeng.com/video.ifeng.com/video04/2011/03/24/480x360_offline20110324.mp4"];
        
        if (self.playerController) {
            self.playerController = nil;
        }
        
        self.playerController = [[AVPlayerViewController alloc] init];
        self.playerController.player = [AVPlayer playerWithURL:url];
        //拉伸模式
        self.playerController.videoGravity = AVLayerVideoGravityResizeAspect;
        //是否显示媒体播放组件
        self.playerController.showsPlaybackControls = YES;
        
        
        self.playerController.delegate = self;
        [self.playerController.player play];
        
        self.playerController.view.bounds = CGRectMake(0, 0, self.view.bounds.size.width, 300);
        self.playerController.view.center = CGPointMake(CGRectGetMidX(self.view.bounds), 64 + CGRectGetMidY(self.playerController.view.bounds) + 30);
        
        //在本VC播放
        [self addChildViewController:self.playerController];
        [self.view addSubview:self.playerController.view];
    

    相关文章

      网友评论

          本文标题:AVPlayer和AVPlayerViewController

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