iphone - 使用UIWebview播放嵌入式YouTube视频

标签 iphone uiwebview youtube

将Youtube视频嵌入UIWebview中。当按下UIButton时,如何使用UIWebview播放此视频。

- (void)viewDidLoad
 {
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;

UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

NSString *videoUrl = @"http://http://www.youtube.com/v/oHg5SJYRHA0";
NSString *htmlString = [NSString stringWithFormat:@"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head><body style=\"background:#F00;margin-top:0px;margin-left:0px\"><div><object width=\"320\" height=\"480\"><param name=\"movie\" value=\"%@\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%@\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"480\"></embed></object></div></body></html>",videoUrl,videoUrl]    ;

[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://www.youtube.com"]];

//[window addSubview:webView];
[webView release]; 

[super viewDidLoad];
// Do any additional setup after loading the view.
} 

当用户按下此UIButton时,它应开始播放嵌入式youtube视频
UIButton *videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [videoButton addTarget:self action:@selector(VideoAction:) forControlEvents:UIControlEventTouchUpInside];
videoButton.frame = CGRectMake(0, 0, 40, 40);
UIImage *icn = [UIImage imageNamed:@"Video_icon.png"];
[videoButton setImage:icn forState:UIControlStateNormal];
UIBarButtonItem *video = [[UIBarButtonItem alloc] initWithCustomView:videoButton];

我如何播放YouTube视频。

感谢帮助。

最佳答案

你为什么不试试这个:

            [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v=oHg5SJYRHA0"]]]
            [self.view addSubview:webView];

关于iphone - 使用UIWebview播放嵌入式YouTube视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11922579/

相关文章:

ios - 如何有选择地向 UIWebView 添加样式表?

javascript - 使用 youtube API 中的按钮播放和暂停 iframe 视频

iPhone "_OBJC_CLASS_"编译错误

caching - 如何清除 UIWebView 的 loadHtmlString 缓存?

iphone - 访问核心数据实体的 NSString 属性

ios - 在 iOS 的 UIWeb View 中禁用垂直滚动反弹?

youtube - 如何通过 api 获取 youtube 推荐视频?

python - 为什么 youtube 不给我视频链接而只给我他们的资源页面?

iphone - 随着 UISlider 值的更改,单元格未更新值

ios - 如果他们仍然拒绝,请偶尔请求 iOS 权限