0

I've looked and found some similar questions asked before. But none of the offered solutions work for me so I'm asking this.

I'm writing an IOS App that starts with a Tab bar, each tab View Controller has an embedded Navigation Controller. I am able to stream video and play it using AVPlayerViewController but am not able to force it to start in Landscape mode (even if the phone is in portrait mode. If I flip the phone, it does become full screen as Landscape mode.

Here is the code section that I have that streams the Video:

-(void) startPlayback {

  NSURL *streamURL = [NSURL URLWithString: pathToVideoStream];

  NSLog(@"Entered %s - Will now try to play video at NSURL: %@",__PRETTY_FUNCTION__, streamURL );

  AVPlayerViewController *moviePlayer = [[AVPlayerViewController alloc] init];
  moviePlayer.player = [AVPlayer playerWithURL:streamURL];
  [self presentViewController:moviePlayer animated:YES completion:nil];
  moviePlayer.view.frame = self.view.frame;
  [moviePlayer.player play];

}

I have added the suggestions from this post: Play Video In Landscape when entire application is Portait

But that did not force the video to play in Landscape mode. I see other suggestions about using CGAffine, but my first instinct says that can't be the correct way to flip the Video. Please advice.

Community
  • 1
  • 1
Curious101
  • 1,538
  • 2
  • 18
  • 36

1 Answers1

0

I found the solution to this.

I created a subclass of AVPlayerViewController and called LandscapeAVPlayerViewController. In the implementation of the subclass I overrode two methods:

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{

   [super supportedInterfaceOrientations];
   return UIInterfaceOrientationMaskLandscape; // allows Left and Right both
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeRight;   // prefers Right in portrait mode
}

And then back in the startPlayback Method I changed the code to :

-(void) startPlayback {

   NSURL *streamURL = [NSURL URLWithString: pathToVideoStream];

   LandscapeAVPlayerViewController *landscapeMoviePlayerVC =  [[LandscapeAVPlayerViewController alloc] init];

   landscapeMoviePlayerVC.player = [AVPlayer playerWithURL:streamURL];

   [self presentViewController:landscapeMoviePlayerVC animated:YES completion:nil];
   [landscapeMoviePlayerVC.player play];

}

That's all I needed to do.

Curious101
  • 1,538
  • 2
  • 18
  • 36