UIWebview不能很好地显示适用于iOS 8的嵌入式youtube [英] UIWebview does not display embedded youtube well for iOS 8

查看:50
本文介绍了UIWebview不能很好地显示适用于iOS 8的嵌入式youtube的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难在UIWebview中播放嵌入的youtube.

I have difficulty to play embed youtube in UIWebview.

如果我使用了youtube的默认嵌入格式,则它不会在UIWebview中显示任何内容.只是空白.

If I used the default embed format from youtube, it does not show anything in the UIWebview. Just blank.

NSString *htmlString = @"<html><body><iframe width=\"420\" height=\"315\" src=\"//www.youtube.com/embed/XNnaxGFO18o?rel=0\" frameborder=\"0\" allowfullscreen></iframe></body></html>";
[self.webView loadHTMLString:htmlString baseURL:nil];

我在Google周围搜索,发现以下代码有效.

I googled around and found the following code works.

NSString* embedHTML = @"\
<html><body style=\"margin:0;\">\
<<div class=\"emvideo emvideo-video emvideo-youtube\"><embed id=\"yt\" src=\"http://www.youtube.com/embed/bPM8LKYMcXs?hd=1\" width=\"320\" height=\"250\"></embed></div>\
</body></html>";
[self.webView loadHTMLString:embedHTML baseURL:nil];

但是,有一个问题.如果我单击播放按钮,则无任何响应.我必须单击播放按钮几次,然后等待一会儿,然后它开始旋转然后播放.

However, there is a problem. If I click the play button, it has no response. I have to click the play button a couple of times and wait a while, then it starts to spin and then play.

所以我有两个问题.

1)您是否有更好的方法在UIWebView中播放嵌入的youtube视频? 2)单击播放按钮后如何立即播放视频?

1) Do you have a better way to play embed youtube video in UIWebView? 2) How to play the video instantly after you click the play button?

谢谢.

推荐答案

创建一个普通的html文件并将其命名为youtubeEmbedding.html并将其添加到youtubeEmbedding.html中的项目中,只需将以下代码添加到文件

create a plain html file and name it as youtubeEmbedding.html and add it to your project in the youtubeEmbedding.html just add the below code to .html file

<!DOCTYPE html>
<html>
<head>
    <style>body{margin:0px 0px 0px 0px;}</style>
</head>
<body>
    <div id="player"></div>
    <script>
        var tag = document.createElement('script');
        tag.src = 'http://www.youtube.com/player_api';

        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
        firstScriptTag.requestFullScreen();
        var ytplayer = null;

        function onYouTubePlayerAPIReady() 
        {
            ytplayer = new YT.Player(
                                     'player',
                                     {
                                     videoId:'%@',
                                     width:'%0.0f',
                                     height:'%0.0f', %@
                                     playerVars:
                                     {
                                     'controls': %d,
                                     'playsinline' : 0,
                                     'rel':0,
                                     'showinfo':0
                                     },
                                     });
        }


    function onPlayerReady(event)
    {
        event.target.playVideo();
    }

    function stopVideo()
    {
        if (ytplayer)
        {
            ytplayer.stopVideo();
            ytplayer.clearVideo();
        }
        return 'STOPPED';
    }

    function playVideo()
    {
        if (ytplayer)
        {
            ytplayer.playVideo();
        }
        return 'PLAY';
    }

    function pauseVideo()
    {
        if (ytplayer)
        {
            ytplayer.pauseVideo();
            ytplayer.clearVideo();
        }
        return 'PAUSED';
    }

    function getPlayerState()
    {
        return ytplayer.getPlayerState();
    }

    function isPlaying()
    {
        return (myPlayerState == YT.PlayerState.PLAYING);
    }
    function isPaused()
    {
        return (myPlayerState == YT.PlayerState.PAUSED);
    }
    function onPlayerError(event)
    {
        alert("Error");
    }
    </script>
</body>

并在您正在使用网络视图的控制器中执行以下操作

and in the controller where u are using web view just do like below

- (void)viewDidLoad
{
   BOOL autoPlay = NO;
   // Do any additional setup after loading the view, typically from a nib.
   NSString *youTubeVideoHTML  = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"youtubeEmbedding" ofType:@"html"] encoding:NSUTF8StringEncoding error:nil];
   NSString *autoPlayString    =  autoPlay ? @"events: {'onReady' : onPlayerReady }," : @""; //customise it weather u want to autoplay or not
   NSURL *url = [NSURL URLWithString:@"https://www.youtube.com/watch?v=AYo72E7ZMHM"];
   NSString *lastComponent = [url query];
   NSString *videoId   = [lastComponent  stringByReplacingOccurrencesOfString:@"v=" withString:@""]; //get  the video id from url
   NSInteger controls  = 1; //i want to show controles for play,pause,fullscreen ... etc
   NSString *html      = [NSString stringWithFormat:youTubeVideoHTML, videoId, CGRectGetWidth(_webView.frame), CGRectGetHeight(_webView.frame), autoPlayString, controls]; //setting the youtube video width and height to fill entire the web view 
  _webView.mediaPlaybackRequiresUserAction = NO;
  _webView.delegate = self; //not needed
  [_webView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]]; //load it to webview

}


//for playing action 
- (IBAction)playAction:(id)sender 
{
  [_webView stringByEvaluatingJavaScriptFromString:@"ytplayer.playVideo()"];
}

如果您有任何问题,请发表评论,希望这对您有帮助..:)

comment if u hav any problem, hope this helps u .. :)

这篇关于UIWebview不能很好地显示适用于iOS 8的嵌入式youtube的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆