嵌入YouTube视频 [英] Embedding YouTube videos on
问题描述
我通过互联网上找到的代码片段从YouTube中嵌入了一段视频,以下是我使用过的代码:
I've embedded a video from YouTube via a snippet I've found on the Internet, here is the code that I've used:
@interface FirstViewController (Private)
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self embedYouTube:@"http://www.youtube.com/watch?v=l3Iwh5hqbyE" frame:CGRectMake(20, 20, 100, 100)];
}
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
NSString *embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</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];
UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];
[videoView release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
它编译正确,当我打开它,我看到一个错误的白框,代码创建。我有两个问题:
It compiles correctly and when I open it, I see a white box positioned incorrectly, which the code created. I have two questions regarding it:
-
我如何知道视频是否会播放,它只是一个纯白色的盒子,模拟器是否播放YouTube上的视频?
How do I know if the video will play, it is just a plain white box, does the simulator play videos from YouTube?
如何定位此框?
推荐答案
刚刚测试过您的代码,它在iPhone上工作正常,但iOS模拟器上不支持Youtube视频,所以你需要一个真实的用于测试的设备。
Just tested your code, it works fine on an iPhone, Youtube videos are not supported on the iOS simulator though, so you'll need a real device for testing.
如何定位此框?
How can I position this box?
您已在此行传递框的X(20),Y(20),宽度(100)和高度(100):
You are already passing the X (20), Y(20), width(100) and height(100) of the box at this line:
[self embedYouTube:@"http://..." frame:CGRectMake(20, 20, 100, 100)];
要更改视图的位置,请修改其中心属性:
To change the position of the view afterwards, you modify its center property:
videoView.center = CGPointMake(200, 100 );
这篇关于嵌入YouTube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!