嵌入YouTube视频

时间:2011-02-25 13:10:23

标签: iphone objective-c ios 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

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

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

  2. 如何定位此框?

6 个答案:

答案 0 :(得分:13)

刚刚测试过您的代码,它在iPhone上工作正常,但iOS模拟器上不支持Youtube视频,因此您需要一个真实的设备进行测试。

  

我如何定位此框?

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

[self embedYouTube:@"http://..." frame:CGRectMake(20, 20, 100, 100)];

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

videoView.center = CGPointMake(200, 100 );

答案 1 :(得分:7)

以下是示例代码,它在iOS 5及更高版本中运行良好,这是Youtube API提供的新实现,myWeb是UIWebView。 URL中的showinfo=0将删除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];

希望这可以帮助你:)

答案 2 :(得分: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];
}

答案 3 :(得分:0)

对于使用Monotouch和C#的用户,您可以将此类添加到您的项目中并使用它来查看您的视频:

public class YouTubeViewer : UIWebView
{
    public YouTubeViewer(string url, RectangleF frame)
    {
        string youTubeVideoHTML = @"<object width=""{1}"" height=""{2}""><param name=""movie""
                            value=""{0}""></param><embed
                            src=""{0}"" type=""application/x-shockwave-flash"" 
                            width=""{1}"" height=""{2}""</embed></object>"; 

        string html = string.Format(youTubeVideoHTML, url, frame.Size.Width, frame.Size.Height);
        this.LoadHtmlString(html, null);
        this.Frame = frame;
    }
}

确保您传递的网址是您点击“分享”按钮时Youtube提供的嵌入代码中的网址。 (并选中旧代码的复选框)

答案 4 :(得分:0)

快速实施:

Role#1
Role#2
Role#3

或者,YouTube为开发者提供了一个很棒的helper class,可以在他们的iOS中设置YouTube播放,为您设置let webView = UIWebView(frame: self.view.frame) // Position your player frame here self.view.addSubview(webView) self.view.bringSubviewToFront(webView) // Playback options: play in-line and start playing immediately webView.allowsInlineMediaPlayback = true webView.mediaPlaybackRequiresUserAction = false // Set the id of the video you want to play let videoID = "zN-GGeNPQEg" // https://www.youtube.com/watch?v=zN-GGeNPQEg // Set up your player let embededHTML = "<html><body style='margin:0px;padding:0px;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}function onPlayerReady(a){a.target.playVideo();}</script><iframe id='playerId' type='text/html' width='\(self.view.frame.size.width)' height='\(self.view.frame.size.height)' src='http://www.youtube.com/embed/\(videoID)?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'></body></html>" // Load the HTML into your UIWebView webView.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().bundleURL)

答案 5 :(得分:0)

swift 4

func embedYouTube(_ urlString: String, frame: CGRect) {

    let html = "<html><head><style type='text/css'>body {background-color: transparent;color: white;}</style></head><body style='margin:0'><iframe width='\(frame.size.width)' height='\(frame.size.height)' src='\(urlString)' frameborder='0' allowfullscreen></iframe></body></html>"
    let videoView = UIWebView(frame: frame)
    videoView.loadHTMLString(html, baseURL: nil)
    view.addSubview(videoView)
}