Embed youtube video in iOS App

a 夏天 提交于 2019-11-30 15:40:15

Try this one this is working perfect. You use youtube Url as "http://www.youtube.com/v/YOU_TUBE_VIDEO_ID".

 UIWebView * youTubeWebView=[[UIWebView alloc]initWithFrame:CGRectMake(0,0,320,320)];
    youTubeWebView.allowsInlineMediaPlayback=YES;
    youTubeWebView.mediaPlaybackRequiresUserAction=NO;
    youTubeWebView.mediaPlaybackAllowsAirPlay=YES;
    youTubeWebView.delegate=self;
    youTubeWebView.scrollView.bounces=NO;

    NSString *linkObj=@"http://www.youtube.com/v/1iBIcJFRLBA";//@"http://www.youtube.com/v/6MaSTM769Gk";
    NSLog(@"linkObj1_________________%@",linkObj);
    NSString *embedHTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;color: white;}\\</style>\\</head><body style=\"margin:0\">\\<embed webkit-playsinline id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \\width=\"320\" height=\"320\"></embed>\\</body></html>";

    NSString *html = [NSString stringWithFormat:embedHTML, linkObj];
    [youTubeWebView loadHTMLString:html baseURL:nil];
    [self.view addSubview:youTubeWebView];

you can add this code if you want to embed you tube video in swift

let webViewYouTube = UIWebView()

    webViewYouTube.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.width/2)

    webViewYouTube.allowsInlineMediaPlayback = true
    webViewYouTube.mediaPlaybackRequiresUserAction = false
    webViewYouTube.mediaPlaybackAllowsAirPlay = true
    webViewYouTube.delegate = self as? UIWebViewDelegate
    webViewYouTube.scrollView.bounces = false
    webViewYouTube.scrollView.isScrollEnabled = false
    let linkObj = "https://www.youtube.com/watch?v=swIoyaBUpEg"

    print("linkObj1_________________\(linkObj)")
    let embedHTML = "    <html><head>    <style type=\"text/css\">    body {    background-color: transparent;color: white;}\\</style>\\</head><body style=\"margin:0\">\\<embed webkit-playsinline id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \\width=\"320\" height=\"320\"></embed>\\</body></html>"
    let html = String(format: embedHTML, linkObj)
    webViewYouTube.loadHTMLString(html, baseURL: nil)
    view.addSubview(webViewYouTube)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!