1

I have a form that contains a button and a webBrowser. In webBbrowser1 I load a youtube video with this code

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    var embed = "<html><head>" +
    "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\"/>" +
    "</head><body>" +
    "<iframe width=\"400\" height=\"400\" src=\"{0}\"" +
    "frameborder = \"0\" allow = \"autoplay; encrypted-media;loop\" allowfullscreen></iframe>" +
    "</body></html>";
    var url = "https://www.youtube.com/embed/X1s4TCcpAQ4?autoplay=1&loop=1";

    this.webBrowser1.DocumentText = string.Format(embed, url);

Then I want when I click the button to stop the video.I try this code and works but I dont like very much because is trick and I prefer to understand how works youtube video

private void button1_Click(object sender, EventArgs e)
{
    this.webBrowser1.DocumentText = "";
}

Could someone help me?thanks in addition!

Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
dimmits
  • 1,999
  • 3
  • 12
  • 30
  • Just as a heads up, the Windows Forms WebBrowser control has been known for quite a long time to be [bizarre, unreliable, and generally hard to work with](https://stackoverflow.com/a/174483/4975230); to be honest I'm surprised Youtube displays at all! I don't know of a good alternative for your case unfortunately, but FWIW we ended up just ripping out the WebBrowser control completely and doing something else. I would rank it below even `FTPWebRequest` in reliability and that's quite a hard class to beat... – jrh Oct 05 '18 at 20:57
  • Also some edits I'd recommend: You're not using the [youtube-api] as far as I can tell, maybe remove that tag? There's some misspellings in the body of the question, your code isn't indented properly, there's some other general proofreading you should do, and also I'd probably recommend removing "Could someone help me? thanks in addition!" (the site generally considers "thanks", etc. to be not useful content in posts). – jrh Oct 05 '18 at 21:01
  • 1
    @jrh It's working because the OP is setting ``. When inserted in a new `HtmlDocument`, the `WebBrowser` will initialize using `IE11` compatibility mode (well, in this case it would work anyway). In the question you linked, `browser.Navigate("about:blank");` is used. This will set the compatibility mode to `IE7`. The [YouTube Player API](https://developers.google.com/youtube/iframe_api_reference) is probably a better path anyway. – Jimi Oct 05 '18 at 23:04
  • @Hossein in case it wasn't clear, I'd suggest a title of something like "How to stop a youtube video programmatically" and maybe rewording "I try this code and works but I dont like very much because is trick and I prefer to understand how works youtube video" to "I tried the code below, it seems to work but I don't understand why." Then remove the "Thanks" line. – jrh Oct 29 '18 at 20:03

0 Answers0