I have two
UIViewControllers
each with an AVPlayer
that are supposed to play a video file when they are pushed into a UINavigationController
: -(void)viewWillAppear:(BOOL)animated{
videoView = [[UIView alloc]initWithFrame:self.view.frame];
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"myVideo" ofType:@"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
AVPlayerLayer *foregroundLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
foregroundLayer.frame = self.view.frame;
foregroundLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[videoView.layer addSublayer:foregroundLayer];
[self.view addSubview:videoView];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avPlayer currentItem]];
}
-(void)viewDidAppear:(BOOL)animated{
[self.avPlayer play];
}
-(void)playerItemDidReachEnd:(NSNotification *)notification {
[self.navigationController popViewControllerAnimated:NO]
}
The first time I push any of the
UIViewControllers
the playback works well. But after that, if I push any of them again the sound plays but the video freezes.I've tried using a
MPMoviePlayerViewController
but the behavior is the same. Any thoughts?