使用webview在iphone应用程序中播放来自youtube的视频 [英] using a webview to play videos from youtube in iphone app
本文介绍了使用webview在iphone应用程序中播放来自youtube的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码在我的iphone应用中观看视频:
I am trying to watch videos in my iphone app using the following code:
-(void) viewDidLoad{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.frame];
NSString *youTubeVideoHTML = @"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
// Populate HTML with the URL and requested frame size
NSString *html = [NSString stringWithFormat:youTubeVideoHTML,@"http://www.youtube.com/watch?v=ZiIcqZoQQwg" , 100, 100];
// Load the html into the webview
[webView loadHTMLString:html baseURL:nil];
[self.view addSubview:webView];
}
谁能告诉我我做错了什么?我在几个例子中看到了这一点,但我仍然无法解决这个问题!
Can anyone tell me what I am doing wrong? I've seen this in a couple examples but still I can't work it out!
推荐答案
它看起来像你的HTML字符串没有正确形成。
It looks like your HTML string isn't properly formed.
试试这个:
- (void)viewDidLoad {
[super viewDidLoad];
float width = 200.0f;
float height = 200.0f;
NSString *youTubeURL = @"http://www.youtube.com/watch?v=ZiIcqZoQQwg";
UIWebView *webView = [UIWebView new];
webView.frame = CGRectMake(60, 60, width, height);
NSMutableString *html = [NSMutableString string];
[html appendString:@"<html><head>"];
[html appendString:@"<style type=\"text/css\">"];
[html appendString:@"body {"];
[html appendString:@"background-color: transparent;"];
[html appendString:@"color: white;"];
[html appendString:@"}"];
[html appendString:@"</style>"];
[html appendString:@"</head><body style=\"margin:0\">"];
[html appendFormat:@"<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\"", youTubeURL];
[html appendFormat:@"width=\"%0.0f\" height=\"%0.0f\"></embed>", width, height];
[html appendString:@"</body></html>"];
[webView loadHTMLString:html baseURL:nil];
[self.view addSubview:webView];
}
值得注意的是,此代码仅适用于设备,它不适用于模拟器。
It's worth noting that this code will only work on a device, it won't work in the simulator.
更新...
由于YouTube所做的更改,上述方法现在不正确。请改用以下方法。
Due to changes made by YouTube, the method above is now incorrect. Use the method below instead.
- (void)viewDidLoad {
[super viewDidLoad];
float width = 200.0f;
float height = 200.0f;
NSString *youTubeToken = @"K95Q0VFyhA8";
UIWebView *wv = [UIWebView new];
webView.frame = CGRectMake(60, 60, width, height);
NSMutableString *html = [NSMutableString string];
[html appendString:@"<html>"];
[html appendString:@"<head>"];
[html appendString:@"<style type=\"text/css\">"];
[html appendString:@"body {"];
[html appendString:@"background-color: transparent;"];
[html appendString:@"color: white;"];
[html appendString:@"margin: 0;"];
[html appendString:@"}"];
[html appendString:@"</style>"];
[html appendString:@"</head>"];
[html appendString:@"<body>"];
[html appendFormat:@"<iframe id=\"ytplayer\" type=\"text/html\" width=\"%0.0f\" height=\"%0.0f\" src=\"http://www.youtube.com/embed/%@\" frameborder=\"0\"/>", width, height, videoToken];
[html appendString:@"</body>"];
[html appendString:@"</html>"];
[wv loadHTMLString:html baseURL:nil];
[self.view addSubview:wv];
}
这篇关于使用webview在iphone应用程序中播放来自youtube的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文