在应用程序中播放YouTube视频

时间:2011-10-24 06:43:53

标签: ios4

想直接在横向模式下在我的应用中播放YouTube视频。我正在编写这样的代码,但没有运气:

- (void)viewDidLoad {
[super viewDidLoad];
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
data = [[HBAppDelegate getGlobalInfo] valueForKey:@"tmp_page_data"];
NSString *url = [[NSString alloc] init];
url = [data valueForKey:@"trailer_url"];
videoView = [[UIWebView alloc] init];
[self embedYouTube:url frame:CGRectMake(0, 0,320, 240)];
}
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame { 

NSString *htmlString =@"<html><head>"
"<meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head>"
"<body style=\"background:#FFFFF;margin-top:20px;margin-left:0px\">"
"<div><object width=\"320\" height=\"240\">"
"<param name=\"wmode\" value=\"transparent\"></param>"
"<embed src=\"%@\""
"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"240\"></embed>"
"</object></div></body></html>";

NSString *html = [NSString stringWithFormat:htmlString, urlString, frame.size.width, frame.size.height];
[videoView setFrame:frame];
videoView.delegate=self;
[videoView loadHTMLString:html baseURL:nil];    
}
- (void)webViewDidFinishLoad:(UIWebView *)_webView {
    UIButton *b = [self findButtonInView:_webView];
    [b sendActionsForControlEvents:UIControlEventTouchUpInside];
}

- (UIButton *)findButtonInView:(UIView *)view {
    UIButton *button = nil;

    if ([view isMemberOfClass:[UIButton class]]) {
        return (UIButton *)view;
    }

    if (view.subviews && [view.subviews count] > 0) {
        for (UIView *subview in view.subviews) {
            button = [self findButtonInView:subview];
            if (button) return button;
        }
    }
    return button;
}

1 个答案:

答案 0 :(得分:3)

NSString *embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color:white;\
color: Black;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];