嘿所以我已經做了大量的研究,使用UIWebView作爲YouTube視頻的視頻播放器。目前我有一個工作解決方案,它使用故事板中的UIWebView設置來播放YouTube視頻。 UIWebView將視頻作爲預覽顯示,當點擊視頻在標準IOS視頻播放器中打開時,視頻完成後用戶單擊完成,視頻關閉,控件返回到應用程序。 Web視圖通過一個IBOutlet連接,並設置如下圖所示:IOS關於故事板中的UIWebView的問題
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
我有一個關於的設置兩個問題網絡視圖和處理視頻播放器的行爲。
1)出於某種原因,即使UIWebView是在故事板視圖控制器中創建的,但Web視圖不會以框架啓動。 viewDidLoad
和coorperateVideo
方法中的NSLog都爲返回值和高度返回0.0。爲什麼是這樣?我的印象是,在故事板中創建的大多數視圖都是用大小檢查器中指定的框架啓動的。在使用它之前是否需要設置Web視圖的框架?
2)我的第二個問題與按下視頻時調用的視頻播放器有關。目前該應用程序已設置,以便唯一支持的界面方向是縱向。然而,當我點擊視頻並且播放器顯示時,用戶受限於在橫向上觀看視頻。有沒有辦法設置它,以便只有視頻播放器視圖可以支持其他方向?
感謝您的幫助!