3

I am trying to use a video on a view controller but not have it full screen, just enough room for a label on top and a button on bottom, is this possible? If not how can I have it so if the user skips the video or it finishes it goes to a certain viewController?

- (void)viewDidLoad {
    [super viewDidLoad];

NSURL *videoURL = [[NSBundle mainBundle]URLForResource:@"TTPlaneHits" withExtension:@"mp4"];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];


AVPlayer *player = [AVPlayer playerWithURL:videoURL];
playerLayer.frame = CGRectMake(0, 21, self.view.frame.size.width, self.view.frame.size.height - 45 - 21);
[self.view.layer addSublayer:playerLayer];
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];


[self.view addSubview:controller.view];
controller.view.frame = self.view.frame;
BARIIIIIIICODE
  • 123
  • 1
  • 1
  • 8

3 Answers3

12

Swift version

func addVideoPlayer(videoUrl: URL, to view: UIView) {
    let player = AVPlayer(url: videoUrl)
    let layer: AVPlayerLayer = AVPlayerLayer(player: player)
    layer.backgroundColor = UIColor.white.cgColor
    layer.frame = view.bounds
    layer.videoGravity = .resizeAspectFill
    view.layer.sublayers?
        .filter { $0 is AVPlayerLayer }
        .forEach { $0.removeFromSuperlayer() }
    view.layer.addSublayer(layer)
}
danieltmbr
  • 1,022
  • 12
  • 20
9

From Apple's documentation:

You can display the visual content of items played by an instance of AVPlayer in a CoreAnimation layer of class AVPlayerLayer; to synchronize real-time playback with other CoreAnimation layers, you can use AVSynchronizedLayer. You cannot use an instance of AVVideoCompositionCoreAnimationTool with an AVPlayer object; for offline rendering you should instead use AVAssetExportSession.

Try this:

(playerView is just an IBOutlet of a UIView placed in storyboards)

 -(void)viewDidLoad {
    [super viewDidLoad];

    NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"]];

    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:videoURL];
    AVPlayer *player = [AVPlayer playerWithPlayerItem:item];

    CALayer *superlayer = self.playerView.layer;

    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    [playerLayer setFrame:self.playerView.bounds];
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [superlayer addSublayer:playerLayer];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playerDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:item];


    [player seekToTime:kCMTimeZero];
    [player play];

}

-(void)playerDidFinishPlaying:(NSNotification *)notification {

    [self performSegueWithIdentifier:@"YourIdentifier" sender:self];
}
Kaitis
  • 628
  • 9
  • 21
  • That worked well, video is kind of oversized though but will work, although if I navigate off that viewcontroller it continues to play in background which can be emptied by a audio. – BARIIIIIIICODE Jun 28 '16 at 19:31
  • 1
    Try changing the videoGravity property to `AVLayerVideoGravityResizeAspectFit `and you can stop the player in your `prepareForSegue:` method. You will have to also make the `AVPlayer` an instance variable – Kaitis Jun 29 '16 at 05:59
-1

It might be easiest if you just use AVPlayerLayer and set it's frame property accordingly (like you would for any CALayer). Here's an example:

AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

//labelHeight is the height of your top label
//buttonHeight is the height of your bottom button
playerLayer.frame = CGRectMake(0, labelHeight, self.view.frame.size.width, self.view.frame.size.height - labelHeight - buttonHeight);
[self.view.layer addSublayer:playerLayer];

As for navigating to a different viewController at the end of the video, you're going to want to register for a callback with NSNotification. @random has a nice explanation of how to do that here.

Community
  • 1
  • 1
bfich
  • 393
  • 2
  • 19