如何以编程方式停止嵌入在UIWebView xcode中的Youtube视频

时间:2014-11-14 23:27:11

标签: xcode webview

我有一个用于iPad的XCode6.1开发的应用程序

我有一个带有嵌入视频的UIWebView,这是代码:

[self.webView setAllowsInlineMediaPlayback:YES];
    [self.webView setMediaPlaybackRequiresUserAction:NO];

    NSString* embedHTML = [NSString stringWithFormat:@"\
                           <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='%f' height='%f' src='http://www.youtube.com/embed/%@?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'>\
                           </body>\
                           </html>", self.webView.frame.size.width, self.webView.frame.size.height, @"JW5meKfy3fY"];
    [self.webView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]];

但是如果我切换到另一个ViewController,播放器仍然播放视频,我希望它停止,怎么做?

1 个答案:

答案 0 :(得分:0)

Google开发网站的iOS帮助程序库帮助了我很多。 非常简单!

https://developers.google.com/youtube/v3/guides/ios_youtube_helper

    - (IBAction)playVideo:(id)sender {
      [self.playerView playVideo];
    }

    - (IBAction)stopVideo:(id)sender {
      [self.playerView stopVideo];
    }