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

Wain
  • 118,658
  • 15
  • 128
  • 151
Henning
  • 253
  • 1
  • 2
  • 11

2 Answers2

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.....

klefevre
  • 8,595
  • 7
  • 42
  • 71
Chirag Desai
  • 827
  • 8
  • 13
  • 1
    Thanks! Now I can hear the video;) (I dont see it) Got this message: Warning: Attempt to present on whose view is not in the window hierarchy! How can I attach the controller to a UIView? – Henning Mar 01 '16 at 13:12
  • are you write this code in "viewDidLoad" ?? because of this you got this message just move this code in "viewDidAppear " may this work for you – Chirag Desai Mar 01 '16 at 13:26
  • 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 playing – Henning Mar 01 '16 at 13:43
  • plz add your code... – Chirag Desai Mar 02 '16 at 05:23
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?

klefevre
  • 8,595
  • 7
  • 42
  • 71
Henning
  • 253
  • 1
  • 2
  • 11
  • ref: http://stackoverflow.com/questions/20555936/can-we-access-programatically-ios-system-settings-for-general-accessibility-subt – Henning Mar 07 '16 at 09:07