user9373061
user9373061

Reputation:

C# embed youtube/videos in your winforms

I have a problem, i added shockwave player to the winform. But it seems that youtube does not support it anymore. So how can i embed a video/youtube video to my winform application?

Upvotes: 7

Views: 18850

Answers (2)

AJAY KACHHIYAPATEL
AJAY KACHHIYAPATEL

Reputation: 169

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

Visit 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();
      }
            

visit https://i.sstatic.net/ECgnj.png][1]

Upvotes: 0

Reza Aghaei
Reza Aghaei

Reputation: 125312

You can use a WebBrowser control to show embedded youtube video. To do so, put a WebBrowser control on a form and the put the following code in form:

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

Note

  1. You should make sure you use the correct url. For example for a video that you can see at this address: https://www.youtube.com/watch?v=L6ZgzJKfERM, the embed url is https://www.youtube.com/embed/L6ZgzJKfERM.

  2. Also you should make sure the video is allowed to be played as embedded. Some videos are just allowed to play on youtube and after you click on play button you receive this error:

    This video contains content from XXXXXX. It is restricted from playback on certain sites or applications.

Upvotes: 15

Related Questions