5

I want to embed a youtube video into a webviewer whenever I press a button. I have this code

- (IBAction)testBtn:(id)sender {

    NSString *code = @"<iframe width=\"560\" height=\"315\" src=\"//www.youtube.com/embed/1iBIcJFRLBA\" frameborder=\"0\" allowfullscreen></iframe>";

    [[self youtubeWebPlayer]loadHTMLString:code baseURL:nil];

}

My problem is, whenever I press the button nothing happens. The webviewer remains blank.

I have put breakpoints in the code to double check that the code is called by my app and it is indeed called.

EDIT***

I have fixed the problem by replacing

    [[self youtubeWebPlayer]loadHTMLString:code baseURL:nil];

with

    [[self youtubeWebPlayer]loadHTMLString:code baseURL:[NSURL URLWithString:@"http://www.youtube.com"]];
4

2 回答 2

11

试试这个,效果很好。您将 youtube 网址用作“ 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];
于 2013-09-18T13:26:57.353 回答
0

如果你想在 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)
于 2018-02-19T05:33:17.380 回答