3

I'm using AVPlayerViewController and can't showing the Done button, and can't close the player also. maybe you can help me. This is my code in objective-c

UIView *view = self.view;

NSURL *fileURL = [NSURL URLWithString: _detailData[0]];

AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];

playerViewController.player = [AVPlayer playerWithURL:fileURL];

self.avPlayerViewcontroller = playerViewController;

[self resizePlayerToViewSize];
[self dismissViewControllerAnimated:YES completion:nil];
[view addSubview:playerViewController.view];

[playerViewController.player play];

view.autoresizesSubviews = TRUE;

Need help guys, thank you very much.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Jakfar Shodiq
  • 59
  • 2
  • 7

2 Answers2

4
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Done" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); // set your own position
[view addSubview:button]; // or you can add [playerViewController.view addSubview:button];

-(void)aMethod:(UIButton *)button {

 // Remove playerViewController.view
}
Venk
  • 5,949
  • 9
  • 41
  • 52
  • If a button will be added as a subview to playerViewController.view it will be always visible (even if property showsPlaybackControls is set to NO) – Roman Podymov Sep 07 '16 at 14:10
  • This worked perfectly in iOS 10.3.3 but not working in iOS 11 . Any idea ? – Deep Shah Oct 22 '17 at 06:55
  • @DeepShah you mean Done button is not displaying? – Venk Oct 23 '17 at 04:53
  • try using `bringSubviewToFront` or try adding done button on `playerViewController.view ` – Venk Oct 23 '17 at 04:55
  • @Venkat Done button is displayed just fine & worked perfectly in iOS 10.3.3 but same code is not working in iOS 11 . Button is displaying in iOS 11 but click event is not working . – Deep Shah Oct 23 '17 at 05:16
  • @Venkat here is my question : https://stackoverflow.com/questions/46871529/custom-button-not-working-in-avplayer-ios-11-but-its-perfectly-working-below-io – Deep Shah Oct 23 '17 at 05:34
1

thanks for your answer. I found it already :)

We can using this, and done button can shown automatically.

NSURL *fileURL = [NSURL URLWithString: _detailData[0]];

// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:fileURL];

// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];

// show the view controller
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = self.view.frame;
Jakfar Shodiq
  • 59
  • 2
  • 7