AVPlayer

I'm refitting old moviePlayer code to the AVPlayer. I had a part when a user selected a movie from a list of choices the movie player would appear showing the clip along with a "done" button that would take the person back to the tableview.


I've gotten the movie to play but I am having trouble figuring out how to have a "back/done" buttont to dismiss the Player. Here is what I have so far:


-(void)playMovie: (NSString *) fileName

{

NSLog(@"in playMovie second");

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: fileName ofType:@"mp4"]];

AVPlayer *player = [AVPlayer playerWithURL:url];

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

//AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

avMovie.view.frame = self.view.bounds;

avMovie.player = player;

//playerLayer.frame = self.view.bounds;

//[playerLayer setVideoGravity:AVLayerVideoGravityResizeAspect];

//[self.view.layer addSublayer:playerLayer];

[player play];

//show view controller

[self addChildViewController:avMovie];

[self.view addSubview:avMovie.view];

}

Accepted Reply

When do you remove the view ? On which action ?


I suppose you should add a done button, may be a pause as well.

Replies

Get the answer here:

https://stackoverflow.com/questions/32993896/impossible-to-stop-avplayer


if player declared as AVPlayer, call

player.replaceCurrentItem(with: nil)

if declared optional AVPlayer?, you can set directly to nil, after pausing

[player pause]

player = nil

I used


[avMovie.view removeFromSuperview];


to remove the view. However, the player does not have a "Back/done" button. Do I have to manually code one in or is there a title or subtitle feature that allows me to enter this functionality?

When do you remove the view ? On which action ?


I suppose you should add a done button, may be a pause as well.

I do appreciate you getting back to me.

Claude, it seems I am having trouble adding the button to my avplayer. The button never shows when I use avMovie.view or self.view when I try to add the subView. Any suggestions?


-(void)playMovie: (NSString *) fileName

{

NSLog(@"in playMovie second");

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: fileName ofType:@"mp4"]];

AVPlayer *player = [AVPlayer playerWithURL:url];

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

//AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

avMovie.view.frame = self.view.bounds;

avMovie.player = player;

//playerLayer.frame = self.view.bounds;

//[playerLayer setVideoGravity:AVLayerVideoGravityResizeAspect];

//[self.view.layer addSublayer:playerLayer];

[player play];

//make button

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(30,30,100,60);

[button setTitle:@"Done" forState:UIControlStateNormal];

[button addTarget:self action:@selector(myDoneButtonMethod:) forControlEvents:UIControlEventTouchUpInside];

[avMovie.view addSubview:button];


//show view controller

[self addChildViewController:avMovie];

[self.view addSubview:avMovie.view];


//avMovie.view.frame = self.view.frame;

//[player replaceCurrentItemWithPlayerItem:nil];

//[player pause];

//[avMovie.view removeFromSuperview];

}