Xcode - Objective C - AVPlayer programmatically - play local video, with standard/system skin [closed]
Asked Answered
B

2

5

Can anyone help me with code for setting up and playing local videofile, using AVPlayer in Xcode? (using AVPlayerLayer, and AVPlayerViewController) All done programmatically and with standard/system videoskin?

Regards Henning

Bidet answered 1/3, 2016 at 12:5 Comment(0)
W
19
AVPlayer *player = [AVPlayer playerWithURL:"YOUR URL"];

// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self presentViewController:controller animated:YES completion:nil];
controller.player = player;
[player play];

hope this help for you.....

Walcott answered 1/3, 2016 at 12:49 Comment(4)
Thanks! Now I can hear the video;) (I dont see it) Got this message: Warning: Attempt to present <AVPlayerViewController: 0x137610df0> on <ViewController: 0x137509580> whose view is not in the window hierarchy! How can I attach the controller to a UIView?Bidet
are you write this code in "viewDidLoad" ?? because of this you got this message just move this code in "viewDidAppear " may this work for youWalcott
Sorry, I am quite new to Objective C.. You are right, if i move the code to viewDidAppear the error goes away, but then no video is playingBidet
plz add your code...Walcott
B
12
// remote file from server:
NSURL *url = [[NSURL alloc] initWithString:@"https://s3-eu-west-1.amazonaws.com/alf-proeysen/Bakvendtland-MASTER.mp4"];

// create a player view controller
player = [AVPlayer playerWithURL:url];
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];

[self addChildViewController:controller];
[self.view addSubview:controller.view];

controller.view.frame = CGRectMake(50,50,500,300);
controller.player = player;
controller.showsPlaybackControls = YES;
player.closedCaptionDisplayEnabled = NO;
[player pause];
[player play];

This is all done in viewDidLoad and its working fine now. I made a subview to present the player.

The only thing that is not working now is player.closedCaptionDisplayEnabled = NO; this is ignored. The video has soft subtitles embedded, and I am able to toggle the subtitles with cc button that is showing. But I am not able to toggle it/control it programmatically?

Bidet answered 7/3, 2016 at 8:55 Comment(1)
ref: #20556436Bidet

© 2022 - 2024 — McMap. All rights reserved.