嵌入YouTube视频

ayv*_*yve 28 youtube iphone objective-c ios

我通过互联网上找到的代码片段从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
Run Code Online (Sandbox Code Playgroud)

它正确编译,当我打开它时,我看到一个错误的白色框,代码创建.我有两个问题:

  1. 我怎么知道视频是否会播放,它只是一个纯白色的盒子,模拟器是否播放来自YouTube的视频?

  2. 我该如何定位这个盒子?

Jul*_*rgé 13

只是测试了你的代码,它在iPhone上工作正常,但iOS模拟器上不支持Youtube视频,所以你需要一个真正的设备进行测试.

我该如何定位这个盒子?

您已经在此行传递框的X(20),Y(20),宽度(100)和高度(100):

[self embedYouTube:@"http://..." frame:CGRectMake(20, 20, 100, 100)];
Run Code Online (Sandbox Code Playgroud)

要在以后更改视图的位置,请修改其中心属性:

videoView.center = CGPointMake(200, 100 );
Run Code Online (Sandbox Code Playgroud)


小智 7

下面是示例代码,它在iOS 5及更高版本上正常工作,这是Youtube API提供的新实现,myWeb这里是一个UIWebView. showinfo=0在URL中将删除videoView中的顶部标题.

NSString *html = [NSString stringWithFormat:@"<html><body><iframe class=\"youtube-player\" type=\"text/html\" width=\"%f\" height=\"%f\" src=\"http://www.youtube.com/embed/q1091sWVCMI?HD=1;rel=0;showinfo=0\" allowfullscreen frameborder=\"0\" rel=nofollow></iframe></body></html>",frame.size.width,frame.size.height];
[myWeb loadHTMLString:html baseURL:nil];
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助你:)


adr*_*ndz 5

这是一个适用于iOS 6和新的YouTube嵌入代码的版本:

- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
    NSString *html = [NSString stringWithFormat:@"<html><head><style type='text/css'>body {background-color: transparent;color: white;}</style></head><body style='margin:0'><iframe width='%f' height='%f' src='%@' frameborder='0' allowfullscreen></iframe></body></html>", frame.size.width, frame.size.height, urlString];
    UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
    [videoView loadHTMLString:html baseURL:nil];
    [self.view addSubview:videoView];
}
Run Code Online (Sandbox Code Playgroud)