2015-10-18 90 views
4

我试图将YouTube视频嵌入到我的Windows 10通用应用程序中。我知道有些方法可能违背YouTube上的条款,但是有没有办法做到这一点不违背他们?在Windows 10通用应用程序中添加Youtube嵌入式视频

我试过了下面的代码,并且我能够找到一个YouTube播放器。但视频无法加载。

在初始化

string html = @"<style> body{margin:0; padding:0;} iframe{width:100%;height:480px;}@media screen and (max-width:300px) { iframe{width:100%;height:180px;}} </style><iframe style=""padding:0px;margin-bottom:-20px;"" src=""https://www.youtube.com/embed/OLE5oAZanA4" + @" ? rel=0"" frameborder=""0"" allowfullscreen></iframe>"; 
     videoView.NavigateToString(html); 

UI代码

<WebView Name="videoView" HorizontalAlignment="Left" Height="297" Margin="466,150,0,0" Grid.Row="1" VerticalAlignment="Top" Width="441"/> 

对于任何人都不会使用MyToolkit(这违背YT条款)。使用此方法时,视图是否仍在追踪?

回答

0

试试这个

StringBuilder stringbuild = new StringBuilder(); 
     stringbuild.Append("<!DOCTYPE html>"); 
     stringbuild.Append("<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">"); 
     stringbuild.Append("<head> <meta charset=\"utf-8\"/> <title></title> </head>"); 
     stringbuild.Append("<body>"); 
     stringbuild.Append(" <style> iframe {border: none;}"); 
     stringbuild.Append(" html, body {margin: 0px; padding: 0px; border: 0px; width: 100%; height: 100%; overflow:hidden;} </style>"); 
     stringbuild.Append(" <div id=\"player\" style=\"width:200px; height:400px;\"></div>"); 
     stringbuild.Append("<script>"); 
     stringbuild.Append("var tag = document.createElement('script');"); 
     stringbuild.Append("tag.src = \"https://www.youtube.com/iframe_api\";"); 
     stringbuild.Append("var firstScriptTag = document.getElementsByTagName('script')[0];"); 
     stringbuild.Append(" firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);"); 
     stringbuild.Append(" function onYouTubeIframeAPIReady() {window.external.notify(\"YoutubePlayerLoadCompleted\"); }"); 
     stringbuild.Append(" var width; var height;"); 
     stringbuild.Append(" function setwidth(incoming) { width = incoming; document.getElementById(\"player\").style.width = incoming+ 'px'; }"); 
     stringbuild.Append("function setheight(incoming) { height = incoming; document.getElementById(\"player\").style.height = incoming +'px'; }"); 
     stringbuild.Append("var player;"); 
     stringbuild.Append(" function loadplayer(incomming) { player = new YT.Player('player', { height: height, width: width, playerVars: { 'fs': 1 }, videoId: incomming, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); var element = document.getElementById('videoframe'); }"); 
     stringbuild.Append("function onPlayerReady(event) { }"); 
     stringbuild.Append("function onPlayerStateChange(event) {}"); 
     stringbuild.Append("function play() { if (!player){} else { try { player.playVideo();} catch(err){window.external.notify(err.message);} }}"); 
     stringbuild.Append(" function pause() { player.pauseVideo(); }"); 
     stringbuild.Append("</script> </body> </html>"); 
     string ts = stringbuild.ToString(); 

     webview.NavigateToString(ts); 

     webview.ScriptNotify += async delegate (object sender1, NotifyEventArgs e1) 
     { 
      var jsScriptValue = e1.Value; 
      if (jsScriptValue.ToString().Equals("YoutubePlayerLoadCompleted")) 
      { 

       await webview.InvokeScriptAsync("setwidth", new string[] {"500" }); 
       await webview.InvokeScriptAsync("setheight", new string[] {"400" }); 
       await webview.InvokeScriptAsync("loadplayer", new string[] { "_P9lGTiiXW0" }); 

      } 

     }; 
相关问题