C# 在切换用户控件时暂停视频或停止 Web 浏览器
C# Pause video or stop Web browser when switching UserControls
我有一个问题,当我在例如电影控件上播放视频然后使用 bringToFront() 方法切换到家庭控件时。视频还在播放,至少声音还在。
public partial class MoviesControl : UserControl
{
public MoviesControl()
{
InitializeComponent();
int width = 560;
int height = 315;
webBrowser1.Width = width + 2;
webBrowser1.Height = height + 2;
var embed = "<html><head>" +
"<meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge,chrome=1\"/>" +
"</head><body scroll=\"no\" style=\"padding:0px;margin:0px;\">" +
"<iframe style=\"border: 1px solid #0000ff;\" width=\"{1}\" height=\"{2}\" src=\"{0}\"" +
"frameborder=\"0\" allow =\"autoplay; encrypted-media\" ></iframe>" +
"</body></html>";
string url = "https://www.youtube.com/embed/JvSZKB2WNKg?rel=0&showinfo=0";
webBrowser1.DocumentText = string.Format(embed, url, width, height);
}
}
YouTube Player API Reference for iframe Embeds 允许您使用 javascript 代码控制播放器。
为了能够使用该脚本 API,您应该通过将 enablejsapi=1
添加到查询字符串来加载 iframe
。
然后要暂停视频,您正在寻找 pauseVideo
命令。您可以使用以下脚本暂停视频:
var i = document.getElementsByTagName("iframe")[0].contentWindow;
i.postMessage('{"event":"command","func":"pauseVideo","args":""}}', '*');
为了能够从 windows 表单调用它,将它放在您的 html 代码中的一个函数中,然后使用浏览器控件的 WebBrowserDocument.InvokeScript()
方法调用它。
例子
int w = 560;
int h = 315;
this.webBrowser1.Width = w + 2;
this.webBrowser1.Height = h + 2;
var embed = "<html><head>" +
"<meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge,chrome=1\"/>" +
"<script>"+
"function stop() {{"+
"var i = document.getElementsByTagName(\"iframe\")[0].contentWindow;" +
"i.postMessage('{{\"event\":\"command\",\"func\":\"pauseVideo\",\"args\":\"\"}}', '*');" +
"}}</script>"+
"</head><body scroll=\"no\" style=\"padding:0px;margin:0px;\">" +
"<iframe style=\"border: 1px solid #fff;\" width=\"{1}\" height=\"{2}\" src=\"{0}\"" +
"allow =\"autoplay; encrypted-media\" ></iframe>" +
"</body></html>";
var url = "https://www.youtube.com/embed/JvSZKB2WNKg?enablejsapi=1&rel=0&showinfo=0";
webBrowser1.DocumentText = string.Format(embed, url, w, h);
然后您可以随时暂停视频:
webBrowser1.Document.InvokeScript("stop", null);
我有一个问题,当我在例如电影控件上播放视频然后使用 bringToFront() 方法切换到家庭控件时。视频还在播放,至少声音还在。
public partial class MoviesControl : UserControl
{
public MoviesControl()
{
InitializeComponent();
int width = 560;
int height = 315;
webBrowser1.Width = width + 2;
webBrowser1.Height = height + 2;
var embed = "<html><head>" +
"<meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge,chrome=1\"/>" +
"</head><body scroll=\"no\" style=\"padding:0px;margin:0px;\">" +
"<iframe style=\"border: 1px solid #0000ff;\" width=\"{1}\" height=\"{2}\" src=\"{0}\"" +
"frameborder=\"0\" allow =\"autoplay; encrypted-media\" ></iframe>" +
"</body></html>";
string url = "https://www.youtube.com/embed/JvSZKB2WNKg?rel=0&showinfo=0";
webBrowser1.DocumentText = string.Format(embed, url, width, height);
}
}
YouTube Player API Reference for iframe Embeds 允许您使用 javascript 代码控制播放器。
为了能够使用该脚本 API,您应该通过将 enablejsapi=1
添加到查询字符串来加载 iframe
。
然后要暂停视频,您正在寻找 pauseVideo
命令。您可以使用以下脚本暂停视频:
var i = document.getElementsByTagName("iframe")[0].contentWindow;
i.postMessage('{"event":"command","func":"pauseVideo","args":""}}', '*');
为了能够从 windows 表单调用它,将它放在您的 html 代码中的一个函数中,然后使用浏览器控件的 WebBrowserDocument.InvokeScript()
方法调用它。
例子
int w = 560;
int h = 315;
this.webBrowser1.Width = w + 2;
this.webBrowser1.Height = h + 2;
var embed = "<html><head>" +
"<meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge,chrome=1\"/>" +
"<script>"+
"function stop() {{"+
"var i = document.getElementsByTagName(\"iframe\")[0].contentWindow;" +
"i.postMessage('{{\"event\":\"command\",\"func\":\"pauseVideo\",\"args\":\"\"}}', '*');" +
"}}</script>"+
"</head><body scroll=\"no\" style=\"padding:0px;margin:0px;\">" +
"<iframe style=\"border: 1px solid #fff;\" width=\"{1}\" height=\"{2}\" src=\"{0}\"" +
"allow =\"autoplay; encrypted-media\" ></iframe>" +
"</body></html>";
var url = "https://www.youtube.com/embed/JvSZKB2WNKg?enablejsapi=1&rel=0&showinfo=0";
webBrowser1.DocumentText = string.Format(embed, url, w, h);
然后您可以随时暂停视频:
webBrowser1.Document.InvokeScript("stop", null);