1

edit: The videos are of the app being used: i.e. they're instructional videos designed to be played in portrait mode.

I used this method (NB iPhoneDevSDK.com is currently marked by Google as containing malware, so be careful. I've uploaded a screengrab of the relevant post) to open & play a YouTube video (using a UIWebView behind the scenes).

The video I want to show was recorded in portrait (uploaded in 320x480) and was intended to fill the iPhone screen nicely when in portrait mode. However, the YouTube video starts in a forced landscape mode, and can only be put into portrait by moving the phone to a landscape position and then back again (sometimes needs to be done a few times).

What I'm wondering is: is there a way of forcing the UIWebView to open a video player that defaults to portrait mode?

NB I've set (interfaceOrientation == UIInterfaceOrientationPortrait) on the UIWebView delegate that's spawning the UIWebView.

Edit: I even tried subclassing UIWebView on the off-chance that this would get it to work:

@interface AltWebView : UIWebView
@end

@implementation AltWebView

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
  • First image: video playing in Landscape by default
  • Second image: Portrait mode once rotated a few times

In Landscape by default In Portrait mode once rotated a few times

Ian Dundas
  • 553
  • 1
  • 6
  • 17

2 Answers2

4

I've been facing same problem recently and used this code. This may not be the right answer to your question but it plays YouTube video in portrait mode.

NSString *embedHTML = @"<iframe title=\"YouTube video player\" width=\"320\" height=\"460\" scrolling=\"no\" src=\"http://www.youtube.com/embed/%@?modestbranding=1&amp;rel=0;autoplay=1;showinfo=0;loop=1;autohide=1\" frameborder=\"0\" allowfullscreen allowTransparency=\"true\"></iframe>"; 

NSString *htmlStr = [NSString stringWithFormat:embedHTML, videoID];  // videoID is like EgXdUs9HsrQ...  

[webView loadHTMLString:htmlStr baseURL:nil]; 
torutoru
  • 131
  • 1
  • 5
1

For whichever view controller that owns the UIWebView, you could specify that you only want to display everything from that view controller in portrait mode.

Something like, say:

- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}

and

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

B.T.W., there's a helpful question with lots of helpful hints over at: iPhone SDK: Orientation (Landscape and Portrait views)

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Thanks for the suggestion: I've tried both of these and it's still starting the video in Landscape mode though unfortunately. – Ian Dundas Nov 15 '11 at 15:17
  • I even tried subclassing UIWebView, just in case with the following: *edit: balls I can't type code in here, see main post* – Ian Dundas Nov 15 '11 at 15:22
  • Those two methods are for View Controllers, not for Views... I bet they never even get called. :-) What you need to do is create a new view controller (with those two methods), put your UIWebView in it's xib file and then push / present the view controller when you have the movie to show. Makes sense? – Michael Dautermann Nov 15 '11 at 15:29
  • Thanks for your suggestion. `[self.view insertSubview:youTubeWebView atIndex:0];` is currently what I'm using (where youTubeWebview is of type UIWebKit), and `self` (UIViewController) contains the two code fragments you gave above, but still no luck unfortunately. Good point r.e. UIWebKit being a UIView instead of a UIViewController though, doh! – Ian Dundas Nov 15 '11 at 19:05
  • p.s. I'm not sure of etiquette around here, but as I'm used to Reddit please have an upvote for your time. – Ian Dundas Nov 15 '11 at 19:10
  • I'll probably take another stab at solving your question (unless somebody else gets to it), but first I have to get some real work done. ;-) – Michael Dautermann Nov 15 '11 at 19:12
  • Thanks man. I've tried some other things (CGAffineTransformMakeRotation transformation on UIWebView's view) but it looks like the YouTube player that slides up might be inaccessible to my mortal skills. An alternative would be to host the videos myself and use the MPMoviePlayerController which I think might be more easily manipulated (and there was a video quality issue on YouTube that would be circumvented, too) – Ian Dundas Nov 15 '11 at 23:14
  • Did you ever figure out how to access the player ViewController? – atreat Feb 21 '13 at 14:57