gpt4 book ai didi

iphone - 使用 webview 在 iphone 应用程序中播放来自 youtube 的视频

转载 作者:可可西里 更新时间:2023-11-01 04:14:45 25 4
gpt4 key购买 nike

我正在尝试使用以下代码在我的 iPhone 应用程序中观看视频:

-(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];
}

谁能告诉我我做错了什么?我已经在几个示例中看到了这一点,但我仍然无法解决!

最佳答案

看起来您的 HTML 字符串格式不正确。

试试这个:

- (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];

}

值得注意的是,这段代码只能在设备上运行,不能在模拟器上运行。

更新...

由于 YouTube 所做的更改,上述方法现在不正确。请改用以下方法。

- (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];

}

关于iphone - 使用 webview 在 iphone 应用程序中播放来自 youtube 的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8079899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com