Serkan Hekimoglu
Serkan Hekimoglu

Reputation: 4284

How to play youtube videos on windows application C#

I've created a windows application with a WebBrowser control and trying to play youtube videos.. I've installed Adobe Shockwave Player, and tried to watch video on my windows application, but it doesn't show any videos. When I hit play button the screen looks like this enter image description here

My code for playing video is

StreamWriter sw = new StreamWriter("utube.html");
        string PlayString = currentVideoUrl.Trim().Replace("watch?v=", "v/");
        string Finalplaycode = "<embed src=" + PlayString + "&hl=en&fs=1& type=application/x-shockwave-flash allowscriptaccess=always allowfullscreen=true width=425 height=344></embed>";
        sw.Write(Finalplaycode);
        sw.Close();
        string PathToNavigate = Directory.GetParent(Application.ExecutablePath) + @"\utube.html";
        webBrowser1.Navigate(PathToNavigate);

and my example video link is : http://www.youtube.com/watch?v=oZdnezj9Dfg&feature=BFa&list=PLD3E900BFF6534896&lf=plpp_video

can anyone help me about this issue? Did I missing any plug-in or something? I am using .NetFramework 4.0

Upvotes: 0

Views: 5189

Answers (1)

Jim Mischel
Jim Mischel

Reputation: 133950

One problem I see is that you're not quoting the parameters in the embed. Your embed will generate as:

<embed src=http://www.youtube.com/v/video_id...>

You need to quote those parameters:

<embed src="http://www.youtube.com/v/video_id...">

For example, here's an embed that works on my site:

<embed type="application/x-shockwave-flash" width="425" height="344" 
src="http://www.youtube.com/v/UejelYnVI3U&amp;hl=en&amp;fs=1"></embed>

You might be interested in reading the official YouTube documentation. They've moved away from using embeds, in favor of iframes. That lets them use the HTML5 video player if it's available, or otherwise fall back to Flash. See http://www.google.com/support/youtube/bin/answer.py?answer=171780 for details.

Update:

The HTML file below embeds the same video in two ways: once using the iframe and once using the standard embed. It works find when I navigate to it with the WebBrowser control:

<html>
<body>
<iframe class="youtube-player" type="text/html" width="640" height="385"
 src="http://www.youtube.com/embed/oZdnezj9Dfg" frameborder="0">
</iframe>

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
<param name="src" value="http://www.youtube.com/v/oZdnezj9Dfg&amp;hl=en&amp;fs=1" />
<embed type="application/x-shockwave-flash" width="425" height="344" 
  src="http://www.youtube.com/v/oZdnezj9Dfg&amp;hl=en&amp;fs=1">
</embed>
</object>

</body>
<html>

Try copying that and navigating to it in your application. Make sure that your program is creating the embed just like that.

Also, you can remove the outer <object> tag if you already have Flash installed. Then, the Flash embed looks just like what I showed originally.

And, there's no requirement that you have the HTML wrapper around it. It works fine if I just include the embed without the <html> and other tags.

Upvotes: 2

Related Questions