大佬教程收集整理的这篇文章主要介绍了在Windows 10 Universal App上添加Youtube嵌入视频,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经尝试了以下代码,并且我能够获得一个YouTube播放器.但视频无法加载.
在Initialize下
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" Grid.Row="1" VerticalAlignment="Top" Width="441"/>
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 10 Universal App上添加Youtube嵌入视频全部内容,希望文章能够帮你解决在Windows 10 Universal App上添加Youtube嵌入视频所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。