如何在 Windows 应用程序 C# 上播放 youtube 视频 [英] How to play youtube videos on windows application C#

查看:32
本文介绍了如何在 Windows 应用程序 C# 上播放 youtube 视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有 WebBrowser 控件的 Windows 应用程序并尝试播放 youtube 视频.我已经安装了 Adob​​e Shockwave Player,并尝试在我的 Windows 应用程序上观看视频,但它没有显示任何视频.当我点击播放按钮时,屏幕看起来像这样

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

我播放视频的代码是

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);

我的示例视频链接是:http://www.youtube.com/watch?v=oZdnezj9Dfg&feature=BFa&list=PLD3E900BFF6534896&lf=plpp_video

有人可以帮我解决这个问题吗?我是不是漏掉了插件之类的?我正在使用 .NetFramework 4.0

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

推荐答案

我看到的一个问题是您没有在嵌入中引用参数.您的嵌入将生成为:

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...>

您需要引用这些参数:

<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>

您可能有兴趣阅读官方 YouTube 文档.他们已经不再使用嵌入,转而使用 iframe.这让他们可以使用 HTML5 视频播放器(如果可用),或者回退到 Flash.请参阅 http://www.google.com/support/youtube/bin/answer.py?answer=171780 了解详情.

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.

更新:

下面的 HTML 文件以两种方式嵌入同一视频:一次使用 iframe,一次使用标准嵌入.当我使用 WebBrowser 控件导航到它时,它可以正常工作:

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.

此外,如果您已经安装了 Flash,您可以删除外部的 标记.然后,Flash 嵌入看起来就像我最初展示的一样.

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.

而且,您不需要在其周围使用 HTML 包装器.如果我只包含没有 <html> 和其他标签的嵌入,它工作正常.

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.

这篇关于如何在 Windows 应用程序 C# 上播放 youtube 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆