Hey so I have ben doing a lot of research into using a UIWebView as a video player for youtube videos. Currently I have a working solution that uses a UIWebView setup in storyboard to play a youtube video. The UIWebView displays the video as a preview, when clicked the video opens in the standard IOS video player and after the video has finished and the user clicks done the video closes and control is returned to the app. The web view is linked through an IBOutlet and is setup as shown below:
ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(nonatomic, weak) IBOutlet UIWebView *mainWebView;
- (void)embedYouTube:(NSString *)url coorperateVideo:(UIWebView *)videoWebView;
@end
ViewController.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize mainWebView = _mainWebView;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Video Frame Width: %f", _mainWebView.frame.size.width);
NSLog(@"Video Frame Height: %f", _mainWebView.frame.size.height);
[self embedYouTube:@"http://www.youtube.com/" coorperateVideo:_mainWebView];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)embedYouTube:(NSString *)url coorperateVideo:(UIWebView *)videoWebView
{
NSLog(@"Video Frame Width: %f", videoWebView.frame.size.width);
NSLog(@"Video Frame Height: %f", videoWebView.frame.size.height);
NSInteger width = 280;
NSInteger height = 170;
videoWebView.allowsInlineMediaPlayback = YES;
videoWebView.mediaPlaybackRequiresUserAction = NO;
videoWebView.mediaPlaybackAllowsAirPlay = YES;
videoWebView.delegate = self;
videoWebView.scrollView.bounces = NO;
[videoWebView.scrollView setScrollEnabled:NO];
NSString *linkObj = @"http://www.youtube.com/v/1iBIcJFRLBA"; //@"http://www.youtube.com/v/6MaSTM769Gk";
NSString *embedHTML = [NSString stringWithFormat:@"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;color: white;}\\</style>\\</head><body style=\"margin:0\">\\<embed webkit-playsinline id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \\width=\"%d\" height=\"%d\"></embed>\\</body></html>", @"%@", width, height];
NSString *html = [NSString stringWithFormat:embedHTML, linkObj];
[videoWebView loadHTMLString:html baseURL:nil];
}
@end
I have two questions regarding the setup of the web view and dealing with the behaviour of the video player.
1) For some reason even though the UIWebView is created in a storyboard view controller the web view doesn't get initiated with a frame. Both the NSLogs in the viewDidLoad
and coorperateVideo
methods return 0.0 for the withs and height. Why is this? I was under the impression that most views created in storyboard are initiated with the frame specified in the size inspector. Do I need to set the frame of the web view before using it?
2) My second question has to do with the video player that is called when the video is pressed. Currently the app is set so that the only supported interface orientation is portrait. However, when I click the video and the player shows, the user is limited from viewing the video in landscape. Is there a way to set it so that only the video player view can support other orientations?
Thanks for the help!