Search code examples
iosswiftswift3avplayeravplayerviewcontroller

Place AVPlayerViewController inside UIView


Currently I am having a dialogue view with four controls at the bottom. Each control is loading a different view inside the dialogue. One of these controls is setting an AVPlayer inside the dialogue view and is playing it. Unfortunately the AVPlayer itself comes without playback controls.

The AVPlayerViewController how ever does have playback controls. Is it possible to place a AVPlayerViewController inside a UIView so that it does not get started as a new screen? I would like to place it inside my UIView so that everything is taking place inside my dialgoue.


Solution

  • You can initialize the AVPlayerViewController and add it as a child to your ViewController , and insert it as a subview.

    [self addChildViewController:yourPlayerViewController];
    [self.view addSubview:yourPlayerViewController.view];
    [yourPlayerViewController didMoveToParentViewController:self];
    

    And to remove it:

    [yourPlayerViewController willMoveToParentViewController:nil];
    [yourPlayerViewController.view removeFromSuperview];
    [yourPlayerViewController removeFromParentViewController];
    

    EDIT SWIFT METHOD: Check here for Swift