C#在你的winforms中嵌入youtube / videos

时间:2018-03-01 10:32:36

标签: c# .net winforms youtube embed

我有一个问题,我在winform中添加了shockwave播放器。但似乎youtube不再支持它了。那么如何将视频/ YouTube视频嵌入到我的winform应用程序中呢?

2 个答案:

答案 0 :(得分:7)

您可以使用WebBrowser控件显示嵌入的YouTube视频。为此,请在表单上放置WebBrowser控件,并将以下代码放在表单中:

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=\"300\" src=\"{0}\"" +
    "frameborder = \"0\" allow = \"autoplay; encrypted-media\" allowfullscreen></iframe>" +
    "</body></html>";
    var url = "https://www.youtube.com/embed/L6ZgzJKfERM";
    this.webBrowser1.DocumentText = string.Format(embed, url);
}

enter image description here

注意

  1. 您应该确保使用正确的网址。例如,您可以在此地址看到的视频:https://www.youtube.com/watch?v=L6ZgzJKfERM,嵌入网址为https://www.youtube.com/embed/L6ZgzJKfERM

  2. 此外,您应确保允许视频以嵌入方式播放。有些视频只允许在youtube上播放,点击“播放”按钮后会收到以下错误:

      

    此视频包含XXXXXX的内容。它受到限制   在某些网站或应用程序上播放。

答案 1 :(得分:0)

[// MOST IMPORTANT NOTE :
        // c# you have button click you upload video and database
        // You show the video using  DataList Control inside you use Literal control
    // Literal control bind using SqlData Source below link use and bind DataList and Literal Control

访问https://youtu.be/hXseP_8ZP5I

 protected void Button1_Click(object sender, EventArgs e)   
       {                 
        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;IntegratedSecurity=True;User Instance=True";
        cn.Open();
        Response.Write("check "+cn.State);                      
        String link = "<iframe width=\"300\" height = \"200\" src = \"https://www.youtube.com/embed/" + TextBox1.Text + "\" frameborder = \"0\" allow = \"autoplay; encrypted-media\" allowfullscreen></iframe>";
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "INSERT INTO Video(VIDEO_DETAILS,PATH)VALUES('"+"YOUTUBE" +"','" + link +"')"; 
        cmd.Connection = cn;
        cmd.ExecuteNonQuery();
        TextBox1.Text = "";
        cn.Close();
      }
            

访问https://i.stack.imgur.com/ECgnj.png][1]