Search code examples
iosobjective-cuiviewavplayer

AVPlayer view in UIView is missing controls


In AVPlayer default video forward slider and play button are not showing if AVPlayer is added to UIView

NSURL *videourl= [NSURL URLWithString:strUrl];
AVPlayer *player = [AVPlayer playerWithURL:videourl];         

AVPlayerViewController *playerViewController = [AVPlayerViewController new];            

playerViewController.player = player;

playerViewController.view.frame = ViewAV.bounds;

AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = ViewAV.bounds;

playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
//externalPlaybackVideoGravity
playerLayer.contentsGravity = AVLayerVideoGravityResizeAspect;

[ViewAV.layer addSublayer:playerLayer];
[ViewAV addSubview:playerViewController.view];

[playerViewController.player play];

Solution

  • Try setting showsPlaybackControls and requiresLinearPlayback to true,

    playerViewController.showsPlaybackControls = YES;
    playerViewController.requiresLinearPlayback = YES;
    

    Currently, you didn't add playerViewController as child viewController so you will not be able to see anything. Add as below,

    [self addChildViewController:playerViewController];
    [self.view.layer addSublayer:playerLayer];
    [self.view addSubview:playerViewController.view];