我正在尝试将 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);
界面代码
<WebView Name="videoView" HorizontalAlignment="Left" Height="297" Margin="466,150,0,0" Grid.Row="1" VerticalAlignment="Top" Width="441"/>
对于确实使用 MyToolkit 的任何人(违反 YT 条款)。使用此方法时是否仍在跟踪 View ?
最佳答案
试试这个
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" });
}
};
关于windows - 在 Windows 10 通用应用程序上添加 Youtube 嵌入视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33193834/