Here, I have used WebBrowser Control and HTML iFrame for playing YouTube videos. I have used WinForms for design.
Form
In this form, I have appended WebBrowser Control where YouTUbe videos will be played.
Code
On "Play" button, let us add this click event.
- using System;
- using System.Windows.Forms;
- namespace FormTest {
- public partial class Form2: Form {
- public Form2() {
- InitializeComponent();
- }
- private void btnPlay_Click(object sender, EventArgs e) {
- string html = "<html><head>";
- html += "<meta content='IE=Edge' http-equiv='X-UA-Compatible'/>";
- html += "<iframe id='video' src= 'https://www.youtube.com/embed/{0}' width='600' height='300' frameborder='0' allowfullscreen></iframe>";
- html += "</body></html>";
- this.webBrowser1.DocumentText = string.Format(html, txtLink.Text.Split('=')[1]);
- }
- }
- }
Result
Conclusion
We can easily play YouTube videos in Windows/Desktop applications in C# with just a small trick.