AVPlayer和AVPlayerViewController

2018-06-07  本文已影响18人  William_

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];
上一篇下一篇

猜你喜欢

热点阅读