Youtube has recently stopped supporting videos embedded in the format www.youtube.com/v/{key}. So I was trying to convert the video from \"/v/\" to \"/embed/\". However when I t
Use WebBrowser.NavigateToString instead of WebBrowser.Navigate and use the HTML instead of URL. check this screenshot for your easy reference
Try to set your WebBrowser to a "silent mode" (ignoring these script errors). It requires some black IE/COM magic, but it works.
See https://stackoverflow.com/a/6198700/3629903 on how to do it.
WPF have a feature "Browser Emulation" to solve this type of issue.
Add the below enumaration
public enum BrowserEmulationVersion
{
Default = 0,
Version7 = 7000,
Version8 = 8000,
Version8Standards = 8888,
Version9 = 9000,
Version9Standards = 9999,
Version10 = 10000,
Version10Standards = 10001,
Version11 = 11000,
Version11Edge = 11001
}
Create a new class "InternetExplorerBrowserEmulation" and add the below codes in it.
public class InternetExplorerBrowserEmulation
{
private const string InternetExplorerRootKey = @"Software\Microsoft\Internet Explorer";
private const string BrowserEmulationKey = InternetExplorerRootKey + @"\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
public static int GetInternetExplorerMajorVersion()
{
int result;
result = 0;
try
{
RegistryKey key;
key = Registry.LocalMachine.OpenSubKey(InternetExplorerRootKey);
if (key != null)
{
object value;
value = key.GetValue("svcVersion", null) ?? key.GetValue("Version", null);
if (value != null)
{
string version;
int separator;
version = value.ToString();
separator = version.IndexOf('.');
if (separator != -1)
{
int.TryParse(version.Substring(0, separator), out result);
}
}
}
}
catch (SecurityException)
{
// The user does not have the permissions required to read from the registry key.
}
catch (UnauthorizedAccessException)
{
// The user does not have the necessary registry rights.
}
return result;
}
public static bool SetBrowserEmulationVersion(BrowserEmulationVersion browserEmulationVersion)
{
bool result;
result = false;
try
{
RegistryKey key;
key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true);
if (key != null)
{
string programName;
programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
if (browserEmulationVersion != BrowserEmulationVersion.Default)
{
// if it's a valid value, update or create the value
key.SetValue(programName, (int)browserEmulationVersion, RegistryValueKind.DWord);
}
else
{
// otherwise, remove the existing value
key.DeleteValue(programName, false);
}
result = true;
}
}
catch (SecurityException)
{
// The user does not have the permissions required to read from the registry key.
}
catch (UnauthorizedAccessException)
{
// The user does not have the necessary registry rights.
}
return result;
}
public static bool SetBrowserEmulationVersion()
{
int ieVersion;
BrowserEmulationVersion emulationCode;
ieVersion = GetInternetExplorerMajorVersion();
if (ieVersion >= 11)
{
emulationCode = BrowserEmulationVersion.Version11;
}
else
{
switch (ieVersion)
{
case 10:
emulationCode = BrowserEmulationVersion.Version10;
break;
case 9:
emulationCode = BrowserEmulationVersion.Version9;
break;
case 8:
emulationCode = BrowserEmulationVersion.Version8;
break;
default:
emulationCode = BrowserEmulationVersion.Version7;
break;
}
}
return SetBrowserEmulationVersion(emulationCode);
}
public static BrowserEmulationVersion GetBrowserEmulationVersion()
{
BrowserEmulationVersion result;
result = BrowserEmulationVersion.Default;
try
{
RegistryKey key;
key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true);
if (key != null)
{
string programName;
object value;
programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
value = key.GetValue(programName, null);
if (value != null)
{
result = (BrowserEmulationVersion)Convert.ToInt32(value);
}
}
}
catch (SecurityException)
{
// The user does not have the permissions required to read from the registry key.
}
catch (UnauthorizedAccessException)
{
// The user does not have the necessary registry rights.
}
return result;
}
public static bool IsBrowserEmulationSet()
{
return GetBrowserEmulationVersion() != BrowserEmulationVersion.Default;
}
}
Before setting URL, we need to add the below codes.
if (!InternetExplorerBrowserEmulation.IsBrowserEmulationSet())
{
InternetExplorerBrowserEmulation.SetBrowserEmulationVersion();
}
This will run the you tube videos using the webbrowser control in WPF
This is duplicate of an existing SO Thread
Use latest version of Internet Explorer in the webbrowser control
The thread has lots of answers in it with actual code to go with it.
The best suggestion in the same is to set a very high number of for your app.exe
in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
I set it to 20000
, which is safe to assume to work in a lot of coming version and to use the latest version as such. This case is easily done during the setup of your exe as such. So you won't have to worry about which version exists and which doesn't. The minimum version you need for embedding to work is IE 9.
Also, another option is not to use Embedded IE at all. Instead, use Chromium. There is a CefSharp project for the same on
https://cefsharp.github.io/
This project allows you to embed chromium browser in your WinForms or WPF app. The app is quite simple
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
ChromiumWebBrowser chrome;
private void InitChrome()
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
chrome = new ChromiumWebBrowser("https://www.youtube.com/embed/v2fDTOdWuQQ?rel=0&showinfo=0");
this.Controls.Add(chrome);
chrome.Dock = DockStyle.Fill;
}
public Form1()
{
InitializeComponent();
InitChrome();
//this.webBrowser1.Navigate("https://www.youtube.com/embed/v2fDTOdWuQQ?rel=0&showinfo=0");
}
}
}
And works great. This will make your app not be dependent on which browser is installed on the target machine.