Reputation: 362
I have a c# Forms project in VisualStudio in which I want to embed a youtube video.
To do so, I add a web browser control in the designer.
The browser works fine with every URL I gave him except for embeded youtube videos.
Instead of a video I get a black screen, no sound and a "script error" giving me the following url: (https://s.ytimg.com/yts/jsbin/www-embed-player-new-vflQo6seZ/www-embed-player-new.js)
In normal youtube, the web browser works just fine - just like chrome.
Somehow something doesn't work with "youtube.com/embed/...".
I don't think its a problem with HTML5.
How can i solve this problem?
Are there any other web browser I can use in Winforms?
Code that reproduces the error: (in Form1 Constractor)
WebBrowser youtubePlayer = new WebBrowser();
this.Controls.Add(youtubePlayer);
youtubePlayer.Navigate("http://www.youtube.com/embed/M7lc1UVf-VE");
Upvotes: 0
Views: 9902
Reputation: 772
Since the other answer is deprecated, here is how you should do it now:
WebBrowser control (both WPF and WinForms versions) behaves in many ways differently from the full IE. You may want to implement Feature Control to bring its behavior as close to IE as possible (particularly, FEATURE_BROWSER_EMULATION). Here is some code:
private void SetBrowserFeatureControlKey(string feature, string appName, uint value)
{
using (var key = Registry.CurrentUser.CreateSubKey(
String.Concat(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\", feature),
RegistryKeyPermissionCheck.ReadWriteSubTree))
{
key.SetValue(appName, (UInt32)value, RegistryValueKind.DWord);
}
}
For example:
private void SetBrowserFeatureControl()
{
// http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx
// FeatureControl settings are per-process
var fileName = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
// make the control is not running inside Visual Studio Designer
if (String.Compare(fileName, "devenv.exe", true) == 0 || String.Compare(fileName, "XDesProc.exe", true) == 0)
return;
SetBrowserFeatureControlKey("FEATURE_BROWSER_EMULATION", fileName, GetBrowserEmulationMode()); // Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
SetBrowserFeatureControlKey("FEATURE_AJAX_CONNECTIONEVENTS", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_DOMSTORAGE ", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_GPU_RENDERING ", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI ", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_DISABLE_LEGACY_COMPRESSION", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_LOCALMACHINE_LOCKDOWN", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_BLOCK_LMZ_OBJECT", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_BLOCK_LMZ_SCRIPT", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_DISABLE_NAVIGATION_SOUNDS", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_SCRIPTURL_MITIGATION", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_SPELLCHECKING", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_STATUS_BAR_THROTTLING", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_TABBED_BROWSING", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_VALIDATE_NAVIGATE_URL", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_WEBOC_DOCUMENT_ZOOM", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_WEBOC_POPUPMANAGEMENT", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_WEBOC_MOVESIZECHILD", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_ADDON_MANAGEMENT", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_WEBSOCKET", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_WINDOW_RESTRICTIONS ", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_XMLHTTP", fileName, 1);
}
private UInt32 GetBrowserEmulationMode()
{
int browserVersion = 7;
using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer",
RegistryKeyPermissionCheck.ReadSubTree,
System.Security.AccessControl.RegistryRights.QueryValues))
{
var version = ieKey.GetValue("svcVersion");
if (null == version)
{
version = ieKey.GetValue("Version");
if (null == version)
throw new ApplicationException("Microsoft Internet Explorer is required!");
}
int.TryParse(version.ToString().Split('.')[0], out browserVersion);
}
UInt32 mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode. Default value for Internet Explorer 11.
switch (browserVersion)
{
case 7:
mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control.
break;
case 8:
mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8
break;
case 9:
mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9.
break;
case 10:
mode = 10000; // Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 mode. Default value for Internet Explorer 10.
break;
default:
// use IE11 mode by default
break;
}
return mode;
}
You should come up with your own set of features and register them before WebBrowser has initialized, e.g., in the main form constructor:
public MainWindow()
{
SetBrowserFeatureControl();
InitializeComponent();
WebBrowser youtubePlayer = new WebBrowser();
this.Controls.Add(youtubePlayer);
youtubePlayer.Navigate("https://www.youtube.com/embed/dQw4w9WgXcQ");
//...
}
This answer is taken from here: C# webbrowser Ajax call I found it while trying to fix the javascript errors that i got when running this code.
Upvotes: 4
Reputation: 1141
Please visit YouTube Embedded Players and Player Parameters
What you are trying here is an IFrame Embed. Try This: Open Notepad and paste it :
<iframe id="ytplayer" type="text/html" width="640" height="390"
src="http://www.youtube.com/embed/HLj0aLPLsys?autoplay=1"
frameborder="0"/>
Save this as an html file and open it on browser. If everything goes fine you see Elvis Presley singing It's Midnight.This is similar to the string you have tried to make your Web Browser Navigate to.
Now Comeback to your code on Visual Studio and inplace of your earlier
youtubePlayer.Navigate("...");
Put this code :
youtubePlayer.Navigate("http://www.youtube.com/v/HLj0aLPLsys?version=3");//Embedded AS3 Player
Or This,
youtubePlayer.Navigate("http://www.youtube.com/apiplayer?video_id=HLj0aLPLsys&version=3");//Chromeless AS3 Player
Note : both of the solutions to your problems are deprecated but work and are the closest solutions that can be reached.
To make the player run the video automatically without user Interference add to the end of the string
&autoplay=1
To allow the webBrowser to take up the entire space available in the form Bounds set :
youtubePlayer.Dock = DockStyle.Fill;
Also to get rid of the Annoying script Errors message :
youtubePlayer.ScriptErrorsSuppressed = true;
Upvotes: 3