Problem embedding an youtube video on iphone

2019-07-19 18:14发布

问题:

I am trying to embed a video with the following code to avoid launching the youtube app and stay in my app:

-(void)embedYouTube:(NSString*)url frame:(CGRect)frame {  

 NSString* embedHTML = @"<iframe type=\"text/html\" width=\"64\" height=\"39\" src=\"http://www.youtube.com/embed/j8Bc7eRTdWY\" frameborder=\"0\"></iframe>";
    if(videoView == nil) {          
        videoView = [[UIWebView alloc] initWithFrame:frame];  
        [movieDescView addSubview:videoView];  
    }  
    [videoView loadHTMLString:embedHTML baseURL:nil];  
}

The problem is when I clicked on the play icon, it opens the youtube website with my uiwebview asking to install some web app . I would like to prevent this web app pop up to show or start the video full screen. Actually, any solution to show a youtube video without exiting my app would do.

回答1:

You can use this to load the movie (no needed to generate a html string):

movieView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 140, 100)];
movieView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.youtube.com/embed/-h4zTEwgCpQ"]];
[movieView loadRequest:request];
[self.view addSubview:movieView];


#pragma mark -
#pragma mark UIWebViewDelegate
//To check if youtube try to show the overview page of m.youtube.com or the current movie
//if the user click on the youtube logo this method stop loading the mobile page
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *urlRequest = [request URL];

    if ([[urlRequest absoluteString] isEqualToString:url]) {
        return YES;
    }

    return NO;
}

The onely thing you have to looking for, is to use the "embed" link.



回答2:

UIWebView  *wbView = (UIWebView *)[self.view viewWithTag:1];
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,url, 64.0, 64.0];
    [wbView loadHTMLString:html baseURL:nil];

  "url" is your video url


回答3:

You don't use embed if you are trying to play the video. Just use the normal video URL so it will open in the iPhone's YouTube application rather than the web. If it opens in the web it will ask to install it's web app.

You need to change your HTML to this:

<a href="http://www.youtube.com/watch?v=j8Bc7eRTdWY">Play Video</a>

Or to launch it directly to the native app use the URL:

http://www.youtube.com/watch?v=j8Bc7eRTdWY

More Reference Information:

Apple Reference on URL Schemes

Apple Reference on Youtube Links



回答4:

Tips:

stringUrl = [stringUrl stringByReplacingOccurrencesOfString:@"/watch?v=" withString:@"/embed/"];

where stringUrl is a video link of youtube ( ex. Youtube )



回答5:

I spent quite a bit of time looking through libraries and old code snippets; this library worked for me:

https://github.com/larcus94/LBYouTubeView