I have two UIViewControllers , each of which has AVPlayer , which should play the video file when they are placed in the 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] }
When you first click on any of the UIViewControllers playback works well. But after that, if I click on them again, the sound plays, but the video freezes.
I tried using MPMoviePlayerViewController , but the behavior is the same. Any thoughts?
source share