嵌入YouTube視頻,我通過一個片段,我在網上找到的嵌入來自YouTube的視頻,在這裏是我使用的代碼:上
@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
它正確編譯,當我打開它,我看到一個不正確的白色框,代碼創建。我有一個關於這兩個問題:
我怎麼知道,如果視頻會玩,這只是一個純白色的盒子,從YouTube確實模擬器播放視頻嗎?
我該如何擺放這個盒子?
代碼真棒位,乾杯! – Shawson 2012-06-10 12:00:06
此代碼是否適用於iOS 6? – Dee 2013-03-07 11:18:45